Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from pathlib import Path
|
4 |
+
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
|
5 |
+
|
6 |
+
# Ensure data directory exists
|
7 |
+
Path("data").mkdir(exist_ok=True)
|
8 |
+
|
9 |
+
# Set OpenAI API key
|
10 |
+
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
11 |
+
|
12 |
+
# Global state for engines
|
13 |
+
state = {"insurance_engine": None, "paul_engine": None, "custom_engine": None}
|
14 |
+
|
15 |
+
# Build index from file
|
16 |
+
def build_index_from_file(file_path):
|
17 |
+
documents = SimpleDirectoryReader(input_files=[file_path]).load_data()
|
18 |
+
index = VectorStoreIndex.from_documents(documents)
|
19 |
+
return index.as_query_engine()
|
20 |
+
|
21 |
+
# Preload fixed documents
|
22 |
+
def preload_indexes():
|
23 |
+
try:
|
24 |
+
state["insurance_engine"] = build_index_from_file("data/insurance_FirstRAG.pdf")
|
25 |
+
except:
|
26 |
+
print("β Failed to load insurance.pdf")
|
27 |
+
|
28 |
+
try:
|
29 |
+
state["paul_engine"] = build_index_from_file("data/paul_graham_FirstRAG.txt")
|
30 |
+
except:
|
31 |
+
print("β Failed to load paul_graham.txt")
|
32 |
+
|
33 |
+
# Upload custom file
|
34 |
+
def upload_and_refresh(file_path):
|
35 |
+
if file_path is None:
|
36 |
+
return "β οΈ No file uploaded."
|
37 |
+
try:
|
38 |
+
state["custom_engine"] = build_index_from_file(file_path)
|
39 |
+
return f"β
Uploaded & Indexed: {os.path.basename(file_path)}"
|
40 |
+
except Exception as e:
|
41 |
+
return f"β Failed: {str(e)}"
|
42 |
+
|
43 |
+
# Query helpers
|
44 |
+
def ask_insurance(query): return _query_engine(state["insurance_engine"], query)
|
45 |
+
def ask_paul(query): return _query_engine(state["paul_engine"], query)
|
46 |
+
def ask_custom(query): return _query_engine(state["custom_engine"], query)
|
47 |
+
|
48 |
+
def _query_engine(engine, query):
|
49 |
+
if not query:
|
50 |
+
return "β Please enter a question."
|
51 |
+
if engine is None:
|
52 |
+
return "π Document not loaded yet."
|
53 |
+
try:
|
54 |
+
return str(engine.query(query))
|
55 |
+
except Exception as e:
|
56 |
+
return f"β Error: {str(e)}"
|
57 |
+
|
58 |
+
# Summarize
|
59 |
+
def summarize_insurance(): return _query_engine(state["insurance_engine"], "Summarize the insurance document.")
|
60 |
+
def summarize_paul(): return _query_engine(state["paul_engine"], "Summarize Paul Graham's main ideas.")
|
61 |
+
def summarize_custom(): return _query_engine(state["custom_engine"], "Summarize the uploaded documents.")
|
62 |
+
|
63 |
+
# Clear
|
64 |
+
def clear_fields(): return "", ""
|
65 |
+
|
66 |
+
# Launch app
|
67 |
+
def launch():
|
68 |
+
preload_indexes()
|
69 |
+
|
70 |
+
with gr.Blocks(
|
71 |
+
title="RAG App with LlamaIndex",
|
72 |
+
css="""
|
73 |
+
body {
|
74 |
+
background-color: #f5f5dc;
|
75 |
+
font-family: 'Georgia', 'Merriweather', serif;
|
76 |
+
}
|
77 |
+
h1 {
|
78 |
+
font-size: 2.5em;
|
79 |
+
font-weight: bold;
|
80 |
+
color: #4e342e;
|
81 |
+
margin-bottom: 0.3em;
|
82 |
+
}
|
83 |
+
.subtitle {
|
84 |
+
font-size: 1.2em;
|
85 |
+
color: #6d4c41;
|
86 |
+
margin-bottom: 1.5em;
|
87 |
+
}
|
88 |
+
.gr-box, .gr-column, .gr-group {
|
89 |
+
border-radius: 15px;
|
90 |
+
padding: 20px;
|
91 |
+
background-color: #fffaf0;
|
92 |
+
box-shadow: 2px 4px 14px rgba(0, 0, 0, 0.1);
|
93 |
+
margin-top: 10px;
|
94 |
+
}
|
95 |
+
textarea, input[type="text"], input[type="file"] {
|
96 |
+
background-color: #fffaf0;
|
97 |
+
border: 1px solid #d2b48c;
|
98 |
+
color: #4e342e;
|
99 |
+
border-radius: 8px;
|
100 |
+
}
|
101 |
+
button {
|
102 |
+
background-color: #a1887f;
|
103 |
+
color: white;
|
104 |
+
font-weight: bold;
|
105 |
+
border-radius: 8px;
|
106 |
+
transition: background-color 0.3s ease;
|
107 |
+
}
|
108 |
+
button:hover {
|
109 |
+
background-color: #8d6e63;
|
110 |
+
}
|
111 |
+
.gr-button {
|
112 |
+
border-radius: 8px !important;
|
113 |
+
}
|
114 |
+
.tabitem {
|
115 |
+
border-radius: 15px !important;
|
116 |
+
}
|
117 |
+
"""
|
118 |
+
) as app:
|
119 |
+
|
120 |
+
with gr.Column():
|
121 |
+
gr.Markdown("""
|
122 |
+
<div style='text-align: center;'>
|
123 |
+
<h1>RAG Application with LlamaIndex</h1>
|
124 |
+
<div class='subtitle'>π Ask questions from documents using Retrieval-Augmented Generation (RAG)</div>
|
125 |
+
</div>
|
126 |
+
""")
|
127 |
+
|
128 |
+
# π Insurance Tab
|
129 |
+
with gr.Tab("π Insurance Summary"):
|
130 |
+
with gr.Column():
|
131 |
+
gr.Markdown("### π‘οΈ Ask about Insurance Document")
|
132 |
+
with gr.Group():
|
133 |
+
insurance_q = gr.Textbox(label="Your Question", placeholder="e.g., What does this cover?")
|
134 |
+
with gr.Row():
|
135 |
+
insurance_ask = gr.Button("Submit")
|
136 |
+
insurance_clear = gr.Button("Clear")
|
137 |
+
insurance_ans = gr.Textbox(label="Response", lines=6)
|
138 |
+
insurance_ask.click(fn=ask_insurance, inputs=insurance_q, outputs=insurance_ans)
|
139 |
+
insurance_clear.click(fn=clear_fields, outputs=[insurance_q, insurance_ans])
|
140 |
+
|
141 |
+
with gr.Group():
|
142 |
+
summarize_btn = gr.Button("Summarize Insurance Document")
|
143 |
+
summarize_output = gr.Textbox(label="Summary", lines=6)
|
144 |
+
summarize_btn.click(fn=summarize_insurance, outputs=summarize_output)
|
145 |
+
|
146 |
+
# π§ Paul Graham Tab
|
147 |
+
with gr.Tab("π§ Paul Graham"):
|
148 |
+
with gr.Column():
|
149 |
+
gr.Markdown("### π§ Ask about Paul Graham's Writings")
|
150 |
+
with gr.Group():
|
151 |
+
paul_q = gr.Textbox(label="Your Question", placeholder="e.g., What does Paul say about startups?")
|
152 |
+
with gr.Row():
|
153 |
+
paul_ask = gr.Button("Ask")
|
154 |
+
paul_clear = gr.Button("Clear")
|
155 |
+
paul_ans = gr.Textbox(label="Response", lines=6)
|
156 |
+
paul_ask.click(fn=ask_paul, inputs=paul_q, outputs=paul_ans)
|
157 |
+
paul_clear.click(fn=clear_fields, outputs=[paul_q, paul_ans])
|
158 |
+
|
159 |
+
with gr.Group():
|
160 |
+
summarize_btn2 = gr.Button("Summarize Paul Graham's Ideas")
|
161 |
+
summarize_output2 = gr.Textbox(label="Summary", lines=6)
|
162 |
+
summarize_btn2.click(fn=summarize_paul, outputs=summarize_output2)
|
163 |
+
|
164 |
+
# π€ Upload Custom
|
165 |
+
with gr.Tab("π€ Upload & Ask Your File"):
|
166 |
+
with gr.Column():
|
167 |
+
gr.Markdown("### π€ Upload Your Own Document")
|
168 |
+
with gr.Group():
|
169 |
+
file_input = gr.File(label="Upload PDF or TXT", type="filepath")
|
170 |
+
status = gr.Textbox(label="Status", interactive=False)
|
171 |
+
file_input.change(fn=upload_and_refresh, inputs=file_input, outputs=status)
|
172 |
+
|
173 |
+
with gr.Group():
|
174 |
+
gr.Markdown("#### π¬ Ask a Question")
|
175 |
+
custom_q = gr.Textbox(label="Your Query")
|
176 |
+
with gr.Row():
|
177 |
+
custom_ask = gr.Button("Ask")
|
178 |
+
custom_clear = gr.Button("Clear")
|
179 |
+
custom_ans = gr.Textbox(label="Response", lines=6)
|
180 |
+
custom_ask.click(fn=ask_custom, inputs=custom_q, outputs=custom_ans)
|
181 |
+
custom_clear.click(fn=clear_fields, outputs=[custom_q, custom_ans])
|
182 |
+
|
183 |
+
with gr.Group():
|
184 |
+
gr.Markdown("#### π Summarize the File")
|
185 |
+
summarize = gr.Button("Summarize Uploaded Document")
|
186 |
+
summary_output = gr.Textbox(label="Summary", lines=6)
|
187 |
+
summarize.click(fn=summarize_custom, outputs=summary_output)
|
188 |
+
|
189 |
+
app.launch()
|
190 |
+
|
191 |
+
|
192 |
+
# Run the app
|
193 |
+
launch()
|