NaimaAqeel commited on
Commit
072e16f
·
verified ·
1 Parent(s): 3064bb8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -8
app.py CHANGED
@@ -107,23 +107,23 @@ def upload_document(file):
107
 
108
  return "Document uploaded and indexed successfully."
109
 
 
110
  # ===============================
111
- # GENERATION PIPELINE
112
  # ===============================
113
- query_vector = get_embeddings(query, is_query=True).astype("float32")
 
114
 
115
  def generate_answer_from_file(query, top_k=10):
116
  if not document_texts:
117
  return "No documents indexed yet."
118
 
119
- query_vector = get_embeddings(query).astype("float32")
120
  scores, indices = index.search(query_vector, k=top_k)
121
  retrieved_chunks = [document_texts[i] for i in indices[0]]
122
  context = "\n\n".join(retrieved_chunks)
123
 
124
- print("\n--- Retrieved Context ---\n", context) # Debugging print
125
-
126
- # Prompt Engineering
127
  prompt = (
128
  "You are a helpful assistant reading student notes or textbook passages.\n\n"
129
  "Based on the context provided, answer the question accurately and clearly.\n\n"
@@ -140,6 +140,7 @@ def generate_answer_from_file(query, top_k=10):
140
  result = qa_pipeline(prompt, max_length=512, do_sample=False)[0]['generated_text']
141
  return result.strip()
142
 
 
143
  # ===============================
144
  # GRADIO INTERFACES
145
  # ===============================
@@ -161,5 +162,3 @@ search_interface = gr.Interface(
161
 
162
  app = gr.TabbedInterface([upload_interface, search_interface], ["Upload", "Ask"])
163
  app.launch()
164
-
165
-
 
107
 
108
  return "Document uploaded and indexed successfully."
109
 
110
+
111
  # ===============================
112
+ # QA GENERATION PIPELINE
113
  # ===============================
114
+ # Initialize text generation pipeline (you can use a more powerful model if needed)
115
+ qa_pipeline = pipeline("text-generation", model="gpt2")
116
 
117
  def generate_answer_from_file(query, top_k=10):
118
  if not document_texts:
119
  return "No documents indexed yet."
120
 
121
+ query_vector = get_embeddings(query, is_query=True).astype("float32")
122
  scores, indices = index.search(query_vector, k=top_k)
123
  retrieved_chunks = [document_texts[i] for i in indices[0]]
124
  context = "\n\n".join(retrieved_chunks)
125
 
126
+ # Prompt for the model
 
 
127
  prompt = (
128
  "You are a helpful assistant reading student notes or textbook passages.\n\n"
129
  "Based on the context provided, answer the question accurately and clearly.\n\n"
 
140
  result = qa_pipeline(prompt, max_length=512, do_sample=False)[0]['generated_text']
141
  return result.strip()
142
 
143
+
144
  # ===============================
145
  # GRADIO INTERFACES
146
  # ===============================
 
162
 
163
  app = gr.TabbedInterface([upload_interface, search_interface], ["Upload", "Ask"])
164
  app.launch()