Jitendra14355 commited on
Commit
9f43cdc
·
verified ·
1 Parent(s): 23e5b28

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -223
app.py CHANGED
@@ -1,28 +1,39 @@
1
-
2
  # =========================================
3
- # RAG QnA System (ACCURATE - BERT QA)
4
  # =========================================
5
 
6
-
7
  import gradio as gr
8
  import numpy as np
9
  import faiss
 
10
 
11
  from sentence_transformers import SentenceTransformer
12
- from transformers import pipeline
 
13
 
14
  # -----------------------------
15
- # 1. Load Documents
16
  # -----------------------------
17
  def load_documents(file_path):
18
- with open(file_path, "r", encoding="utf-8") as f:
19
- text = f.read()
 
 
 
 
 
 
 
 
20
  return text.split("\n\n")
21
 
22
 
23
  def chunk_text(text, chunk_size=120):
24
  words = text.split()
25
- return [" ".join(words[i:i+chunk_size]) for i in range(0, len(words), chunk_size)]
 
 
 
26
 
27
 
28
  documents = load_documents("data/data.txt")
@@ -43,15 +54,15 @@ index = faiss.IndexFlatL2(dimension)
43
  index.add(np.array(embeddings))
44
 
45
  # -----------------------------
46
- # 3. Load QA Model (BERT)
47
  # -----------------------------
48
- qa_pipeline = pipeline(
49
- "question-answering",
50
- model="distilbert-base-cased-distilled-squad"
51
- )
52
 
53
  # -----------------------------
54
- # 4. RAG Function
55
  # -----------------------------
56
  def rag_query(query):
57
  if not query.strip():
@@ -64,19 +75,30 @@ def rag_query(query):
64
  retrieved_docs = [all_chunks[i] for i in I[0]]
65
  context = " ".join(retrieved_docs)
66
 
67
- # Extract exact answer
68
- result = qa_pipeline(
69
- question=query,
70
- context=context
71
- )
72
 
73
- answer = result["answer"]
 
74
 
75
- # If low confidence → avoid hallucination
76
- if result["score"] < 0.2:
77
- answer = "Not found in document"
78
 
79
- return f"Answer:\n{answer}\n\nConfidence: {result['score']:.2f}\n\nContext:\n{context}"
 
 
 
 
 
 
 
 
 
 
 
80
 
81
  # -----------------------------
82
  # 5. Gradio UI
@@ -85,208 +107,12 @@ iface = gr.Interface(
85
  fn=rag_query,
86
  inputs=gr.Textbox(lines=2, placeholder="Ask your question..."),
87
  outputs="text",
88
- title="📚 RAG QnA System (Accurate)",
89
- description="Retriever + BERT QA No hallucination"
90
  )
91
 
92
  # -----------------------------
93
  # 6. Launch
94
  # -----------------------------
95
  if __name__ == "__main__":
