mayzinoo commited on
Commit
3c2b4d9
·
verified ·
1 Parent(s): cca0085

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -36
app.py CHANGED
@@ -75,28 +75,24 @@ def retrieve_and_generate_app(query, top_k=3):
75
  if not model or not index or not document_ids or not documents or not llm_pipeline:
76
  return "System not fully initialized. Please check logs for missing components."
77
 
78
- # 1. Query Embedding
79
- query_embedding = model.encode([query])
80
-
81
- # 2. Retrieval using FAISS
82
- D, I = index.search(query_embedding, top_k)
83
-
84
- retrieved_docs = []
85
- for i in I[0]:
86
- sol_id = document_ids[i]
87
- # Find the full content of the retrieved SOL
88
- # --- CHANGE THIS LINE ---
89
- # Original (incorrect): retrieved_content = next((doc["text"] for doc in documents if doc["id"] == sol_id), "Content not found.")
90
- retrieved_content = next((doc["content"] for doc in documents if doc["id"] == sol_id), "Content not found.")
91
- retrieved_docs.append({"id": sol_id, "content": retrieved_content})
92
-
93
- # 3. Context Construction
94
- # --- CHANGE THIS LINE ---
95
- # Original (incorrect): context = "\n\n".join([f"SOL {doc['id']}: {doc['text']}" for doc in retrieved_docs])
96
- context = "\n\n".join([f"SOL {doc['id']}: {doc['content']}" for doc in retrieved_docs])
97
-
98
- # 4. LLM Generation
99
- prompt = f"""
100
  Given the following information about Virginia Standards of Learning (SOLs):
101
  {context}
102
  Based on this information, answer the following question:
@@ -105,25 +101,28 @@ If the question is about a specific SOL number, provide a direct explanation for
105
  If asked for lesson plans, worksheets, or proofs, explain what the document generally entails and whether it provides such materials.
106
  Be concise and to the point.
107
  """
108
- print(f"\n--- PROMPT SENT TO LLM ---\n{prompt}\n--------------------------\n")
109
 
110
- response = llm_pipeline(prompt, max_new_tokens=500, num_return_sequences=1, do_sample=True, temperature=0.7)
111
 
112
- generated_text = response[0]['generated_text']
113
 
114
- print(f"\n--- RAW GENERATED TEXT ---\n{generated_text}\n--------------------------\n")
115
 
116
- answer_start_marker = f"Based on this information, answer the following question:\n{query}"
117
- if answer_start_marker in generated_text:
118
- answer = generated_text.split(answer_start_marker, 1)[1].strip()
119
- answer = re.sub(r'If the question is about a specific SOL number,.*?$', '', answer, flags=re.DOTALL).strip()
120
- else:
121
- answer = generated_text
122
 
123
- print(f"\n--- FINAL ANSWER ---\n{answer}\n--------------------\n")
124
- return answer
 
 
 
125
 
126
- # Create Gradio interface
127
  demo = gr.Interface(
128
  fn=retrieve_and_generate_app,
129
  inputs=gr.Textbox(lines=2, placeholder="Enter your geometry-related question here..."),
@@ -132,6 +131,5 @@ demo = gr.Interface(
132
  description="Ask questions about the Geometry SOL Instructional Guide"
133
  )
134
 
135
- # Launch the app
136
  if __name__ == "__main__":
137
  demo.launch()
 
75
  if not model or not index or not document_ids or not documents or not llm_pipeline:
76
  return "System not fully initialized. Please check logs for missing components."
77
 
78
+ try:
79
+ # 1. Query Embedding
80
+ query_embedding = model.encode([query])
81
+
82
+ # 2. Retrieval using FAISS
83
+ D, I = index.search(query_embedding, top_k)
84
+
85
+ retrieved_docs = []
86
+ for i in I[0]:
87
+ sol_id = document_ids[i]
88
+ retrieved_content = next((doc["content"] for doc in documents if doc["id"] == sol_id), "Content not found.")
89
+ retrieved_docs.append({"id": sol_id, "content": retrieved_content})
90
+
91
+ # 3. Context Construction
92
+ context = "\n\n".join([f"SOL {doc['id']}: {doc['content']}" for doc in retrieved_docs])
93
+
94
+ # 4. LLM Generation
95
+ prompt = f"""
 
 
 
 
96
  Given the following information about Virginia Standards of Learning (SOLs):
97
  {context}
98
  Based on this information, answer the following question:
 
101
  If asked for lesson plans, worksheets, or proofs, explain what the document generally entails and whether it provides such materials.
102
  Be concise and to the point.
103
  """
104
+ print(f"\n--- PROMPT SENT TO LLM ---\n{prompt}\n--------------------------\n")
105
 
106
+ response = llm_pipeline(prompt, max_new_tokens=500, num_return_sequences=1, do_sample=True, temperature=0.7)
107
 
108
+ generated_text = response[0]['generated_text']
109
 
110
+ print(f"\n--- RAW GENERATED TEXT ---\n{generated_text}\n--------------------------\n")
111
 
112
+ answer_start_marker = f"Based on this information, answer the following question:\n{query}"
113
+ if answer_start_marker in generated_text:
114
+ answer = generated_text.split(answer_start_marker, 1)[1].strip()
115
+ answer = re.sub(r'If the question is about a specific SOL number,.*?$', '', answer, flags=re.DOTALL).strip()
116
+ else:
117
+ answer = generated_text
118
 
119
+ print(f"\n--- FINAL ANSWER ---\n{answer}\n--------------------\n")
120
+ return answer if answer else "No valid response generated. Check logs for details."
121
+ except Exception as e:
122
+ print(f"\n--- ERROR ---\n{str(e)}\n------------\n")
123
+ return f"An error occurred: {str(e)}. Please check the logs for more details."
124
 
125
+ # Create Gradio interface
126
  demo = gr.Interface(
127
  fn=retrieve_and_generate_app,
128
  inputs=gr.Textbox(lines=2, placeholder="Enter your geometry-related question here..."),
 
131
  description="Ask questions about the Geometry SOL Instructional Guide"
132
  )
133
 
 
134
  if __name__ == "__main__":
135
  demo.launch()