Update app.py
Browse files
app.py
CHANGED
|
@@ -5,23 +5,14 @@ from google import genai
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
model = genai.GenerativeModel("gemini-2.5-flash")
|
| 13 |
-
else:
|
| 14 |
-
model = None
|
| 15 |
|
| 16 |
@app.get("/health")
|
| 17 |
def health():
|
| 18 |
-
return jsonify({"ok": True, "
|
| 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():
|
|
@@ -29,20 +20,20 @@ def chat():
|
|
| 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
|
| 38 |
)
|
| 39 |
|
| 40 |
try:
|
| 41 |
-
resp =
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
except Exception as e:
|
| 45 |
-
print("Gemini error:", e)
|
| 46 |
return jsonify({"error": "Gemini call failed"}), 500
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|
|
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
+
# Uses GEMINI_API_KEY from env automatically (recommended by Google docs)
|
| 9 |
+
client = genai.Client()
|
| 10 |
+
|
| 11 |
+
MODEL = os.environ.get("GEMINI_MODEL", "gemini-2.5-flash") # or gemini-2.5-flash-lite
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
@app.get("/health")
|
| 14 |
def health():
|
| 15 |
+
return jsonify({"ok": True, "model": MODEL})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
@app.post("/v1/chat")
|
| 18 |
def chat():
|
|
|
|
| 20 |
text = (data.get("text") or "").strip()
|
| 21 |
if not text:
|
| 22 |
return jsonify({"error": "Missing 'text'"}), 400
|
|
|
|
|
|
|
| 23 |
|
| 24 |
system_style = (
|
| 25 |
"You are Andy, a witty pop-artist bust. "
|
| 26 |
+
"Keep replies short (1-2 sentences), playful, and conversational."
|
| 27 |
)
|
| 28 |
|
| 29 |
try:
|
| 30 |
+
resp = client.models.generate_content(
|
| 31 |
+
model=MODEL,
|
| 32 |
+
contents=f"{system_style}\n\nUser: {text}"
|
| 33 |
+
)
|
| 34 |
+
return jsonify({"input": text, "reply_text": (resp.text or "").strip()})
|
| 35 |
except Exception as e:
|
| 36 |
+
print("Gemini error:", repr(e))
|
| 37 |
return jsonify({"error": "Gemini call failed"}), 500
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|