Spaces:
Running

mgokg commited on
Commit
3e8c029
·
verified ·
1 Parent(s): 7f73cd3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -15
app.py CHANGED
@@ -3,8 +3,13 @@ import gradio as gr
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
  """
@@ -15,14 +20,13 @@ def generate_with_history(input_text, history):
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",
@@ -30,7 +34,10 @@ def generate_with_history(input_text, history):
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(
@@ -38,14 +45,13 @@ def generate_with_history(input_text, history):
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:
@@ -53,20 +59,19 @@ def generate_with_history(input_text, history):
53
 
54
  # Building the Blocks interface
55
  with gr.Blocks() as demo:
56
- # State component (always invisible)
57
  history_state = gr.State([])
58
-
59
- # Change visible=False to True (or remove it)
60
- input_box = gr.Textbox(label="Input", visible=True)
61
  output_box = gr.Markdown(label="Response", visible=True)
62
-
63
  btn = gr.Button("Submit", visible=True)
64
-
65
  btn.click(
66
  fn=generate_with_history,
67
  inputs=[input_box, history_state],
68
  outputs=[output_box, history_state],
69
  api_name="generate"
70
  )
 
71
  if __name__ == "__main__":
72
  demo.launch()
 
3
  from google import genai
4
  from google.genai import types
5
 
6
+ # Client initialization (v1beta is required for Google Search as a tool)
7
+ client_genai = genai.Client(
8
+ api_key=os.environ.get("GEMINI_API_KEY"),
9
+ http_options=types.HttpOptions(api_version="v1beta"),
10
+ )
11
+
12
+ SYSTEM_PROMPT = "antworte kurz und knapp. optimiere deine antwort für sprachausgabe."
13
 
14
  def generate_with_history(input_text, history):
15
  """
 
20
  history = []
21
 
22
  # Build the contents list including history
 
23
  contents = []
24
  for msg in history:
25
  contents.append(types.Content(
26
  role=msg["role"],
27
  parts=[types.Part.from_text(text=msg["content"])]
28
  ))
29
+
30
  # Add the current user prompt
31
  contents.append(types.Content(
32
  role="user",
 
34
  ))
35
 
36
  tools = [types.Tool(google_search=types.GoogleSearch())]
37
+ config = types.GenerateContentConfig(
38
+ tools=tools,
39
+ system_instruction=SYSTEM_PROMPT,
40
+ )
41
 
42
  try:
43
  response = client_genai.models.generate_content(
 
45
  contents=contents,
46
  config=config,
47
  )
48
+
49
  response_text = response.text
50
+
51
  # Update history: add the current exchange
52
  history.append({"role": "user", "content": input_text})
53
  history.append({"role": "model", "content": response_text})
54
+
 
55
  return response_text, history
56
 
57
  except Exception as e:
 
59
 
60
  # Building the Blocks interface
61
  with gr.Blocks() as demo:
 
62
  history_state = gr.State([])
63
+
64
+ input_box = gr.Textbox(label="Input", visible=True)
 
65
  output_box = gr.Markdown(label="Response", visible=True)
66
+
67
  btn = gr.Button("Submit", visible=True)
68
+
69
  btn.click(
70
  fn=generate_with_history,
71
  inputs=[input_box, history_state],
72
  outputs=[output_box, history_state],
73
  api_name="generate"
74
  )
75
+
76
  if __name__ == "__main__":
77
  demo.launch()