Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 {
|
8 |
+
height: 350px;
|
9 |
+
overflow: auto;
|
10 |
+
border-radius: 10px;
|
11 |
+
border: 1px solid #e0e0e0;
|
12 |
+
}
|
13 |
+
.textbox {
|
14 |
+
border-radius: 20px !important;
|
15 |
+
padding: 12px 20px !important;
|
16 |
+
}
|
17 |
+
.btn-column {
|
18 |
+
display: flex;
|
19 |
+
flex-direction: column;
|
20 |
+
gap: 10px;
|
21 |
+
}
|
22 |
+
"""
|
23 |
+
|
24 |
+
def upload_pdf(pdf_file):
|
25 |
+
global qa_chain
|
26 |
+
try:
|
27 |
+
qa_chain = create_qa_chain_from_pdf(pdf_file.name)
|
28 |
+
return gr.update(visible=True), f"✅ PDF `{pdf_file.name}` 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 + [[message, "⚠️ Please upload a PDF first."]]
|
36 |
+
try:
|
37 |
+
result = qa_chain({"query": message})
|
38 |
+
answer = result["result"]
|
39 |
+
chat_history.append((message, answer))
|
40 |
+
return "", chat_history
|
41 |
+
except Exception as e:
|
42 |
+
return "", chat_history + [[message, f"❌ Error: {e}"]]
|
43 |
+
|
44 |
+
def create_interface():
|
45 |
+
with gr.Blocks(css=css, theme="soft") as demo:
|
46 |
+
gr.Markdown("""
|
47 |
+
<h1 style='text-align: center;'>📘 University of Education Lahore PDF Chatbot</h1>
|
48 |
+
<p style='text-align: center;'>Upload a university prospectus or document and ask anything!</p>
|
49 |
+
""")
|
50 |
+
|
51 |
+
pdf_input = gr.File(label="Upload PDF", type="file", file_types=[".pdf"])
|
52 |
+
status = gr.Markdown()
|
53 |
+
|
54 |
+
chatbot = gr.Chatbot(elem_id="chatbot", visible=False)
|
55 |
+
|
56 |
+
examples = [
|
57 |
+
"What are the admission requirements?",
|
58 |
+
"How can I contact the administration?",
|
59 |
+
"What programs are offered?"
|
60 |
+
]
|
61 |
+
|
62 |
+
with gr.Row():
|
63 |
+
message = gr.Textbox(
|
64 |
+
label="Type your question here",
|
65 |
+
placeholder="Ask about the uploaded PDF...",
|
66 |
+
elem_classes="textbox",
|
67 |
+
scale=4
|
68 |
+
)
|
69 |
+
with gr.Column(scale=1, elem_classes="btn-column"):
|
70 |
+
submit_button = gr.Button("↩️ Enter")
|
71 |
+
reset_button = gr.Button("🗑️ Reset Chat")
|
72 |
+
|
73 |
+
pdf_input.change(upload_pdf, inputs=[pdf_input], outputs=[chatbot, status])
|
74 |
+
message.submit(respond, [message, chatbot], [message, chatbot])
|
75 |
+
submit_button.click(respond, [message, chatbot], [message, chatbot])
|
76 |
+
reset_button.click(lambda: [], None, chatbot)
|
77 |
+
|
78 |
+
gr.Examples(examples, inputs=message)
|
79 |
+
|
80 |
+
return demo
|
81 |
+
|
82 |
+
if __name__ == "__main__":
|
83 |
+
demo = create_interface()
|
84 |
+
demo.launch()
|