Spaces:
Sleeping
Sleeping
Download backend.py from LoopBreakers-ai/LoopBreakers: direct link, hf CLI and curl.
- Browser
- Download file 3.65 kB
-
https://huggingface.co/spaces/LoopBreakers-ai/LoopBreakers/resolve/main/backend.py
- Command line
-
hf download hf://spaces/LoopBreakers-ai/LoopBreakers/backend.py
-
curl -L -o backend.py https://huggingface.co/spaces/LoopBreakers-ai/LoopBreakers/resolve/main/backend.py
3.65 kB
| import os | |
| import re | |
| import json | |
| import requests | |
| from flask import Flask, request, jsonify, send_from_directory | |
| from flask_cors import CORS | |
| app = Flask(__name__) | |
| CORS(app) | |
| # Secure Claude API Key (use Hugging Face Secrets for production) | |
| CLAUDE_API_KEY = os.environ["CLAUDE_API_KEY"] | |
| # Extract final answer (including LaTeX $...$ or $$...$$) | |
| def extract_final_answer(text): | |
| if not text: | |
| return None | |
| # β Match full final answer line first (for things like: Final Answer: $x = 3$ or $x = 2$) | |
| match_full = re.search(r'final answer[:\-]?\s*(.*)', text, re.IGNORECASE) | |
| if match_full: | |
| answer = match_full.group(1).strip() | |
| if answer: return answer | |
| # β Match LaTeX-style $$...$$ if used alone | |
| match_dollars = re.search(r'\$\$(.+?)\$\$', text) | |
| if match_dollars: | |
| return match_dollars.group(1).strip() | |
| # β Match LaTeX-style $...$ if used alone | |
| match_dollar = re.search(r'\$(.+?)\$', text) | |
| if match_dollar: | |
| return match_dollar.group(1).strip() | |
| # β Final fallback: return last number | |
| fallback = re.findall(r'\b\d+\.?\d*\b', text) | |
| return fallback[-1].strip() if fallback else None | |
| # Claude API call function | |
| def call_claude(prompt): | |
| headers = { | |
| "x-api-key": CLAUDE_API_KEY, | |
| "Content-Type": "application/json", | |
| "anthropic-version": "2023-06-01" | |
| } | |
| body = { | |
| "model": "claude-3-opus-20240229", | |
| "max_tokens": 1024, | |
| "temperature": 0.5, | |
| "system": ( | |
| "You are a helpful and clear Year 5β10 maths tutor. " | |
| "Provide accurate, step-by-step explanations in about 5β8 natural language steps. " | |
| "Avoid calculator-style breakdowns or repeated dashes. " | |
| "Use natural teacher-style explanations. " | |
| "Use LaTeX formatting for mathematical expressions inside $...$ or $$...$$. " | |
| "At the end, clearly write: Final Answer: $[answer]$" | |
| ), | |
| "messages": [{"role": "user", "content": prompt}] | |
| } | |
| try: | |
| res = requests.post("https://api.anthropic.com/v1/messages", headers=headers, data=json.dumps(body), timeout=20) | |
| if res.status_code == 200: | |
| return res.json()["content"][0]["text"] | |
| else: | |
| return f"β Claude API Error {res.status_code}: {res.text}" | |
| except Exception as e: | |
| return f"β Claude API call failed: {e}" | |
| # Solve route | |
| def solve(): | |
| data = request.get_json() | |
| question = data.get("question", "") | |
| if not question.strip(): | |
| return jsonify({"error": "No question provided."}), 400 | |
| first_response = call_claude(question) | |
| final_answer = extract_final_answer(first_response) | |
| # Retry with clearer instruction if the answer wasn't found | |
| if not final_answer: | |
| retry_prompt = ( | |
| f"Your previous answer was unclear.\n\n" | |
| f"β Question: {question}\n" | |
| f"β Response: {first_response}\n" | |
| f"Please retry with clear steps and end with: Final Answer: $[answer]$" | |
| ) | |
| retry_response = call_claude(retry_prompt) | |
| retry_answer = extract_final_answer(retry_response) | |
| return jsonify({ | |
| "steps": retry_response, | |
| "final_answer": retry_answer or "Not found" | |
| }) | |
| return jsonify({ | |
| "steps": first_response, | |
| "final_answer": final_answer | |
| }) | |
| # Serve frontend HTML for local/browser view | |
| def serve_index(): | |
| return send_from_directory(".", "index.html") | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860) | |