Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,10 +16,6 @@ classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnl
|
|
| 16 |
print("Loading embedding model...")
|
| 17 |
embedding_model = SentenceTransformer("intfloat/e5-large")
|
| 18 |
|
| 19 |
-
print("Loading text generation model...")
|
| 20 |
-
# Use a lighter model for testing
|
| 21 |
-
#qa_pipeline = pipeline("text-generation", model="gpt2")
|
| 22 |
-
|
| 23 |
# === Qdrant Setup ===
|
| 24 |
print("Connecting to Qdrant...")
|
| 25 |
qdrant_client = QdrantClient(path="qdrant_data")
|
|
@@ -29,20 +25,16 @@ collection_name = "math_problems"
|
|
| 29 |
def is_valid_math_question(text):
|
| 30 |
candidate_labels = ["math", "not math"]
|
| 31 |
result = classifier(text, candidate_labels)
|
| 32 |
-
print("Classifier result:", result)
|
| 33 |
return result['labels'][0] == "math" and result['scores'][0] > 0.7
|
| 34 |
|
| 35 |
# === Retrieval ===
|
| 36 |
def retrieve_from_qdrant(query):
|
| 37 |
-
print("Retrieving context from Qdrant...")
|
| 38 |
query_vector = embedding_model.encode(query).tolist()
|
| 39 |
hits = qdrant_client.search(collection_name=collection_name, query_vector=query_vector, limit=3)
|
| 40 |
-
print("Retrieved hits:", hits)
|
| 41 |
return [hit.payload for hit in hits] if hits else []
|
| 42 |
|
| 43 |
# === Web Search ===
|
| 44 |
def web_search_tavily(query):
|
| 45 |
-
print("Calling Tavily...")
|
| 46 |
TAVILY_API_KEY = "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"
|
| 47 |
response = requests.post(
|
| 48 |
"https://api.tavily.com/search",
|
|
@@ -59,36 +51,25 @@ class MathAnswer(dspy.Signature):
|
|
| 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
|
| 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
|
| 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 ===
|
|
@@ -100,19 +81,14 @@ def store_feedback(question, answer, feedback, correct_answer):
|
|
| 100 |
"correct_answer": correct_answer,
|
| 101 |
"timestamp": str(datetime.now())
|
| 102 |
}
|
| 103 |
-
print("Storing feedback:", entry)
|
| 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 |
|
| 115 |
-
|
| 116 |
def submit_feedback(question, model_answer, feedback, correct_answer):
|
| 117 |
store_feedback(question, model_answer, feedback, correct_answer)
|
| 118 |
return "โ
Feedback received. Thank you!"
|
|
@@ -120,29 +96,27 @@ def submit_feedback(question, model_answer, feedback, correct_answer):
|
|
| 120 |
# === Gradio UI ===
|
| 121 |
with gr.Blocks() as demo:
|
| 122 |
gr.Markdown("## ๐งฎ Math Question Answering with DSPy + Feedback")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
gr.Markdown("### Was the answer helpful?")
|
| 138 |
-
fb_question = gr.Textbox(label="Original Question")
|
| 139 |
-
fb_answer = gr.Textbox(label="Model's Answer")
|
| 140 |
-
fb_like = gr.Radio(["๐", "๐"], label="Your Feedback")
|
| 141 |
-
fb_correct = gr.Textbox(label="Correct Answer (optional)")
|
| 142 |
-
fb_submit_btn = gr.Button("Submit Feedback")
|
| 143 |
-
fb_status = gr.Textbox(label="Status", interactive=False)
|
| 144 |
-
fb_submit_btn.click(fn=submit_feedback,
|
| 145 |
-
inputs=[fb_question, fb_answer, fb_like, fb_correct],
|
| 146 |
-
outputs=[fb_status])
|
| 147 |
|
| 148 |
demo.launch(share=True, debug=True)
|
|
|
|
|
|
| 16 |
print("Loading embedding model...")
|
| 17 |
embedding_model = SentenceTransformer("intfloat/e5-large")
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# === Qdrant Setup ===
|
| 20 |
print("Connecting to Qdrant...")
|
| 21 |
qdrant_client = QdrantClient(path="qdrant_data")
|
|
|
|
| 25 |
def is_valid_math_question(text):
|
| 26 |
candidate_labels = ["math", "not math"]
|
| 27 |
result = classifier(text, candidate_labels)
|
|
|
|
| 28 |
return result['labels'][0] == "math" and result['scores'][0] > 0.7
|
| 29 |
|
| 30 |
# === Retrieval ===
|
| 31 |
def retrieve_from_qdrant(query):
|
|
|
|
| 32 |
query_vector = embedding_model.encode(query).tolist()
|
| 33 |
hits = qdrant_client.search(collection_name=collection_name, query_vector=query_vector, limit=3)
|
|
|
|
| 34 |
return [hit.payload for hit in hits] if hits else []
|
| 35 |
|
| 36 |
# === Web Search ===
|
| 37 |
def web_search_tavily(query):
|
|
|
|
| 38 |
TAVILY_API_KEY = "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"
|
| 39 |
response = requests.post(
|
| 40 |
"https://api.tavily.com/search",
|
|
|
|
| 51 |
# === DSPy Programs ===
|
| 52 |
class MathRetrievalQA(dspy.Program):
|
| 53 |
def forward(self, question):
|
|
|
|
| 54 |
context_items = retrieve_from_qdrant(question)
|
| 55 |
context = "\n".join([item["solution"] for item in context_items if "solution" in item])
|
|
|
|
| 56 |
if not context:
|
| 57 |
+
return {"answer": "", "retrieved_context": ""}
|
| 58 |
prompt = f"Question: {question}\nContext: {context}\nAnswer:"
|
|
|
|
|
|
|
|
|
|
| 59 |
return {"answer": prompt, "retrieved_context": context}
|
| 60 |
|
|
|
|
|
|
|
| 61 |
class WebFallbackQA(dspy.Program):
|
| 62 |
def forward(self, question):
|
|
|
|
| 63 |
answer = web_search_tavily(question)
|
|
|
|
| 64 |
return {"answer": answer, "retrieved_context": "Tavily"}
|
| 65 |
|
|
|
|
| 66 |
class MathRouter(dspy.Program):
|
| 67 |
def forward(self, question):
|
|
|
|
| 68 |
if not is_valid_math_question(question):
|
| 69 |
+
return {"answer": "โ Only math questions are accepted. Please rephrase.", "retrieved_context": ""}
|
| 70 |
result = MathRetrievalQA().forward(question)
|
|
|
|
| 71 |
return result if result["answer"] else WebFallbackQA().forward(question)
|
| 72 |
+
|
| 73 |
router = MathRouter()
|
| 74 |
|
| 75 |
# === Feedback Storage ===
|
|
|
|
| 81 |
"correct_answer": correct_answer,
|
| 82 |
"timestamp": str(datetime.now())
|
| 83 |
}
|
|
|
|
| 84 |
with open("feedback.json", "a") as f:
|
| 85 |
f.write(json.dumps(entry) + "\n")
|
| 86 |
|
| 87 |
# === Gradio Functions ===
|
| 88 |
def ask_question(question):
|
|
|
|
| 89 |
result = router.forward(question)
|
|
|
|
|
|
|
| 90 |
return result["answer"], question, result["answer"]
|
| 91 |
|
|
|
|
| 92 |
def submit_feedback(question, model_answer, feedback, correct_answer):
|
| 93 |
store_feedback(question, model_answer, feedback, correct_answer)
|
| 94 |
return "โ
Feedback received. Thank you!"
|
|
|
|
| 96 |
# === Gradio UI ===
|
| 97 |
with gr.Blocks() as demo:
|
| 98 |
gr.Markdown("## ๐งฎ Math Question Answering with DSPy + Feedback")
|
| 99 |
+
|
| 100 |
+
with gr.Row():
|
| 101 |
+
question_input = gr.Textbox(label="Enter your math question", lines=2)
|
| 102 |
+
|
| 103 |
+
answer_output = gr.Markdown()
|
| 104 |
+
hidden_q = gr.Textbox(visible=False)
|
| 105 |
+
hidden_a = gr.Textbox(visible=False)
|
| 106 |
|
| 107 |
+
submit_btn = gr.Button("Get Answer")
|
| 108 |
+
submit_btn.click(fn=ask_question, inputs=[question_input], outputs=[answer_output, hidden_q, hidden_a])
|
| 109 |
+
|
| 110 |
+
# Feedback section on same page
|
| 111 |
+
gr.Markdown("### ๐ฌ Give Feedback")
|
| 112 |
+
fb_correct = gr.Textbox(label="Correct Answer (optional)")
|
| 113 |
+
fb_like = gr.Radio(["๐", "๐"], label="Was the answer helpful?")
|
| 114 |
+
fb_submit_btn = gr.Button("Submit Feedback")
|
| 115 |
+
fb_status = gr.Markdown()
|
| 116 |
+
|
| 117 |
+
fb_submit_btn.click(fn=submit_feedback,
|
| 118 |
+
inputs=[hidden_q, hidden_a, fb_like, fb_correct],
|
| 119 |
+
outputs=[fb_status])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
demo.launch(share=True, debug=True)
|
| 122 |
+
|