Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,3 @@
|
|
| 1 |
-
#pip install llama-index-embeddings-huggingface
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import os
|
| 4 |
-
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
|
| 5 |
-
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
# β
Access OpenAI API Key
|
| 9 |
-
openai_api_key = os.environ.get("OPENAI_API_KEY")
|
| 10 |
-
if not openai_api_key:
|
| 11 |
-
raise ValueError("β OPENAI_API_KEY not found. Add it in Space settings > Secrets.")
|
| 12 |
-
os.environ["OPENAI_API_KEY"] = openai_api_key
|
| 13 |
-
|
| 14 |
-
# β
Set Hugging Face Embedding globally via Settings
|
| 15 |
-
embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 16 |
-
Settings.embed_model = embed_model # Replaces deprecated ServiceContext
|
| 17 |
-
|
| 18 |
-
# β
Helper to load and filter documents
|
| 19 |
-
def load_filtered_docs(folder):
|
| 20 |
-
docs = SimpleDirectoryReader(folder).load_data()
|
| 21 |
-
return [doc for doc in docs if doc.text and doc.text.strip()]
|
| 22 |
-
|
| 23 |
-
# β
Load Paul Graham documents
|
| 24 |
-
pg_docs = load_filtered_docs("data/paul")
|
| 25 |
-
pg_index = VectorStoreIndex.from_documents(pg_docs)
|
| 26 |
-
pg_engine = pg_index.as_query_engine()
|
| 27 |
-
|
| 28 |
-
# β
Load Insurance documents
|
| 29 |
-
ins_docs = load_filtered_docs("data/insurance")
|
| 30 |
-
ins_index = VectorStoreIndex.from_documents(ins_docs)
|
| 31 |
-
ins_engine = ins_index.as_query_engine()
|
| 32 |
-
|
| 33 |
-
# β
Query functions
|
| 34 |
-
def query_pg(query):
|
| 35 |
-
if not query.strip():
|
| 36 |
-
return "β Please enter a valid question before submitting."
|
| 37 |
-
try:
|
| 38 |
-
return str(pg_engine.query(query))
|
| 39 |
-
except Exception as e:
|
| 40 |
-
return f"β Error: {str(e)}"
|
| 41 |
-
|
| 42 |
-
def query_ins(query):
|
| 43 |
-
if not query.strip():
|
| 44 |
-
return "β Please enter a valid question before submitting."
|
| 45 |
-
try:
|
| 46 |
-
return str(ins_engine.query(query))
|
| 47 |
-
except Exception as e:
|
| 48 |
-
return f"β Error: {str(e)}"
|
| 49 |
-
|
| 50 |
-
# β
Predefined questions
|
| 51 |
-
paul_questions = [
|
| 52 |
-
"What is the main purpose of writing, according to Paul Graham?",
|
| 53 |
-
"Why do students often struggle with writing in school?",
|
| 54 |
-
"How does Paul Graham describe the relationship between writing and thinking?",
|
| 55 |
-
"What is one reason Paul Graham gives for why school essays feel boring?",
|
| 56 |
-
"What does Paul Graham suggest writers should focus on first?"
|
| 57 |
-
]
|
| 58 |
-
|
| 59 |
-
insurance_questions = [
|
| 60 |
-
"What is insurance and why is it important?",
|
| 61 |
-
"What types of insurance are common?",
|
| 62 |
-
"How does life insurance work?",
|
| 63 |
-
"What is the difference between premium and coverage?",
|
| 64 |
-
"What should you check before buying insurance?"
|
| 65 |
-
]
|
| 66 |
-
|
| 67 |
-
# β
Gradio Interface
|
| 68 |
def launch_interface():
|
| 69 |
with gr.Blocks(
|
| 70 |
title="RAG App",
|
|
@@ -79,14 +12,11 @@ def launch_interface():
|
|
| 79 |
"""
|
| 80 |
) as demo:
|
| 81 |
|
| 82 |
-
gr.Markdown("""
|
| 83 |
-
<div id='header-text'>
|
| 84 |
-
<h1>RAG Bot with LlamaIndex (PDF + TXT)</h1>
|
| 85 |
-
</div>
|
| 86 |
-
""")
|
| 87 |
|
| 88 |
with gr.Tabs():
|
| 89 |
with gr.Tab("Paul Graham"):
|
|
|
|
| 90 |
if os.path.exists("data/logo.png"):
|
| 91 |
gr.Image("data/logo.png", show_label=False, container=False, height=120)
|
| 92 |
|
|
@@ -97,15 +27,20 @@ def launch_interface():
|
|
| 97 |
</div>
|
| 98 |
""")
|
| 99 |
|
| 100 |
-
dropdown_pg = gr.Dropdown(label="Pick a Question", choices=paul_questions, interactive=True)
|
| 101 |
textbox_pg = gr.Textbox(label="Ask Anything", placeholder="Type your question...", lines=2)
|
| 102 |
output_pg = gr.Textbox(label="Response", lines=10)
|
| 103 |
-
|
| 104 |
submit_pg = gr.Button("Submit")
|
| 105 |
clear_pg = gr.Button("Clear")
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
clear_pg.click(lambda: ("", "", ""), outputs=[dropdown_pg, textbox_pg, output_pg])
|
| 110 |
|
| 111 |
with gr.Tab("Insurance"):
|
|
@@ -116,18 +51,20 @@ def launch_interface():
|
|
| 116 |
</div>
|
| 117 |
""")
|
| 118 |
|
| 119 |
-
dropdown_ins = gr.Dropdown(label="Pick a Question", choices=insurance_questions, interactive=True)
|
| 120 |
textbox_ins = gr.Textbox(label="Ask Anything", placeholder="Type your question...", lines=2)
|
| 121 |
output_ins = gr.Textbox(label="Response", lines=10)
|
| 122 |
-
|
| 123 |
submit_ins = gr.Button("Submit")
|
| 124 |
clear_ins = gr.Button("Clear")
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
clear_ins.click(lambda: ("", "", ""), outputs=[dropdown_ins, textbox_ins, output_ins])
|
| 129 |
|
| 130 |
demo.launch()
|
| 131 |
-
|
| 132 |
-
if __name__ == "__main__":
|
| 133 |
-
launch_interface()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def launch_interface():
|
| 2 |
with gr.Blocks(
|
| 3 |
title="RAG App",
|
|
|
|
| 12 |
"""
|
| 13 |
) as demo:
|
| 14 |
|
| 15 |
+
gr.Markdown("""<div id='header-text'><h1>RAG Bot with LlamaIndex (PDF + TXT)</h1></div>""")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
with gr.Tabs():
|
| 18 |
with gr.Tab("Paul Graham"):
|
| 19 |
+
# β
Logo moved above heading
|
| 20 |
if os.path.exists("data/logo.png"):
|
| 21 |
gr.Image("data/logo.png", show_label=False, container=False, height=120)
|
| 22 |
|
|
|
|
| 27 |
</div>
|
| 28 |
""")
|
| 29 |
|
| 30 |
+
dropdown_pg = gr.Dropdown(label="Pick a Question", choices=[""] + paul_questions, interactive=True)
|
| 31 |
textbox_pg = gr.Textbox(label="Ask Anything", placeholder="Type your question...", lines=2)
|
| 32 |
output_pg = gr.Textbox(label="Response", lines=10)
|
|
|
|
| 33 |
submit_pg = gr.Button("Submit")
|
| 34 |
clear_pg = gr.Button("Clear")
|
| 35 |
|
| 36 |
+
def handle_pg_submit(drop_value, text_value):
|
| 37 |
+
final_query = drop_value if drop_value else text_value
|
| 38 |
+
if not final_query.strip():
|
| 39 |
+
return "β Please select or enter a question."
|
| 40 |
+
return query_pg(final_query)
|
| 41 |
+
|
| 42 |
+
dropdown_pg.change(fn=query_pg, inputs=dropdown_pg, outputs=output_pg)
|
| 43 |
+
submit_pg.click(handle_pg_submit, inputs=[dropdown_pg, textbox_pg], outputs=output_pg)
|
| 44 |
clear_pg.click(lambda: ("", "", ""), outputs=[dropdown_pg, textbox_pg, output_pg])
|
| 45 |
|
| 46 |
with gr.Tab("Insurance"):
|
|
|
|
| 51 |
</div>
|
| 52 |
""")
|
| 53 |
|
| 54 |
+
dropdown_ins = gr.Dropdown(label="Pick a Question", choices=[""] + insurance_questions, interactive=True)
|
| 55 |
textbox_ins = gr.Textbox(label="Ask Anything", placeholder="Type your question...", lines=2)
|
| 56 |
output_ins = gr.Textbox(label="Response", lines=10)
|
|
|
|
| 57 |
submit_ins = gr.Button("Submit")
|
| 58 |
clear_ins = gr.Button("Clear")
|
| 59 |
|
| 60 |
+
def handle_ins_submit(drop_value, text_value):
|
| 61 |
+
final_query = drop_value if drop_value else text_value
|
| 62 |
+
if not final_query.strip():
|
| 63 |
+
return "β Please select or enter a question."
|
| 64 |
+
return query_ins(final_query)
|
| 65 |
+
|
| 66 |
+
dropdown_ins.change(fn=query_ins, inputs=dropdown_ins, outputs=output_ins)
|
| 67 |
+
submit_ins.click(handle_ins_submit, inputs=[dropdown_ins, textbox_ins], outputs=output_ins)
|
| 68 |
clear_ins.click(lambda: ("", "", ""), outputs=[dropdown_ins, textbox_ins, output_ins])
|
| 69 |
|
| 70 |
demo.launch()
|
|
|
|
|
|
|
|
|