Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,7 +9,7 @@ if not openai_api_key:
|
|
9 |
raise ValueError("β OPENAI_API_KEY not found. Add it in Space settings > Secrets.")
|
10 |
os.environ["OPENAI_API_KEY"] = openai_api_key
|
11 |
|
12 |
-
# β
Set Hugging Face Embedding globally
|
13 |
embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
14 |
Settings.embed_model = embed_model
|
15 |
|
@@ -22,7 +22,7 @@ def load_filtered_docs(folder):
|
|
22 |
print(f"β Error loading docs from {folder}: {e}")
|
23 |
return []
|
24 |
|
25 |
-
# β
Load
|
26 |
pg_docs = load_filtered_docs("data/paul")
|
27 |
pg_index = VectorStoreIndex.from_documents(pg_docs)
|
28 |
pg_engine = pg_index.as_query_engine()
|
@@ -50,26 +50,23 @@ def query_ins(query):
|
|
50 |
|
51 |
# β
Predefined questions
|
52 |
paul_questions = [
|
53 |
-
|
54 |
"What is the main purpose of writing, according to Paul Graham?",
|
55 |
"Why do students often struggle with writing in school?",
|
56 |
"How does Paul Graham describe the relationship between writing and thinking?",
|
57 |
"What is one reason Paul Graham gives for why school essays feel boring?",
|
58 |
-
"What does Paul Graham suggest writers should focus on first?"
|
59 |
-
"What is the link between curiosity and writing?"
|
60 |
"How can one write more clearly according to Paul Graham?"
|
61 |
]
|
62 |
|
63 |
insurance_questions = [
|
64 |
-
|
65 |
"What is insurance and why is it important?",
|
66 |
-
"What should you check before buying insurance?"
|
67 |
"What are the primary types of insurance?",
|
68 |
-
"What is health insurance and what does it cover",
|
69 |
"How does life insurance differ from term insurance?",
|
70 |
"What is the difference between premium and coverage?",
|
71 |
"How is insurance regulated?"
|
72 |
-
|
73 |
]
|
74 |
|
75 |
# β
Gradio interface
|
@@ -90,6 +87,7 @@ def launch_interface():
|
|
90 |
gr.Markdown("""<div id='header-text'><h1>RAG Bot with LlamaIndex (PDF + TXT)</h1></div>""")
|
91 |
|
92 |
with gr.Tabs():
|
|
|
93 |
with gr.Tab("Paul Graham"):
|
94 |
if os.path.exists("data/logo.png"):
|
95 |
gr.Image("data/logo.png", show_label=False, container=False, height=120)
|
@@ -116,10 +114,10 @@ def launch_interface():
|
|
116 |
def handle_pg_clear():
|
117 |
return "", "", ""
|
118 |
|
119 |
-
#dropdown_pg.change(query_pg, inputs=dropdown_pg, outputs=output_pg)
|
120 |
submit_pg.click(handle_pg_submit, inputs=[dropdown_pg, textbox_pg], outputs=output_pg)
|
121 |
clear_pg.click(fn=handle_pg_clear, outputs=[dropdown_pg, textbox_pg, output_pg])
|
122 |
|
|
|
123 |
with gr.Tab("Insurance"):
|
124 |
gr.Markdown("""
|
125 |
<div id='header-text'>
|
@@ -143,11 +141,43 @@ def launch_interface():
|
|
143 |
def handle_ins_clear():
|
144 |
return "", "", ""
|
145 |
|
146 |
-
#dropdown_ins.change(query_ins, inputs=dropdown_ins, outputs=output_ins)
|
147 |
submit_ins.click(handle_ins_submit, inputs=[dropdown_ins, textbox_ins], outputs=output_ins)
|
148 |
clear_ins.click(fn=handle_ins_clear, outputs=[dropdown_ins, textbox_ins, output_ins])
|
149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
demo.launch()
|
151 |
|
152 |
-
# β
Launch app
|
153 |
launch_interface()
|
|
|
9 |
raise ValueError("β OPENAI_API_KEY not found. Add it in Space settings > Secrets.")
|
10 |
os.environ["OPENAI_API_KEY"] = openai_api_key
|
11 |
|
12 |
+
# β
Set Hugging Face Embedding globally
|
13 |
embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
14 |
Settings.embed_model = embed_model
|
15 |
|
|
|
22 |
print(f"β Error loading docs from {folder}: {e}")
|
23 |
return []
|
24 |
|
25 |
+
# β
Load predefined document sets
|
26 |
pg_docs = load_filtered_docs("data/paul")
|
27 |
pg_index = VectorStoreIndex.from_documents(pg_docs)
|
28 |
pg_engine = pg_index.as_query_engine()
|
|
|
50 |
|
51 |
# β
Predefined questions
|
52 |
paul_questions = [
|
|
|
53 |
"What is the main purpose of writing, according to Paul Graham?",
|
54 |
"Why do students often struggle with writing in school?",
|
55 |
"How does Paul Graham describe the relationship between writing and thinking?",
|
56 |
"What is one reason Paul Graham gives for why school essays feel boring?",
|
57 |
+
"What does Paul Graham suggest writers should focus on first?",
|
58 |
+
"What is the link between curiosity and writing?",
|
59 |
"How can one write more clearly according to Paul Graham?"
|
60 |
]
|
61 |
|
62 |
insurance_questions = [
|
|
|
63 |
"What is insurance and why is it important?",
|
64 |
+
"What should you check before buying insurance?",
|
65 |
"What are the primary types of insurance?",
|
66 |
+
"What is health insurance and what does it cover?",
|
67 |
"How does life insurance differ from term insurance?",
|
68 |
"What is the difference between premium and coverage?",
|
69 |
"How is insurance regulated?"
|
|
|
70 |
]
|
71 |
|
72 |
# β
Gradio interface
|
|
|
87 |
gr.Markdown("""<div id='header-text'><h1>RAG Bot with LlamaIndex (PDF + TXT)</h1></div>""")
|
88 |
|
89 |
with gr.Tabs():
|
90 |
+
# β
Tab 1: Paul Graham
|
91 |
with gr.Tab("Paul Graham"):
|
92 |
if os.path.exists("data/logo.png"):
|
93 |
gr.Image("data/logo.png", show_label=False, container=False, height=120)
|
|
|
114 |
def handle_pg_clear():
|
115 |
return "", "", ""
|
116 |
|
|
|
117 |
submit_pg.click(handle_pg_submit, inputs=[dropdown_pg, textbox_pg], outputs=output_pg)
|
118 |
clear_pg.click(fn=handle_pg_clear, outputs=[dropdown_pg, textbox_pg, output_pg])
|
119 |
|
120 |
+
# β
Tab 2: Insurance
|
121 |
with gr.Tab("Insurance"):
|
122 |
gr.Markdown("""
|
123 |
<div id='header-text'>
|
|
|
141 |
def handle_ins_clear():
|
142 |
return "", "", ""
|
143 |
|
|
|
144 |
submit_ins.click(handle_ins_submit, inputs=[dropdown_ins, textbox_ins], outputs=output_ins)
|
145 |
clear_ins.click(fn=handle_ins_clear, outputs=[dropdown_ins, textbox_ins, output_ins])
|
146 |
|
147 |
+
# β
Tab 3: Upload & Ask
|
148 |
+
with gr.Tab("Upload & Ask"):
|
149 |
+
gr.Markdown("""
|
150 |
+
<div id='header-text'>
|
151 |
+
<h2>Upload Your Document and Ask Questions</h2>
|
152 |
+
<p>Supported formats: PDF, TXT</p>
|
153 |
+
</div>
|
154 |
+
""")
|
155 |
+
|
156 |
+
upload_input = gr.File(label="Upload a document", file_types=[".pdf", ".txt"])
|
157 |
+
user_question = gr.Textbox(label="Ask a question", placeholder="Type your question here", lines=2)
|
158 |
+
upload_output = gr.Textbox(label="Response", lines=10)
|
159 |
+
upload_submit = gr.Button("Submit")
|
160 |
+
upload_clear = gr.Button("Clear")
|
161 |
+
|
162 |
+
def handle_upload_question(file, query):
|
163 |
+
if not file or not query.strip():
|
164 |
+
return "β Please upload a file and enter a valid question."
|
165 |
+
try:
|
166 |
+
reader = SimpleDirectoryReader(input_files=[file.name])
|
167 |
+
docs = reader.load_data()
|
168 |
+
temp_index = VectorStoreIndex.from_documents(docs)
|
169 |
+
temp_engine = temp_index.as_query_engine()
|
170 |
+
return str(temp_engine.query(query))
|
171 |
+
except Exception as e:
|
172 |
+
return f"β Error: {str(e)}"
|
173 |
+
|
174 |
+
def handle_upload_clear():
|
175 |
+
return None, "", ""
|
176 |
+
|
177 |
+
upload_submit.click(fn=handle_upload_question, inputs=[upload_input, user_question], outputs=upload_output)
|
178 |
+
upload_clear.click(fn=handle_upload_clear, outputs=[upload_input, user_question, upload_output])
|
179 |
+
|
180 |
demo.launch()
|
181 |
|
182 |
+
# β
Launch app
|
183 |
launch_interface()
|