Poojashetty357 commited on
Commit
1703dbf
Β·
verified Β·
1 Parent(s): 20ffede

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py CHANGED
@@ -1,3 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  def launch_interface():
2
  with gr.Blocks(
3
  title="RAG App",
@@ -68,3 +132,7 @@ def launch_interface():
68
  clear_ins.click(lambda: ("", "", ""), outputs=[dropdown_ins, textbox_ins, output_ins])
69
 
70
  demo.launch()
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
4
+ from llama_index.embeddings.huggingface import HuggingFaceEmbedding
5
+
6
+
7
+ # βœ… Access OpenAI API Key
8
+ openai_api_key = os.environ.get("OPENAI_API_KEY")
9
+ if not openai_api_key:
10
+ raise ValueError("❌ OPENAI_API_KEY not found. Add it in Space settings > Secrets.")
11
+ os.environ["OPENAI_API_KEY"] = openai_api_key
12
+
13
+ # βœ… Set Hugging Face Embedding globally via Settings
14
+ embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-L6-v2")
15
+ Settings.embed_model = embed_model # Replaces deprecated ServiceContext
16
+
17
+ # βœ… Helper to load and filter documents
18
+ def load_filtered_docs(folder):
19
+ docs = SimpleDirectoryReader(folder).load_data()
20
+ return [doc for doc in docs if doc.text and doc.text.strip()]
21
+
22
+ # βœ… Load Paul Graham documents
23
+ pg_docs = load_filtered_docs("data/paul")
24
+ pg_index = VectorStoreIndex.from_documents(pg_docs)
25
+ pg_engine = pg_index.as_query_engine()
26
+
27
+ # βœ… Load Insurance documents
28
+ ins_docs = load_filtered_docs("data/insurance")
29
+ ins_index = VectorStoreIndex.from_documents(ins_docs)
30
+ ins_engine = ins_index.as_query_engine()
31
+
32
+ # βœ… Query functions
33
+ def query_pg(query):
34
+ if not query.strip():
35
+ return "❌ Please enter a valid question before submitting."
36
+ try:
37
+ return str(pg_engine.query(query))
38
+ except Exception as e:
39
+ return f"❌ Error: {str(e)}"
40
+
41
+ def query_ins(query):
42
+ if not query.strip():
43
+ return "❌ Please enter a valid question before submitting."
44
+ try:
45
+ return str(ins_engine.query(query))
46
+ except Exception as e:
47
+ return f"❌ Error: {str(e)}"
48
+
49
+ # βœ… Predefined questions
50
+ paul_questions = [
51
+ "What is the main purpose of writing, according to Paul Graham?",
52
+ "Why do students often struggle with writing in school?",
53
+ "How does Paul Graham describe the relationship between writing and thinking?",
54
+ "What is one reason Paul Graham gives for why school essays feel boring?",
55
+ "What does Paul Graham suggest writers should focus on first?"
56
+ ]
57
+
58
+ insurance_questions = [
59
+ "What is insurance and why is it important?",
60
+ "What types of insurance are common?",
61
+ "How does life insurance work?",
62
+ "What is the difference between premium and coverage?",
63
+ "What should you check before buying insurance?"
64
+ ]
65
  def launch_interface():
66
  with gr.Blocks(
67
  title="RAG App",
 
132
  clear_ins.click(lambda: ("", "", ""), outputs=[dropdown_ins, textbox_ins, output_ins])
133
 
134
  demo.launch()
135
+
136
+
137
+ if __name__ == "__main__":
138
+ launch_interface()