96
- iface.launch()
97
-
98
-
99
- # # =========================================
100
- # # RAG QnA System (ACCURATE - BERT QA)
101
- # # =========================================
102
-
103
- # import gradio as gr
104
- # import numpy as np
105
- # import faiss
106
-
107
- # from sentence_transformers import SentenceTransformer
108
- # from transformers import pipeline
109
-
110
- # # -----------------------------
111
- # # 1. Load Documents
112
- # # -----------------------------
113
- # def load_documents(file_path):
114
- # with open(file_path, "r", encoding="utf-8") as f:
115
- # text = f.read()
116
- # return text.split("\n\n")
117
-
118
-
119
- # def chunk_text(text, chunk_size=120):
120
- # words = text.split()
121
- # return [" ".join(words[i:i+chunk_size]) for i in range(0, len(words), chunk_size)]
122
-
123
-
124
- # documents = load_documents("data/data.txt")
125
-
126
- # all_chunks = []
127
- # for doc in documents:
128
- # all_chunks.extend(chunk_text(doc))
129
-
130
- # # -----------------------------
131
- # # 2. Embeddings + FAISS
132
- # # -----------------------------
133
- # embedder = SentenceTransformer("all-MiniLM-L6-v2")
134
-
135
- # embeddings = embedder.encode(all_chunks)
136
-
137
- # dimension = embeddings.shape[1]
138
- # index = faiss.IndexFlatL2(dimension)
139
- # index.add(np.array(embeddings))
140
-
141
- # # -----------------------------
142
- # # 3. Load QA Model (BERT)
143
- # # -----------------------------
144
- # qa_pipeline = pipeline(
145
- # "question-answering",
146
- # model="distilbert-base-cased-distilled-squad"
147
- # )
148
-
149
- # # -----------------------------
150
- # # 4. RAG Function
151
- # # -----------------------------
152
- # def rag_query(query):
153
- # if not query.strip():
154
- # return "Please enter a question."
155
-
156
- # # Retrieve relevant chunks
157
- # query_embedding = embedder.encode([query])
158
- # D, I = index.search(np.array(query_embedding), k=5)
159
-
160
- # retrieved_docs = [all_chunks[i] for i in I[0]]
161
- # context = " ".join(retrieved_docs)
162
-
163
- # # Extract exact answer
164
- # result = qa_pipeline(
165
- # question=query,
166
- # context=context
167
- # )
168
-
169
- # answer = result["answer"]
170
-
171
- # # If low confidence → avoid hallucination
172
- # if result["score"] < 0.2:
173
- # answer = "Not found in document"
174
-
175
- # return f"Answer:\n{answer}\n\nConfidence: {result['score']:.2f}\n\nContext:\n{context}"
176
-
177
- # # -----------------------------
178
- # # 5. Gradio UI
179
- # # -----------------------------
180
- # iface = gr.Interface(
181
- # fn=rag_query,
182
- # inputs=gr.Textbox(lines=2, placeholder="Ask your question..."),
183
- # outputs="text",
184
- # title="📚 RAG QnA System (Accurate)",
185
- # description="Retriever + BERT QA → No hallucination"
186
- # )
187
-
188
- # # -----------------------------
189
- # # 6. Launch
190
- # # -----------------------------
191
- # if __name__ == "__main__":
192
- # iface.launch()
193
-
194
-
195
- # #import gradio as gr
196
- # # import numpy as np
197
- # # import faiss
198
-
199
- # # from sentence_transformers import SentenceTransformer
200
- # # from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
201
- # # import torch
202
-
203
- # # # -----------------------------
204
- # # # 1. Load Documents
205
- # # # -----------------------------
206
- # # def load_documents(file_path):
207
- # # with open(file_path, "r", encoding="utf-8") as f:
208
- # # text = f.read()
209
- # # return text.split("\n\n")
210
-
211
-
212
- # # def chunk_text(text, chunk_size=100):
213
- # # words = text.split()
214
- # # return [" ".join(words[i:i+chunk_size]) for i in range(0, len(words), chunk_size)]
215
-
216
-
217
- # # documents = load_documents("data/data.txt")
218
-
219
- # # all_chunks = []
220
- # # for doc in documents:
221
- # # all_chunks.extend(chunk_text(doc))
222
-
223
- # # # -----------------------------
224
- # # # 2. Embeddings + FAISS
225
- # # # -----------------------------
226
- # # embedder = SentenceTransformer("all-MiniLM-L6-v2")
227
-
228
- # # embeddings = embedder.encode(all_chunks)
229
-
230
- # # dimension = embeddings.shape[1]
231
- # # index = faiss.IndexFlatL2(dimension)
232
- # # index.add(np.array(embeddings))
233
-
234
- # # # -----------------------------
235
- # # # 3. Load Model (FIXED)
236
- # # # -----------------------------
237
- # # model_name = "google/flan-t5-small"
238
-
239
- # # tokenizer = AutoTokenizer.from_pretrained(model_name)
240
- # # model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
241
-
242
- # # # -----------------------------
243
- # # # 4. RAG Function
244
- # # # -----------------------------
245
- # # def rag_query(query):
246
- # # if not query.strip():
247
- # # return "Please enter a question."
248
-
249
- # # query_embedding = embedder.encode([query])
250
-
251
- # # D, I = index.search(np.array(query_embedding), k=3)
252
- # # retrieved_docs = [all_chunks[i] for i in I[0]]
253
-
254
- # # context = " ".join(retrieved_docs)
255
-
256
- # # input_text = f"""
257
- # # Answer the question based only on the context below.
258
-
259
- # # Context: {context}
260
-
261
- # # Question: {query}
262
- # # """
263
-
264
- # # inputs = tokenizer(input_text, return_tensors="pt", truncation=True)
265
-
266
- # # outputs = model.generate(
267
- # # **inputs,
268
- # # max_length=120,
269
- # # do_sample=True,
270
- # # temperature=0.7
271
- # # )
272
-
273
- # # answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
274
-
275
- # # return f"Answer:\n{answer}\n\nContext:\n{context}"
276
-
277
- # # # -----------------------------
278
- # # # 5. Gradio UI
279
- # # # -----------------------------
280
- # # iface = gr.Interface(
281
- # # fn=rag_query,
282
- # # inputs=gr.Textbox(lines=2, placeholder="Ask your question..."),
283
- # # outputs="text",
284
- # # title="📚 RAG QnA System",
285
- # # description="Ask questions based on your document"
286
- # # )
287
-
288
- # # # -----------------------------
289
- # # # 6. Launch
290
- # # # -----------------------------
291
- # # if __name__ == "__main__":
292
- # # iface.launch()
 
 
1
  # =========================================
