Download app.py from CCockrum/Casseuse: direct link, hf CLI and curl.
- Browser
- Download file 3.04 kB
-
https://huggingface.co/spaces/CCockrum/Casseuse/resolve/main/app.py
- Command line
-
hf download hf://spaces/CCockrum/Casseuse/app.py
-
curl -L -o app.py https://huggingface.co/spaces/CCockrum/Casseuse/resolve/main/app.py
3.04 kB
| from flask import Flask, request, jsonify, send_from_directory | |
| from flask_cors import CORS | |
| import os | |
| import requests | |
| import time | |
| from functools import wraps | |
| app = Flask(__name__, static_folder='static') | |
| CORS(app) | |
| # Get API key from environment variable | |
| ANTHROPIC_API_KEY = os.environ.get("ANTHROPIC_API_KEY") | |
| if not ANTHROPIC_API_KEY: | |
| raise ValueError("ANTHROPIC_API_KEY not found in environment variables!") | |
| # Simple rate limiting (per IP) | |
| rate_limit_storage = {} | |
| RATE_LIMIT = 50 # requests per hour per IP | |
| RATE_WINDOW = 3600 # 1 hour in seconds | |
| def rate_limit(f): | |
| def decorated_function(*args, **kwargs): | |
| ip = request.remote_addr | |
| current_time = time.time() | |
| # Clean old entries | |
| if ip in rate_limit_storage: | |
| rate_limit_storage[ip] = [t for t in rate_limit_storage[ip] if current_time - t < RATE_WINDOW] | |
| else: | |
| rate_limit_storage[ip] = [] | |
| # Check rate limit | |
| if len(rate_limit_storage[ip]) >= RATE_LIMIT: | |
| return jsonify({"error": "Rate limit exceeded. Please try again later."}), 429 | |
| # Add current request | |
| rate_limit_storage[ip].append(current_time) | |
| return f(*args, **kwargs) | |
| return decorated_function | |
| def index(): | |
| return send_from_directory('static', 'index.html') | |
| def static_files(path): | |
| return send_from_directory('static', path) | |
| def chat(): | |
| try: | |
| data = request.json | |
| # Validate request | |
| if not data or 'messages' not in data: | |
| return jsonify({"error": "Invalid request format"}), 400 | |
| messages = data.get('messages', []) | |
| system_prompt = data.get('system', '') | |
| model = data.get('model', 'claude-sonnet-4-20250514') | |
| max_tokens = data.get('max_tokens', 1000) | |
| # Call Anthropic API | |
| response = requests.post( | |
| "https://api.anthropic.com/v1/messages", | |
| headers={ | |
| "x-api-key": ANTHROPIC_API_KEY, | |
| "anthropic-version": "2023-06-01", | |
| "content-type": "application/json" | |
| }, | |
| json={ | |
| "model": model, | |
| "max_tokens": max_tokens, | |
| "system": system_prompt, | |
| "messages": messages | |
| }, | |
| timeout=30 | |
| ) | |
| if response.status_code == 200: | |
| return jsonify(response.json()) | |
| else: | |
| return jsonify({ | |
| "error": f"API error: {response.status_code}", | |
| "details": response.text | |
| }), response.status_code | |
| except Exception as e: | |
| return jsonify({"error": str(e)}), 500 | |
| def health(): | |
| return jsonify({"status": "healthy", "api_key_configured": bool(ANTHROPIC_API_KEY)}) | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0', port=7860) |