-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
64 lines (48 loc) · 1.82 KB
/
Copy pathapp.py
File metadata and controls
64 lines (48 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Streamlit App"""
import tempfile
import threading
from queue import Queue
import streamlit as st
from langchain.document_loaders import PyMuPDFLoader
from langchain.schema import Document
from streamlit.runtime.uploaded_file_manager import UploadedFile
from rabbithole import summarize_document
# Global variables
q = Queue()
def load_file(file: UploadedFile) -> list[Document]:
"""
Load a file and return a list of Document objects
:param file: File to load.
Supported file types: PDF
:return: List of Document objects
"""
if file.type == "application/pdf":
# Save to temporary file
temp_file = tempfile.NamedTemporaryFile(delete=False)
temp_file.write(file.read())
temp_file.close()
# Load the file using PyMuPDF
return PyMuPDFLoader(file_path=temp_file.name).load_and_split()
else:
raise ValueError(f"Unsupported file type: {file.type}")
def run_summarization(q: Queue, document: list[Document]):
"""Execute the text summarization"""
q.put(summarize_document(document))
st.title("RabbitHole")
uploaded_file = st.file_uploader("Upload a text file", type=["pdf"], accept_multiple_files=False)
if st.button("Summarize"):
if uploaded_file is None:
st.warning("Please upload a file first.")
st.stop()
# Load the text from the uploaded PDF file
text = load_file(uploaded_file)
# Start the summarization in a separate thread
thread = threading.Thread(target=run_summarization, args=(q, text))
thread.start()
# Display a loading animation while waiting for the summarization to complete
with st.spinner('Summarizing...'):
thread.join()
# Retrieve the result from the queue
summarized_text = q.get()
st.success('Summarization completed.')
st.write(summarized_text)