Spaces:
Running
Running
Download _old/app.py.api-backup from Nrighton233j/Api-cache: direct link, hf CLI and curl.
- Browser
- Download file 1.57 kB
-
https://huggingface.co/spaces/Nrighton233j/Api-cache/resolve/main/_old/app.py.api-backup
- Command line
-
hf download hf://spaces/Nrighton233j/Api-cache/_old/app.py.api-backup
-
curl -L -o app.py.api-backup https://huggingface.co/spaces/Nrighton233j/Api-cache/resolve/main/_old/app.py.api-backup
1.57 kB
| from flask import Flask, request, jsonify | |
| import os | |
| import requests | |
| app = Flask(__name__) | |
| OLLAMA_URL = os.getenv( | |
| "OLLAMA_URL", | |
| "http://127.0.0.1:11434/api/chat" | |
| ) | |
| OLLAMA_MODEL = os.getenv( | |
| "OLLAMA_MODEL", | |
| "gemma2:2b" | |
| ) | |
| .route("/") | |
| def health(): | |
| return { | |
| "status": "ok", | |
| "app": "api-cache", | |
| "ai": "ollama" | |
| } | |
| .route("/api/ollama/chat", methods=["POST"]) | |
| def ollama_chat(): | |
| data = request.get_json(silent=True) or {} | |
| messages = data.get("messages") | |
| if not messages: | |
| prompt = data.get("prompt", "") | |
| if not prompt: | |
| return jsonify({ | |
| "error": "missing prompt or messages" | |
| }), 400 | |
| messages = [ | |
| { | |
| "role": "user", | |
| "content": prompt | |
| } | |
| ] | |
| try: | |
| response = requests.post( | |
| OLLAMA_URL, | |
| json={ | |
| "model": OLLAMA_MODEL, | |
| "messages": messages, | |
| "stream": False | |
| }, | |
| timeout=300 | |
| ) | |
| response.raise_for_status() | |
| result = response.json() | |
| return jsonify({ | |
| "model": OLLAMA_MODEL, | |
| "message": result.get("message", {}), | |
| "done": result.get("done", False) | |
| }) | |
| except requests.RequestException as e: | |
| return jsonify({ | |
| "error": "Ollama request failed", | |
| "details": str(e) | |
| }), 502 | |
| if __name__ == "__main__": | |
| app.run( | |
| host="0.0.0.0", | |
| port=5000 | |
| ) | |