Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
from pdf_bot import create_qa_chain_from_pdf
|
3 |
|
4 |
-
qa_chain = None
|
5 |
|
6 |
css = """
|
7 |
#chatbot {
|
@@ -27,25 +27,22 @@ def upload_pdf(pdf_file_path):
|
|
27 |
qa_chain = create_qa_chain_from_pdf(pdf_file_path)
|
28 |
return gr.update(visible=True), f"✅ PDF `{pdf_file_path.split('/')[-1]}` loaded successfully!"
|
29 |
except Exception as e:
|
30 |
-
qa_chain = None
|
31 |
return gr.update(visible=False), f"❌ Error: {e}"
|
32 |
|
33 |
def respond(message, chat_history):
|
34 |
global qa_chain
|
35 |
if qa_chain is None:
|
36 |
-
chat_history
|
37 |
-
|
38 |
-
return "", chat_history
|
39 |
-
|
40 |
try:
|
41 |
result = qa_chain({"query": message})
|
42 |
answer = result["result"]
|
43 |
-
chat_history.append(
|
44 |
-
chat_history.append(
|
45 |
return "", chat_history
|
46 |
except Exception as e:
|
47 |
-
chat_history.append(
|
48 |
-
chat_history.append(
|
49 |
return "", chat_history
|
50 |
|
51 |
def create_interface():
|
@@ -57,7 +54,8 @@ def create_interface():
|
|
57 |
|
58 |
pdf_input = gr.File(label="Upload PDF", type="filepath", file_types=[".pdf"])
|
59 |
status = gr.Markdown()
|
60 |
-
|
|
|
61 |
|
62 |
examples = [
|
63 |
"What are the admission requirements?",
|
@@ -87,4 +85,4 @@ def create_interface():
|
|
87 |
|
88 |
if __name__ == "__main__":
|
89 |
demo = create_interface()
|
90 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from pdf_bot import create_qa_chain_from_pdf
|
3 |
|
4 |
+
qa_chain = None # Global chain reference
|
5 |
|
6 |
css = """
|
7 |
#chatbot {
|
|
|
27 |
qa_chain = create_qa_chain_from_pdf(pdf_file_path)
|
28 |
return gr.update(visible=True), f"✅ PDF `{pdf_file_path.split('/')[-1]}` loaded successfully!"
|
29 |
except Exception as e:
|
|
|
30 |
return gr.update(visible=False), f"❌ Error: {e}"
|
31 |
|
32 |
def respond(message, chat_history):
|
33 |
global qa_chain
|
34 |
if qa_chain is None:
|
35 |
+
return message, chat_history + [{"role": "assistant", "content": "⚠️ Please upload a PDF first."}]
|
36 |
+
|
|
|
|
|
37 |
try:
|
38 |
result = qa_chain({"query": message})
|
39 |
answer = result["result"]
|
40 |
+
chat_history.append({"role": "user", "content": message})
|
41 |
+
chat_history.append({"role": "assistant", "content": answer})
|
42 |
return "", chat_history
|
43 |
except Exception as e:
|
44 |
+
chat_history.append({"role": "user", "content": message})
|
45 |
+
chat_history.append({"role": "assistant", "content": f"❌ Error: {e}"})
|
46 |
return "", chat_history
|
47 |
|
48 |
def create_interface():
|
|
|
54 |
|
55 |
pdf_input = gr.File(label="Upload PDF", type="filepath", file_types=[".pdf"])
|
56 |
status = gr.Markdown()
|
57 |
+
|
58 |
+
chatbot = gr.Chatbot(elem_id="chatbot", visible=False, type="messages") # ✅ fixed type here
|
59 |
|
60 |
examples = [
|
61 |
"What are the admission requirements?",
|
|
|
85 |
|
86 |
if __name__ == "__main__":
|
87 |
demo = create_interface()
|
88 |
+
demo.launch()
|