Spaces:
Sleeping
Sleeping
Update backend.py
Browse files- backend.py +27 -14
backend.py
CHANGED
|
@@ -5,13 +5,13 @@ import requests
|
|
| 5 |
from flask import Flask, request, jsonify, send_from_directory
|
| 6 |
from flask_cors import CORS
|
| 7 |
|
| 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 |
-
#
|
| 15 |
def extract_final_answer(text):
|
| 16 |
if not text:
|
| 17 |
return None
|
|
@@ -21,7 +21,25 @@ 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 call_claude(prompt):
|
| 26 |
headers = {
|
| 27 |
"x-api-key": CLAUDE_API_KEY,
|
|
@@ -55,11 +73,10 @@ def solve():
|
|
| 55 |
if not question.strip():
|
| 56 |
return jsonify({"error": "No question provided."}), 400
|
| 57 |
|
| 58 |
-
# First
|
| 59 |
first_response = call_claude(question)
|
| 60 |
final_answer = extract_final_answer(first_response)
|
| 61 |
|
| 62 |
-
# Retry if needed
|
| 63 |
if not final_answer:
|
| 64 |
retry_prompt = (
|
| 65 |
f"Your previous answer to this question was incomplete.\n\n"
|
|
@@ -70,22 +87,18 @@ def solve():
|
|
| 70 |
)
|
| 71 |
retry_response = call_claude(retry_prompt)
|
| 72 |
retry_answer = extract_final_answer(retry_response)
|
|
|
|
| 73 |
return jsonify({
|
| 74 |
-
"steps":
|
| 75 |
"final_answer": retry_answer or "Not found"
|
| 76 |
})
|
| 77 |
|
| 78 |
-
|
| 79 |
return jsonify({
|
| 80 |
-
"steps":
|
| 81 |
"final_answer": final_answer
|
| 82 |
})
|
| 83 |
|
| 84 |
-
# Serve index.html from root
|
| 85 |
-
@app.route("/")
|
| 86 |
-
def serve_index():
|
| 87 |
-
return send_from_directory(".", "index.html")
|
| 88 |
-
|
| 89 |
# Run app
|
| 90 |
if __name__ == "__main__":
|
| 91 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 5 |
from flask import Flask, request, jsonify, send_from_directory
|
| 6 |
from flask_cors import CORS
|
| 7 |
|
| 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 |
+
# Clean repeated lines and stop after final answer
|
| 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,
|
|
|
|
| 73 |
if not question.strip():
|
| 74 |
return jsonify({"error": "No question provided."}), 400
|
| 75 |
|
| 76 |
+
# First attempt
|
| 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 |
)
|
| 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": cleaned_steps,
|
| 93 |
"final_answer": retry_answer or "Not found"
|
| 94 |
})
|
| 95 |
|
| 96 |
+
cleaned_steps = clean_steps(first_response)
|
| 97 |
return jsonify({
|
| 98 |
+
"steps": cleaned_steps,
|
| 99 |
"final_answer": final_answer
|
| 100 |
})
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
# Run app
|
| 103 |
if __name__ == "__main__":
|
| 104 |
app.run(host="0.0.0.0", port=7860)
|