Atualizar app/app.py

This commit is contained in:
2026-08-19 21:29:27 -03:00
parent 555ecf5e43
commit c5c4b67b1e
+26 -15
View File
@@ -87,6 +87,17 @@ def rate_limited(f):
return f(*args, **kwargs)
return wrapper
def call_ollama_with_fallback(payload):
for model in FALLBACK_MODELS:
payload["model"] = model
try:
r = requests.post(f"{app.config['OLLAMA_BASE_URL']}/api/generate", json=payload)
r.raise_for_status()
return r.json(), model
except Exception:
continue
raise Exception("All models failed")
def admin_required(f):
@wraps(f)
@@ -989,23 +1000,23 @@ def generate():
if not model_id:
return jsonify({"error": f"Unsupported model '{model_name}'."}), 400
ollama_url = f"{app.config['OLLAMA_BASE_URL']}/api/generate"
payload = {"model": model_id, "prompt": prompt}
# ollama_url = f"{app.config['OLLAMA_BASE_URL']}/api/generate"
# payload = {"model": model_id, "prompt": prompt}
# try:
# response = requests.post(ollama_url, json=payload, timeout=60)
# response.raise_for_status()
# except requests.RequestException as e:
# return jsonify({"error": "Failed to call Ollama API.", "details": str(e)}), 502
# try:
# ollama_data = response.json()
# except json.JSONDecodeError:
# return jsonify({"error": "Invalid JSON response from Ollama."}), 502
#output_text = ollama_data.get("response") or ollama_data.get("output") or ""
#return jsonify({"model": model_name, "output": output_text})
try:
response = requests.post(ollama_url, json=payload, timeout=60)
response.raise_for_status()
except requests.RequestException as e:
return jsonify({"error": "Failed to call Ollama API.", "details": str(e)}), 502
result, used_model = call_ollama_with_fallback({"prompt": prompt})
return jsonify({"model_used": used_model, "output": result.get("response")})
try:
ollama_data = response.json()
except json.JSONDecodeError:
return jsonify({"error": "Invalid JSON response from Ollama."}), 502
output_text = ollama_data.get("response") or ollama_data.get("output") or ""
return jsonify({"model": model_name, "output": output_text})
if __name__ == "__main__":