Spaces:
Paused
Paused
gradio
Browse files
app.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
-
import
|
2 |
from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate
|
3 |
from llama_index.llms.huggingface import HuggingFaceInferenceAPI
|
4 |
from dotenv import load_dotenv
|
5 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
6 |
from llama_index.core import Settings
|
7 |
import os
|
8 |
-
import
|
9 |
|
10 |
# Load environment variables
|
11 |
load_dotenv()
|
@@ -31,12 +31,6 @@ DATA_DIR = "data"
|
|
31 |
os.makedirs(DATA_DIR, exist_ok=True)
|
32 |
os.makedirs(PERSIST_DIR, exist_ok=True)
|
33 |
|
34 |
-
def displayPDF(file):
|
35 |
-
with open(file, "rb") as f:
|
36 |
-
base64_pdf = base64.b64encode(f.read()).decode('utf-8')
|
37 |
-
pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="100%" height="600" type="application/pdf"></iframe>'
|
38 |
-
st.markdown(pdf_display, unsafe_allow_html=True)
|
39 |
-
|
40 |
def data_ingestion():
|
41 |
documents = SimpleDirectoryReader(DATA_DIR).load_data()
|
42 |
storage_context = StorageContext.from_defaults()
|
@@ -69,33 +63,54 @@ def handle_query(query):
|
|
69 |
else:
|
70 |
return "Sorry, I couldn't find an answer."
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
st.markdown("start chat ...🚀")
|
77 |
-
|
78 |
-
if 'messages' not in st.session_state:
|
79 |
-
st.session_state.messages = [{'role': 'assistant', "content": 'Hello! Upload a PDF and ask me anything about its content.'}]
|
80 |
-
|
81 |
-
with st.sidebar:
|
82 |
-
st.title("Menu:")
|
83 |
-
uploaded_file = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button")
|
84 |
-
if st.button("Submit & Process"):
|
85 |
-
with st.spinner("Processing..."):
|
86 |
-
filepath = "data/saved_pdf.pdf"
|
87 |
-
with open(filepath, "wb") as f:
|
88 |
-
f.write(uploaded_file.getbuffer())
|
89 |
-
# displayPDF(filepath) # Display the uploaded PDF
|
90 |
-
data_ingestion() # Process PDF every time new file is uploaded
|
91 |
-
st.success("Done")
|
92 |
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
st.write(message['content'])
|
|
|
1 |
+
import gradio as gr
|
2 |
from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate
|
3 |
from llama_index.llms.huggingface import HuggingFaceInferenceAPI
|
4 |
from dotenv import load_dotenv
|
5 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
6 |
from llama_index.core import Settings
|
7 |
import os
|
8 |
+
import tempfile
|
9 |
|
10 |
# Load environment variables
|
11 |
load_dotenv()
|
|
|
31 |
os.makedirs(DATA_DIR, exist_ok=True)
|
32 |
os.makedirs(PERSIST_DIR, exist_ok=True)
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
def data_ingestion():
|
35 |
documents = SimpleDirectoryReader(DATA_DIR).load_data()
|
36 |
storage_context = StorageContext.from_defaults()
|
|
|
63 |
else:
|
64 |
return "Sorry, I couldn't find an answer."
|
65 |
|
66 |
+
def process_file(file):
|
67 |
+
if file is None:
|
68 |
+
return "Please upload a PDF file."
|
69 |
+
|
70 |
+
temp_dir = tempfile.mkdtemp()
|
71 |
+
temp_path = os.path.join(temp_dir, "uploaded.pdf")
|
72 |
+
|
73 |
+
with open(temp_path, "wb") as f:
|
74 |
+
f.write(file.read())
|
75 |
+
|
76 |
+
# Copy the file to the DATA_DIR
|
77 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
78 |
+
dest_path = os.path.join(DATA_DIR, "saved_pdf.pdf")
|
79 |
+
os.replace(temp_path, dest_path)
|
80 |
+
|
81 |
+
# Process the uploaded PDF
|
82 |
+
data_ingestion()
|
83 |
+
|
84 |
+
return "PDF processed successfully. You can now ask questions about its content."
|
85 |
|
86 |
+
def chatbot(message, history):
|
87 |
+
response = handle_query(message)
|
88 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
+
# Gradio interface
|
91 |
+
with gr.Blocks() as demo:
|
92 |
+
gr.Markdown("# (PDF) Information and Inference🗞️")
|
93 |
+
gr.Markdown("Retrieval-Augmented Generation")
|
94 |
+
|
95 |
+
with gr.Row():
|
96 |
+
with gr.Column(scale=1):
|
97 |
+
file_output = gr.Textbox(label="Upload Status")
|
98 |
+
upload_button = gr.UploadButton("Upload PDF", file_types=[".pdf"])
|
99 |
+
upload_button.upload(process_file, upload_button, file_output)
|
100 |
+
|
101 |
+
with gr.Column(scale=2):
|
102 |
+
chatbot = gr.Chatbot(
|
103 |
+
[],
|
104 |
+
elem_id="chatbot",
|
105 |
+
bubble_full_width=False,
|
106 |
+
)
|
107 |
+
msg = gr.Textbox(label="Ask me anything about the content of the PDF:")
|
108 |
+
clear = gr.Button("Clear")
|
109 |
+
|
110 |
+
msg.submit(chatbot, [msg, chatbot], [chatbot, msg]).then(
|
111 |
+
lambda: gr.update(value=""), None, [msg], queue=False
|
112 |
+
)
|
113 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
114 |
|
115 |
+
if __name__ == "__main__":
|
116 |
+
demo.launch()
|
|