mgokg commited on
Commit
11b0a8c
·
verified ·
1 Parent(s): 6a4db8d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -29
app.py CHANGED
@@ -3,56 +3,72 @@ import gradio as gr
3
  from google import genai
4
  from google.genai import types
5
 
6
- # Client initialisieren
7
  client_genai = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
8
 
9
- def generate_for_js(input_text):
10
  """
11
- Diese Funktion entspricht exakt der Erwartung des JS-Clients:
12
- - Input: input_text
13
- - Output: Ein String (oder Liste von Strings)
14
  """
15
- contents = [
16
- types.Content(
17
- role="user",
18
- parts=[types.Part.from_text(text=input_text)],
19
- ),
20
- ]
 
 
 
 
 
21
 
 
 
 
 
 
 
22
  tools = [types.Tool(google_search=types.GoogleSearch())]
23
  config = types.GenerateContentConfig(tools=tools)
24
 
25
- full_response = ""
26
-
27
  try:
28
- # Da das JS await client.predict nutzt, sammeln wir hier den gesamten Text
29
- # Falls du echtes Streaming im JS willst, müsste dort 'client.submit' genutzt werden.
30
  response = client_genai.models.generate_content(
31
  model="gemini-2.0-flash-001",
32
  contents=contents,
33
  config=config,
34
  )
35
- return response.text
 
 
 
 
 
 
 
 
 
36
  except Exception as e:
37
- return f"Fehler: {str(e)}"
38
 
39
- # Gradio Interface Definition
40
  with gr.Blocks() as demo:
41
- # Der API-Name muss "generate" sein, damit /generate im JS funktioniert
42
- prompt_input = gr.Textbox(label="Input Text", visible=False)
43
- output_text = gr.Markdown(visible=False)
44
 
45
- submit_btn = gr.Button("Senden", visible=False)
 
 
46
 
47
- # Hier definieren wir den Endpunkt explizit
48
- submit_btn.click(
49
- fn=generate_for_js,
50
- inputs=prompt_input,
51
- outputs=output_text,
 
 
52
  api_name="generate"
53
  )
54
 
55
  if __name__ == "__main__":
56
- # Wichtig: Wenn du es auf Hugging Face Spaces hostest (mgokg/webapi),
57
- # wird der api_name automatisch unter /predict/generate verfügbar.
58
  demo.launch()
 
3
  from google import genai
4
  from google.genai import types
5
 
6
+ # Client initialization
7
  client_genai = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
8
 
9
+ def generate_with_history(input_text, history):
10
  """
11
+ - input_text: The new message from JS
12
+ - history: A list of previous messages [user, model, user, model...]
 
13
  """
14
+ if history is None:
15
+ history = []
16
+
17
+ # Build the contents list including history
18
+ # Gemini API expects a list of Content objects
19
+ contents = []
20
+ for msg in history:
21
+ contents.append(types.Content(
22
+ role=msg["role"],
23
+ parts=[types.Part.from_text(text=msg["content"])]
24
+ ))
25
 
26
+ # Add the current user prompt
27
+ contents.append(types.Content(
28
+ role="user",
29
+ parts=[types.Part.from_text(text=input_text)]
30
+ ))
31
+
32
  tools = [types.Tool(google_search=types.GoogleSearch())]
33
  config = types.GenerateContentConfig(tools=tools)
34
 
 
 
35
  try:
 
 
36
  response = client_genai.models.generate_content(
37
  model="gemini-2.0-flash-001",
38
  contents=contents,
39
  config=config,
40
  )
41
+
42
+ response_text = response.text
43
+
44
+ # Update history: add the current exchange
45
+ history.append({"role": "user", "content": input_text})
46
+ history.append({"role": "model", "content": response_text})
47
+
48
+ # Return the response for JS and the updated history for the State
49
+ return response_text, history
50
+
51
  except Exception as e:
52
+ return f"Error: {str(e)}", history
53
 
54
+ # Building the Blocks interface
55
  with gr.Blocks() as demo:
56
+ # State component to store the conversation across API calls
57
+ history_state = gr.State([])
 
58
 
59
+ # Hidden components to match your JS expectations
60
+ input_box = gr.Textbox(visible=False)
61
+ output_box = gr.Markdown(visible=False)
62
 
63
+ # The API endpoint
64
+ # Note: We include history_state in both inputs and outputs
65
+ btn = gr.Button("Submit", visible=False)
66
+ btn.click(
67
+ fn=generate_with_history,
68
+ inputs=[input_box, history_state],
69
+ outputs=[output_box, history_state],
70
  api_name="generate"
71
  )
72
 
73
  if __name__ == "__main__":
 
 
74
  demo.launch()