Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,28 @@
|
|
1 |
import gradio as gr
|
2 |
-
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
|
3 |
import os
|
|
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
pg_engine = pg_index.as_query_engine()
|
10 |
|
11 |
-
# Load and index Insurance documents
|
12 |
-
ins_docs =
|
13 |
-
|
14 |
-
ins_index = VectorStoreIndex.from_documents(ins_docs)
|
15 |
ins_engine = ins_index.as_query_engine()
|
16 |
|
17 |
-
# Query functions
|
18 |
def query_pg(query):
|
19 |
if not query.strip():
|
20 |
return "β Please enter a valid question before submitting."
|
@@ -31,7 +39,7 @@ def query_ins(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?",
|
@@ -48,7 +56,7 @@ insurance_questions = [
|
|
48 |
"What should you check before buying insurance?"
|
49 |
]
|
50 |
|
51 |
-
#
|
52 |
def launch_interface():
|
53 |
with gr.Blocks(
|
54 |
title="RAG App",
|
@@ -65,7 +73,7 @@ def launch_interface():
|
|
65 |
|
66 |
gr.Markdown("""
|
67 |
<div id='header-text'>
|
68 |
-
<h1>RAG Bot with LlamaIndex</h1>
|
69 |
</div>
|
70 |
""")
|
71 |
|
@@ -116,4 +124,4 @@ def launch_interface():
|
|
116 |
demo.launch()
|
117 |
|
118 |
if __name__ == "__main__":
|
119 |
-
launch_interface()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import os
|
3 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, ServiceContext
|
4 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
5 |
|
6 |
+
# β
Use Hugging Face embedding model
|
7 |
+
embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
8 |
+
service_context = ServiceContext.from_defaults(embed_model=embed_model)
|
9 |
+
|
10 |
+
# β
Helper to load and filter documents
|
11 |
+
def load_filtered_docs(folder):
|
12 |
+
docs = SimpleDirectoryReader(folder).load_data()
|
13 |
+
return [doc for doc in docs if doc.text and doc.text.strip()]
|
14 |
+
|
15 |
+
# β
Load and index Paul Graham documents
|
16 |
+
pg_docs = load_filtered_docs("data/paul")
|
17 |
+
pg_index = VectorStoreIndex.from_documents(pg_docs, service_context=service_context)
|
18 |
pg_engine = pg_index.as_query_engine()
|
19 |
|
20 |
+
# β
Load and index Insurance documents (PDF included)
|
21 |
+
ins_docs = load_filtered_docs("data/insurance")
|
22 |
+
ins_index = VectorStoreIndex.from_documents(ins_docs, service_context=service_context)
|
|
|
23 |
ins_engine = ins_index.as_query_engine()
|
24 |
|
25 |
+
# β
Query functions
|
26 |
def query_pg(query):
|
27 |
if not query.strip():
|
28 |
return "β Please enter a valid question before submitting."
|
|
|
39 |
except Exception as e:
|
40 |
return f"β Error: {str(e)}"
|
41 |
|
42 |
+
# β
Predefined questions
|
43 |
paul_questions = [
|
44 |
"What is the main purpose of writing, according to Paul Graham?",
|
45 |
"Why do students often struggle with writing in school?",
|
|
|
56 |
"What should you check before buying insurance?"
|
57 |
]
|
58 |
|
59 |
+
# β
Gradio Interface
|
60 |
def launch_interface():
|
61 |
with gr.Blocks(
|
62 |
title="RAG App",
|
|
|
73 |
|
74 |
gr.Markdown("""
|
75 |
<div id='header-text'>
|
76 |
+
<h1>RAG Bot with LlamaIndex (PDF + TXT)</h1>
|
77 |
</div>
|
78 |
""")
|
79 |
|
|
|
124 |
demo.launch()
|
125 |
|
126 |
if __name__ == "__main__":
|
127 |
+
launch_interface()
|