Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -57,55 +57,39 @@ class MathAnswer(dspy.Signature):
|
|
| 57 |
answer = dspy.OutputField()
|
| 58 |
|
| 59 |
# === DSPy Programs ===
|
| 60 |
-
# === DSPy Programs with Output Guard ===
|
| 61 |
class MathRetrievalQA(dspy.Program):
|
| 62 |
def forward(self, question):
|
| 63 |
print("Inside MathRetrievalQA...")
|
| 64 |
context_items = retrieve_from_qdrant(question)
|
| 65 |
context = "\n".join([item["solution"] for item in context_items if "solution" in item])
|
| 66 |
print("Context for generation:", context)
|
| 67 |
-
|
| 68 |
if not context:
|
| 69 |
-
return
|
| 70 |
-
|
| 71 |
-
# === Replace below with real model call when ready ===
|
| 72 |
prompt = f"Question: {question}\nContext: {context}\nAnswer:"
|
| 73 |
-
print("
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
print("Generated answer:", generated_answer)
|
| 78 |
-
|
| 79 |
-
# === Output Guard ===
|
| 80 |
-
if not generated_answer or len(generated_answer.strip()) < 10 or "I don't know" in generated_answer:
|
| 81 |
-
return {"answer": "", "retrieved_context": context}
|
| 82 |
-
|
| 83 |
-
return {"answer": generated_answer.strip(), "retrieved_context": context}
|
| 84 |
|
|
|
|
| 85 |
|
| 86 |
class WebFallbackQA(dspy.Program):
|
| 87 |
def forward(self, question):
|
| 88 |
print("Fallback to Tavily...")
|
| 89 |
answer = web_search_tavily(question)
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
return {"answer": answer.strip(), "retrieved_context": "Tavily"}
|
| 93 |
|
| 94 |
|
| 95 |
class MathRouter(dspy.Program):
|
| 96 |
def forward(self, question):
|
| 97 |
print("Routing question:", question)
|
| 98 |
if not is_valid_math_question(question):
|
| 99 |
-
return
|
| 100 |
-
|
| 101 |
result = MathRetrievalQA().forward(question)
|
| 102 |
-
|
| 103 |
-
if result["answer"]
|
| 104 |
-
|
| 105 |
-
else:
|
| 106 |
-
return WebFallbackQA().forward(question)
|
| 107 |
-
|
| 108 |
-
|
| 109 |
|
| 110 |
# === Feedback Storage ===
|
| 111 |
def store_feedback(question, answer, feedback, correct_answer):
|
|
@@ -120,26 +104,14 @@ def store_feedback(question, answer, feedback, correct_answer):
|
|
| 120 |
with open("feedback.json", "a") as f:
|
| 121 |
f.write(json.dumps(entry) + "\n")
|
| 122 |
|
| 123 |
-
def load_feedback_entries():
|
| 124 |
-
entries = []
|
| 125 |
-
try:
|
| 126 |
-
with open("feedback.json", "r") as f:
|
| 127 |
-
for line in f:
|
| 128 |
-
entry = json.loads(line)
|
| 129 |
-
entries.append(entry)
|
| 130 |
-
except FileNotFoundError:
|
| 131 |
-
pass
|
| 132 |
-
return entries
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
# === Gradio Functions ===
|
| 136 |
# === Gradio Functions ===
|
| 137 |
def ask_question(question):
|
| 138 |
print("ask_question() called with:", question)
|
| 139 |
result = router.forward(question)
|
| 140 |
print("Result:", result)
|
| 141 |
-
return result
|
| 142 |
-
|
|
|
|
| 143 |
|
| 144 |
|
| 145 |
def submit_feedback(question, model_answer, feedback, correct_answer):
|
|
@@ -170,29 +142,8 @@ with gr.Blocks() as demo:
|
|
| 170 |
fb_correct = gr.Textbox(label="Correct Answer (optional)")
|
| 171 |
fb_submit_btn = gr.Button("Submit Feedback")
|
| 172 |
fb_status = gr.Textbox(label="Status", interactive=False)
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
def feedback_submission_and_display(question, answer, feedback, correct_answer):
|
| 178 |
-
store_feedback(question, answer, feedback, correct_answer)
|
| 179 |
-
entries = load_feedback_entries()
|
| 180 |
-
display_rows = [[
|
| 181 |
-
e["question"],
|
| 182 |
-
e["model_answer"],
|
| 183 |
-
e["feedback"],
|
| 184 |
-
e["correct_answer"],
|
| 185 |
-
e["timestamp"]
|
| 186 |
-
] for e in entries]
|
| 187 |
-
return "β
Feedback received. Thank you!", display_rows
|
| 188 |
-
|
| 189 |
-
fb_submit_btn.click(
|
| 190 |
-
fn=feedback_submission_and_display,
|
| 191 |
-
inputs=[fb_question, fb_answer, fb_like, fb_correct],
|
| 192 |
-
outputs=[fb_status, feedback_display]
|
| 193 |
-
)
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
|
| 198 |
demo.launch(share=True, debug=True)
|
|
|
|
| 57 |
answer = dspy.OutputField()
|
| 58 |
|
| 59 |
# === DSPy Programs ===
|
|
|
|
| 60 |
class MathRetrievalQA(dspy.Program):
|
| 61 |
def forward(self, question):
|
| 62 |
print("Inside MathRetrievalQA...")
|
| 63 |
context_items = retrieve_from_qdrant(question)
|
| 64 |
context = "\n".join([item["solution"] for item in context_items if "solution" in item])
|
| 65 |
print("Context for generation:", context)
|
|
|
|
| 66 |
if not context:
|
| 67 |
+
return dspy.Output(answer="", retrieved_context="")
|
|
|
|
|
|
|
| 68 |
prompt = f"Question: {question}\nContext: {context}\nAnswer:"
|
| 69 |
+
print("Generating answer...")
|
| 70 |
+
# answer = qa_pipeline(prompt, max_new_tokens=100)[0]["generated_text"]
|
| 71 |
+
print("Generated answer:", prompt)
|
| 72 |
+
return {"answer": prompt, "retrieved_context": context}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
# return dspy.Output(answer=answer, retrieved_context=context)
|
| 75 |
|
| 76 |
class WebFallbackQA(dspy.Program):
|
| 77 |
def forward(self, question):
|
| 78 |
print("Fallback to Tavily...")
|
| 79 |
answer = web_search_tavily(question)
|
| 80 |
+
# return dspy.Output(answer=answer, retrieved_context="Tavily")
|
| 81 |
+
return {"answer": answer, "retrieved_context": "Tavily"}
|
|
|
|
| 82 |
|
| 83 |
|
| 84 |
class MathRouter(dspy.Program):
|
| 85 |
def forward(self, question):
|
| 86 |
print("Routing question:", question)
|
| 87 |
if not is_valid_math_question(question):
|
| 88 |
+
return dspy.Output(answer="β Only math questions are accepted. Please rephrase.", retrieved_context="")
|
|
|
|
| 89 |
result = MathRetrievalQA().forward(question)
|
| 90 |
+
#return result if result.answer else WebFallbackQA().forward(question)
|
| 91 |
+
return result if result["answer"] else WebFallbackQA().forward(question)
|
| 92 |
+
router = MathRouter()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
# === Feedback Storage ===
|
| 95 |
def store_feedback(question, answer, feedback, correct_answer):
|
|
|
|
| 104 |
with open("feedback.json", "a") as f:
|
| 105 |
f.write(json.dumps(entry) + "\n")
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
# === Gradio Functions ===
|
| 108 |
def ask_question(question):
|
| 109 |
print("ask_question() called with:", question)
|
| 110 |
result = router.forward(question)
|
| 111 |
print("Result:", result)
|
| 112 |
+
#return result.answer, question, result.answer
|
| 113 |
+
#return result["answer"], question, result["answer"]
|
| 114 |
+
return result["answer"]
|
| 115 |
|
| 116 |
|
| 117 |
def submit_feedback(question, model_answer, feedback, correct_answer):
|
|
|
|
| 142 |
fb_correct = gr.Textbox(label="Correct Answer (optional)")
|
| 143 |
fb_submit_btn = gr.Button("Submit Feedback")
|
| 144 |
fb_status = gr.Textbox(label="Status", interactive=False)
|
| 145 |
+
fb_submit_btn.click(fn=submit_feedback,
|
| 146 |
+
inputs=[fb_question, fb_answer, fb_like, fb_correct],
|
| 147 |
+
outputs=[fb_status])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
demo.launch(share=True, debug=True)
|