Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Load and index Paul Graham documents
|
| 6 |
+
pg_docs = SimpleDirectoryReader("data/paul").load_data()
|
| 7 |
+
pg_docs = [doc for doc in pg_docs if doc.text and doc.text.strip()]
|
| 8 |
+
pg_index = VectorStoreIndex.from_documents(pg_docs)
|
| 9 |
+
pg_engine = pg_index.as_query_engine()
|
| 10 |
+
|
| 11 |
+
# Load and index Insurance documents
|
| 12 |
+
ins_docs = SimpleDirectoryReader("data/insurance").load_data()
|
| 13 |
+
ins_docs = [doc for doc in ins_docs if doc.text and doc.text.strip()]
|
| 14 |
+
ins_index = VectorStoreIndex.from_documents(ins_docs)
|
| 15 |
+
ins_engine = ins_index.as_query_engine()
|
| 16 |
+
|
| 17 |
+
# Query functions with input validation
|
| 18 |
+
def query_pg(query):
|
| 19 |
+
if not query.strip():
|
| 20 |
+
return "❌ Please enter a valid question before submitting."
|
| 21 |
+
try:
|
| 22 |
+
return str(pg_engine.query(query))
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return f"❌ Error: {str(e)}"
|
| 25 |
+
|
| 26 |
+
def query_ins(query):
|
| 27 |
+
if not query.strip():
|
| 28 |
+
return "❌ Please enter a valid question before submitting."
|
| 29 |
+
try:
|
| 30 |
+
return str(ins_engine.query(query))
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return f"❌ Error: {str(e)}"
|
| 33 |
+
|
| 34 |
+
# Predefined questions
|
| 35 |
+
paul_questions = [
|
| 36 |
+
"What is the main purpose of writing, according to Paul Graham?",
|
| 37 |
+
"Why do students often struggle with writing in school?",
|
| 38 |
+
"How does Paul Graham describe the relationship between writing and thinking?",
|
| 39 |
+
"What is one reason Paul Graham gives for why school essays feel boring?",
|
| 40 |
+
"What does Paul Graham suggest writers should focus on first?"
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
insurance_questions = [
|
| 44 |
+
"What is insurance and why is it important?",
|
| 45 |
+
"What types of insurance are common?",
|
| 46 |
+
"How does life insurance work?",
|
| 47 |
+
"What is the difference between premium and coverage?",
|
| 48 |
+
"What should you check before buying insurance?"
|
| 49 |
+
]
|
| 50 |
+
|
| 51 |
+
# UI
|
| 52 |
+
def launch_interface():
|
| 53 |
+
with gr.Blocks(
|
| 54 |
+
title="RAG App",
|
| 55 |
+
css="""
|
| 56 |
+
.gradio-container {
|
| 57 |
+
background-color: #e6fff7 !important;
|
| 58 |
+
}
|
| 59 |
+
#header-text {
|
| 60 |
+
text-align: center;
|
| 61 |
+
color: #2b6777;
|
| 62 |
+
}
|
| 63 |
+
"""
|
| 64 |
+
) as demo:
|
| 65 |
+
|
| 66 |
+
gr.Markdown("""
|
| 67 |
+
<div id='header-text'>
|
| 68 |
+
<h1>RAG Bot with LlamaIndex</h1>
|
| 69 |
+
</div>
|
| 70 |
+
""")
|
| 71 |
+
|
| 72 |
+
with gr.Tabs():
|
| 73 |
+
# Paul Graham Tab
|
| 74 |
+
with gr.Tab("Paul Graham"):
|
| 75 |
+
if os.path.exists("data/logo.png"):
|
| 76 |
+
gr.Image("data/logo.png", show_label=False, container=False, height=120)
|
| 77 |
+
|
| 78 |
+
gr.Markdown("""
|
| 79 |
+
<div id='header-text'>
|
| 80 |
+
<h2>Paul Graham: Writing and Thinking</h2>
|
| 81 |
+
<p>Select a question or ask your own based on the essay.</p>
|
| 82 |
+
</div>
|
| 83 |
+
""")
|
| 84 |
+
|
| 85 |
+
dropdown_pg = gr.Dropdown(label="Pick a Question", choices=paul_questions, interactive=True)
|
| 86 |
+
textbox_pg = gr.Textbox(label="Ask Anything", placeholder="Type your question...", lines=2)
|
| 87 |
+
output_pg = gr.Textbox(label="Response", lines=10)
|
| 88 |
+
|
| 89 |
+
submit_pg = gr.Button("Submit")
|
| 90 |
+
clear_pg = gr.Button("Clear")
|
| 91 |
+
|
| 92 |
+
dropdown_pg.change(query_pg, inputs=dropdown_pg, outputs=output_pg)
|
| 93 |
+
submit_pg.click(query_pg, inputs=textbox_pg, outputs=output_pg)
|
| 94 |
+
clear_pg.click(lambda: ("", "", ""), outputs=[dropdown_pg, textbox_pg, output_pg])
|
| 95 |
+
|
| 96 |
+
# Insurance Tab
|
| 97 |
+
with gr.Tab("Insurance"):
|
| 98 |
+
gr.Markdown("""
|
| 99 |
+
<div id='header-text'>
|
| 100 |
+
<h2>Understanding Insurance</h2>
|
| 101 |
+
<p>Explore key insurance concepts or ask your own questions.</p>
|
| 102 |
+
</div>
|
| 103 |
+
""")
|
| 104 |
+
|
| 105 |
+
dropdown_ins = gr.Dropdown(label="Pick a Question", choices=insurance_questions, interactive=True)
|
| 106 |
+
textbox_ins = gr.Textbox(label="Ask Anything", placeholder="Type your question...", lines=2)
|
| 107 |
+
output_ins = gr.Textbox(label="Response", lines=10)
|
| 108 |
+
|
| 109 |
+
submit_ins = gr.Button("Submit")
|
| 110 |
+
clear_ins = gr.Button("Clear")
|
| 111 |
+
|
| 112 |
+
dropdown_ins.change(query_ins, inputs=dropdown_ins, outputs=output_ins)
|
| 113 |
+
submit_ins.click(query_ins, inputs=textbox_ins, outputs=output_ins)
|
| 114 |
+
clear_ins.click(lambda: ("", "", ""), outputs=[dropdown_ins, textbox_ins, output_ins])
|
| 115 |
+
|
| 116 |
+
demo.launch()
|
| 117 |
+
|
| 118 |
+
if __name__ == "__main__":
|
| 119 |
+
launch_interface()
|