Spaces:
Sleeping
Sleeping
Update backend.py
Browse files- backend.py +20 -28
backend.py
CHANGED
|
@@ -8,10 +8,10 @@ from flask_cors import CORS
|
|
| 8 |
app = Flask(__name__)
|
| 9 |
CORS(app)
|
| 10 |
|
| 11 |
-
# Claude API key
|
| 12 |
CLAUDE_API_KEY = os.environ.get("CLAUDE_API_KEY", "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")
|
| 13 |
|
| 14 |
-
# Extract final numeric answer
|
| 15 |
def extract_final_answer(text):
|
| 16 |
if not text:
|
| 17 |
return None
|
|
@@ -21,25 +21,7 @@ def extract_final_answer(text):
|
|
| 21 |
fallback = re.findall(r'\b\d+\.?\d*\b', text)
|
| 22 |
return fallback[-1].strip() if fallback else None
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
def clean_steps(raw_text):
|
| 26 |
-
lines = raw_text.strip().split("\n")
|
| 27 |
-
seen = set()
|
| 28 |
-
cleaned = []
|
| 29 |
-
|
| 30 |
-
for line in lines:
|
| 31 |
-
stripped = line.strip()
|
| 32 |
-
if not stripped:
|
| 33 |
-
continue
|
| 34 |
-
if stripped.lower().startswith("**final answer"):
|
| 35 |
-
break
|
| 36 |
-
if stripped not in seen:
|
| 37 |
-
cleaned.append(stripped)
|
| 38 |
-
seen.add(stripped)
|
| 39 |
-
|
| 40 |
-
return "\n".join(cleaned)
|
| 41 |
-
|
| 42 |
-
# Claude call
|
| 43 |
def call_claude(prompt):
|
| 44 |
headers = {
|
| 45 |
"x-api-key": CLAUDE_API_KEY,
|
|
@@ -56,6 +38,7 @@ def call_claude(prompt):
|
|
| 56 |
),
|
| 57 |
"messages": [{"role": "user", "content": prompt}]
|
| 58 |
}
|
|
|
|
| 59 |
try:
|
| 60 |
res = requests.post("https://api.anthropic.com/v1/messages", headers=headers, data=json.dumps(body), timeout=20)
|
| 61 |
if res.status_code == 200:
|
|
@@ -65,7 +48,7 @@ def call_claude(prompt):
|
|
| 65 |
except Exception as e:
|
| 66 |
return f"β Claude API call failed: {e}"
|
| 67 |
|
| 68 |
-
#
|
| 69 |
@app.route("/solve", methods=["POST"])
|
| 70 |
def solve():
|
| 71 |
data = request.get_json()
|
|
@@ -73,10 +56,11 @@ def solve():
|
|
| 73 |
if not question.strip():
|
| 74 |
return jsonify({"error": "No question provided."}), 400
|
| 75 |
|
| 76 |
-
# First
|
| 77 |
first_response = call_claude(question)
|
| 78 |
final_answer = extract_final_answer(first_response)
|
| 79 |
|
|
|
|
| 80 |
if not final_answer:
|
| 81 |
retry_prompt = (
|
| 82 |
f"Your previous answer to this question was incomplete.\n\n"
|
|
@@ -87,18 +71,26 @@ def solve():
|
|
| 87 |
)
|
| 88 |
retry_response = call_claude(retry_prompt)
|
| 89 |
retry_answer = extract_final_answer(retry_response)
|
| 90 |
-
cleaned_steps = clean_steps(retry_response)
|
| 91 |
return jsonify({
|
| 92 |
-
"steps":
|
| 93 |
"final_answer": retry_answer or "Not found"
|
| 94 |
})
|
| 95 |
|
| 96 |
-
cleaned_steps = clean_steps(first_response)
|
| 97 |
return jsonify({
|
| 98 |
-
"steps":
|
| 99 |
"final_answer": final_answer
|
| 100 |
})
|
| 101 |
|
| 102 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
if __name__ == "__main__":
|
| 104 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 8 |
app = Flask(__name__)
|
| 9 |
CORS(app)
|
| 10 |
|
| 11 |
+
# β
Claude API key
|
| 12 |
CLAUDE_API_KEY = os.environ.get("CLAUDE_API_KEY", "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")
|
| 13 |
|
| 14 |
+
# β
Extract final numeric answer
|
| 15 |
def extract_final_answer(text):
|
| 16 |
if not text:
|
| 17 |
return None
|
|
|
|
| 21 |
fallback = re.findall(r'\b\d+\.?\d*\b', text)
|
| 22 |
return fallback[-1].strip() if fallback else None
|
| 23 |
|
| 24 |
+
# β
Query Claude
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
def call_claude(prompt):
|
| 26 |
headers = {
|
| 27 |
"x-api-key": CLAUDE_API_KEY,
|
|
|
|
| 38 |
),
|
| 39 |
"messages": [{"role": "user", "content": prompt}]
|
| 40 |
}
|
| 41 |
+
|
| 42 |
try:
|
| 43 |
res = requests.post("https://api.anthropic.com/v1/messages", headers=headers, data=json.dumps(body), timeout=20)
|
| 44 |
if res.status_code == 200:
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
return f"β Claude API call failed: {e}"
|
| 50 |
|
| 51 |
+
# β
API route
|
| 52 |
@app.route("/solve", methods=["POST"])
|
| 53 |
def solve():
|
| 54 |
data = request.get_json()
|
|
|
|
| 56 |
if not question.strip():
|
| 57 |
return jsonify({"error": "No question provided."}), 400
|
| 58 |
|
| 59 |
+
# First response
|
| 60 |
first_response = call_claude(question)
|
| 61 |
final_answer = extract_final_answer(first_response)
|
| 62 |
|
| 63 |
+
# Retry only if first response fails
|
| 64 |
if not final_answer:
|
| 65 |
retry_prompt = (
|
| 66 |
f"Your previous answer to this question was incomplete.\n\n"
|
|
|
|
| 71 |
)
|
| 72 |
retry_response = call_claude(retry_prompt)
|
| 73 |
retry_answer = extract_final_answer(retry_response)
|
|
|
|
| 74 |
return jsonify({
|
| 75 |
+
"steps": retry_response,
|
| 76 |
"final_answer": retry_answer or "Not found"
|
| 77 |
})
|
| 78 |
|
|
|
|
| 79 |
return jsonify({
|
| 80 |
+
"steps": first_response,
|
| 81 |
"final_answer": final_answer
|
| 82 |
})
|
| 83 |
|
| 84 |
+
# β
Serve frontend (index.html)
|
| 85 |
+
@app.route("/")
|
| 86 |
+
def index():
|
| 87 |
+
return send_from_directory(".", "index.html")
|
| 88 |
+
|
| 89 |
+
# β
Allow other static assets (e.g. CSS, favicon)
|
| 90 |
+
@app.route("/<path:path>")
|
| 91 |
+
def static_proxy(path):
|
| 92 |
+
return send_from_directory(".", path)
|
| 93 |
+
|
| 94 |
+
# β
Run app
|
| 95 |
if __name__ == "__main__":
|
| 96 |
app.run(host="0.0.0.0", port=7860)
|