tonyassi commited on
Commit
bc2ba55
·
verified ·
1 Parent(s): 0c2df8d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -11
app.py CHANGED
@@ -1,37 +1,56 @@
1
  import os
2
  from flask import Flask, request, jsonify
3
  from waitress import serve
 
4
  from google import genai
 
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():
19
  data = request.get_json(silent=True) or {}
20
- text = (data.get("text") or "").strip()
21
- if not text:
22
  return jsonify({"error": "Missing 'text'"}), 400
23
 
24
- system_style = (
25
- "You shoudld reply like Andy Warhol."
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
 
1
  import os
2
  from flask import Flask, request, jsonify
3
  from waitress import serve
4
+
5
  from google import genai
6
+ from google.genai import types
7
 
8
  app = Flask(__name__)
9
 
10
+ # Config
11
+ MODEL = os.environ.get("GEMINI_MODEL", "gemini-3-flash-preview")
12
+ THINKING_LEVEL = os.environ.get("GEMINI_THINKING_LEVEL", "HIGH")
13
+
14
+ SYSTEM_PROMPT = (
15
+ "You should respond like Andy Warhol.\n"
16
+ "Respond in 1-3 sentences and less than 300 characters."
17
+ )
18
 
19
+ # Gemini client (expects GEMINI_API_KEY set as a HF Space Secret)
20
+ client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
21
 
22
  @app.get("/health")
23
  def health():
24
+ return jsonify({"ok": True, "model": MODEL, "thinking_level": THINKING_LEVEL})
25
 
26
  @app.post("/v1/chat")
27
  def chat():
28
  data = request.get_json(silent=True) or {}
29
+ user_text = (data.get("text") or "").strip()
30
+ if not user_text:
31
  return jsonify({"error": "Missing 'text'"}), 400
32
 
33
+ contents = [
34
+ types.Content(
35
+ role="user",
36
+ parts=[types.Part.from_text(text=user_text)],
37
+ )
38
+ ]
39
+
40
+ config = types.GenerateContentConfig(
41
+ system_instruction=[types.Part.from_text(text=SYSTEM_PROMPT)],
42
+ thinking_config=types.ThinkingConfig(thinking_level=THINKING_LEVEL),
43
+ # No tools for now (faster/more predictable)
44
  )
45
 
46
  try:
47
  resp = client.models.generate_content(
48
  model=MODEL,
49
+ contents=contents,
50
+ config=config,
51
  )
52
+ reply_text = (resp.text or "").strip()
53
+ return jsonify({"input": user_text, "reply_text": reply_text, "model": MODEL})
54
  except Exception as e:
55
  print("Gemini error:", repr(e))
56
  return jsonify({"error": "Gemini call failed"}), 500