tonyassi commited on
Commit
10789e2
·
verified ·
1 Parent(s): 0002844

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -21
app.py CHANGED
@@ -5,23 +5,14 @@ from google import 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-2.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():
@@ -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 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__":
 
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__":