Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,50 @@
|
|
| 1 |
import os
|
| 2 |
from flask import Flask, request, jsonify
|
| 3 |
from waitress import serve
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
@app.get("/health")
|
| 8 |
def health():
|
| 9 |
-
return jsonify({"ok": True})
|
| 10 |
|
| 11 |
@app.post("/v1/echo")
|
| 12 |
def echo():
|
| 13 |
-
"""
|
| 14 |
-
JSON body:
|
| 15 |
-
{ "text": "hello" }
|
| 16 |
-
|
| 17 |
-
Returns:
|
| 18 |
-
{ "input": "...", "output": "..." }
|
| 19 |
-
"""
|
| 20 |
data = request.get_json(silent=True) or {}
|
| 21 |
text = data.get("text", "")
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
if __name__ == "__main__":
|
| 27 |
-
# HF Spaces expects your server to bind to 0.0.0.0 and the PORT env var if present.
|
| 28 |
port = int(os.environ.get("PORT", "7860"))
|
| 29 |
serve(app, host="0.0.0.0", port=port)
|
|
|
|
| 1 |
import os
|
| 2 |
from flask import Flask, request, jsonify
|
| 3 |
from waitress import serve
|
| 4 |
+
import google.generativeai as genai
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
+
# ---- Gemini setup ----
|
| 9 |
+
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "")
|
| 10 |
+
if GEMINI_API_KEY:
|
| 11 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 12 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 13 |
+
else:
|
| 14 |
+
model = None
|
| 15 |
+
|
| 16 |
@app.get("/health")
|
| 17 |
def health():
|
| 18 |
+
return jsonify({"ok": True, "gemini_ready": bool(model)})
|
| 19 |
|
| 20 |
@app.post("/v1/echo")
|
| 21 |
def echo():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
data = request.get_json(silent=True) or {}
|
| 23 |
text = data.get("text", "")
|
| 24 |
+
return jsonify({"input": text, "output": f"Andy heard: {text}"})
|
| 25 |
+
|
| 26 |
+
@app.post("/v1/chat")
|
| 27 |
+
def chat():
|
| 28 |
+
data = request.get_json(silent=True) or {}
|
| 29 |
+
text = (data.get("text") or "").strip()
|
| 30 |
+
if not text:
|
| 31 |
+
return jsonify({"error": "Missing 'text'"}), 400
|
| 32 |
+
if not model:
|
| 33 |
+
return jsonify({"error": "Gemini not configured. Set GEMINI_API_KEY secret."}), 500
|
| 34 |
+
|
| 35 |
+
system_style = (
|
| 36 |
+
"You are Andy, a witty pop-artist bust. "
|
| 37 |
+
"Keep responses short (1-2 sentences), playful, and conversational."
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
try:
|
| 41 |
+
resp = model.generate_content(f"{system_style}\n\nUser: {text}")
|
| 42 |
+
reply_text = (resp.text or "").strip()
|
| 43 |
+
return jsonify({"input": text, "reply_text": reply_text})
|
| 44 |
+
except Exception as e:
|
| 45 |
+
print("Gemini error:", e)
|
| 46 |
+
return jsonify({"error": "Gemini call failed"}), 500
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|
|
|
|
| 49 |
port = int(os.environ.get("PORT", "7860"))
|
| 50 |
serve(app, host="0.0.0.0", port=port)
|