2
+ # RAG QnA System (FIXED FOR HF SPACES)
3
  # =========================================
4
 
 
5
  import gradio as gr
6
  import numpy as np
7
  import faiss
8
+ import os
9
 
10
  from sentence_transformers import SentenceTransformer
11
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
12
+ import torch
13
 
14
  # -----------------------------
15
+ # 1. Load Documents (FIXED)
16
  # -----------------------------
17
  def load_documents(file_path):
18
+ if not os.path.exists(file_path):
19
+ return ["No document found."]
20
+
21
+ try:
22
+ with open(file_path, "r", encoding="utf-8") as f:
23
+ text = f.read()
24
+ except:
25
+ with open(file_path, "r", encoding="latin-1") as f:
26
+ text = f.read()
27
+
28
  return text.split("\n\n")
29
 
30
 
31
  def chunk_text(text, chunk_size=120):
32
  words = text.split()
33
+ return [
34
+ " ".join(words[i:i+chunk_size])
35
+ for i in range(0, len(words), chunk_size)
36
+ ]
37
 
38
 
39
  documents = load_documents("data/data.txt")
 
54
  index.add(np.array(embeddings))
55
 
56
  # -----------------------------
57
+ # 3. GENERATIVE MODEL (FIXED)
58
  # -----------------------------
59
+ model_name = "google/flan-t5-base"
60
+
61
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
62
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
63
 
64
  # -----------------------------
65
+ # 4. RAG Function (FIXED)
66
  # -----------------------------
67
  def rag_query(query):
68
  if not query.strip():
 
75
  retrieved_docs = [all_chunks[i] for i in I[0]]
76
  context = " ".join(retrieved_docs)
77
 
78
+ # Prompt for model
79
+ prompt = f"""
80
+ Answer the question ONLY using the context below.
81
+ If the answer is not present, say "Not found in document".
 
82
 
83
+ Context:
84
+ {context}
85
 
86
+ Question:
87
+ {query}
88
+ """
89
 
90
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
91
+
92
+ outputs = model.generate(
93
+ **inputs,
94
+ max_new_tokens=150,
95
+ do_sample=True,
96
+ temperature=0.7
97
+ )
98
+
99
+ answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
100
+
101
+ return f"Answer:\n{answer}\n\nContext:\n{context}"
102
 
103
  # -----------------------------
104
  # 5. Gradio UI
 
107
  fn=rag_query,
108
  inputs=gr.Textbox(lines=2, placeholder="Ask your question..."),
109
  outputs="text",
110
+ title="📚 RAG QnA System (Fixed)",
111
+ description="Retriever + FLAN-T5 (Works on Hugging Face Spaces)"
112
  )
113
 
114
  # -----------------------------
115
  # 6. Launch
116
  # -----------------------------
117
  if __name__ == "__main__":
118
+ iface.launch()