Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,6 +14,13 @@ import google.generativeai as genai
|
|
| 14 |
genai.configure(api_key="AIzaSyBO3-HG-WcITn58PdpK7mMyvFQitoH00qA") # Replace with your actual Gemini API key
|
| 15 |
|
| 16 |
# Load Gemini model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
import re
|
|
@@ -137,14 +144,22 @@ class WebFallbackQA(dspy.Program):
|
|
| 137 |
return {"answer": answer, "retrieved_context": "Tavily"}
|
| 138 |
|
| 139 |
|
|
|
|
| 140 |
class MathRouter(dspy.Program):
|
| 141 |
def forward(self, question):
|
| 142 |
print("Routing question:", question)
|
| 143 |
if not is_valid_math_question(question):
|
| 144 |
-
return
|
|
|
|
| 145 |
result = MathRetrievalQA().forward(question)
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
return result if result["answer"] else WebFallbackQA().forward(question)
|
|
|
|
| 148 |
router = MathRouter()
|
| 149 |
|
| 150 |
# === Feedback Storage ===
|
|
|
|
| 14 |
genai.configure(api_key="AIzaSyBO3-HG-WcITn58PdpK7mMyvFQitoH00qA") # Replace with your actual Gemini API key
|
| 15 |
|
| 16 |
# Load Gemini model
|
| 17 |
+
def output_guard(answer):
|
| 18 |
+
# Check if answer is empty or too short
|
| 19 |
+
if not answer or len(answer.strip()) < 20:
|
| 20 |
+
print("Output guard triggered: answer too short or empty.")
|
| 21 |
+
return False
|
| 22 |
+
# You can add more checks here if needed
|
| 23 |
+
return True
|
| 24 |
|
| 25 |
|
| 26 |
import re
|
|
|
|
| 144 |
return {"answer": answer, "retrieved_context": "Tavily"}
|
| 145 |
|
| 146 |
|
| 147 |
+
|
| 148 |
class MathRouter(dspy.Program):
|
| 149 |
def forward(self, question):
|
| 150 |
print("Routing question:", question)
|
| 151 |
if not is_valid_math_question(question):
|
| 152 |
+
return {"answer": "❌ Only math questions are accepted. Please rephrase.", "retrieved_context": ""}
|
| 153 |
+
|
| 154 |
result = MathRetrievalQA().forward(question)
|
| 155 |
+
|
| 156 |
+
# Apply output guard here
|
| 157 |
+
if not output_guard(result["answer"]):
|
| 158 |
+
print("Output guard failed, falling back to web search.")
|
| 159 |
+
return WebFallbackQA().forward(question)
|
| 160 |
+
|
| 161 |
return result if result["answer"] else WebFallbackQA().forward(question)
|
| 162 |
+
|
| 163 |
router = MathRouter()
|
| 164 |
|
| 165 |
# === Feedback Storage ===
|