mgokg commited on
Commit
868e79a
·
verified ·
1 Parent(s): 846bc4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -63
app.py CHANGED
@@ -4,68 +4,13 @@ import os
4
  import json
5
  from google import genai
6
  from google.genai import types
7
- from gradio_client import Client
8
-
9
- def clean_json_string(json_str):
10
- """
11
- Removes any comments or prefixes before the actual JSON content.
12
- """
13
- # Find the first occurrence of '{'
14
- json_start = json_str.find('{')
15
- if json_start == -1:
16
- # If no '{' is found, try with '[' for arrays
17
- json_start = json_str.find('[')
18
- if json_start == -1:
19
- return json_str
20
-
21
- # Extract everything from the first JSON marker
22
- cleaned_str = json_str[json_start:]
23
-
24
- # Verify it's valid JSON
25
- try:
26
- json.loads(cleaned_str)
27
- return cleaned_str
28
- except json.JSONDecodeError:
29
- return cleaned_str # Return extracted part anyway
30
-
31
- def generate(input_text):
32
- client = genai.Client(
33
- api_key=os.environ.get("GEMINI_API_KEY"),
34
- )
35
-
36
- model = "gemini-flash-latest"
37
- contents = [
38
- types.Content(
39
- role="user",
40
- parts=[
41
- types.Part.from_text(text=f"{input_text}"),
42
- ],
43
- ),
44
- ]
45
- tools = [
46
- types.Tool(googleSearch=types.GoogleSearch(
47
- )),
48
- ]
49
- generate_content_config = types.GenerateContentConfig(
50
- temperature=0.35,
51
- tools=tools,
52
- )
53
-
54
- for chunk in client.models.generate_content_stream(
55
- model=model,
56
- contents=contents,
57
- config=generate_content_config,
58
- ):
59
- print(chunk.text, end="")
60
-
61
-
62
 
63
  def generate1(input_text):
64
  client = genai.Client(
65
  api_key=os.environ.get("GEMINI_API_KEY"),
66
  )
67
 
68
- model = "gemini-2.0-flash"
69
  contents = [
70
  types.Content(
71
  role="user",
@@ -74,9 +19,12 @@ def generate1(input_text):
74
  ],
75
  ),
76
  ]
 
 
77
  tools = [
78
- types.Tool(google_search_retrieval=types.DynamicRetrievalConfig(dynamic_threshold=0.3, mode="MODE_DYNAMIC")),
79
  ]
 
80
  generate_content_config = types.GenerateContentConfig(
81
  temperature=0.45,
82
  tools=tools,
@@ -84,26 +32,29 @@ def generate1(input_text):
84
  )
85
 
86
  full_response = ""
 
87
  for chunk in client.models.generate_content_stream(
88
  model=model,
89
  contents=contents,
90
  config=generate_content_config,
91
  ):
92
- full_response += chunk.text
93
- yield full_response
 
 
94
 
95
  if __name__ == '__main__':
96
  with gr.Blocks() as demo:
97
  gr.Markdown("# Gemini 2.0 Flash + Websearch")
98
  output_textbox = gr.Markdown()
99
- input_textbox = gr.Textbox(lines=3, label="Input", placeholder="Enter event details here...")
100
  submit_button = gr.Button("Send")
101
 
102
- # Die Funktion generate gibt nun (Response, ClearInput) zurück
103
  submit_button.click(
104
- fn=generate,
105
  inputs=input_textbox,
106
- outputs=[output_textbox]
107
  )
108
 
109
  demo.launch(show_error=True)
 
4
  import json
5
  from google import genai
6
  from google.genai import types
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  def generate1(input_text):
9
  client = genai.Client(
10
  api_key=os.environ.get("GEMINI_API_KEY"),
11
  )
12
 
13
+ model = "gemini-flash-latest"
14
  contents = [
15
  types.Content(
16
  role="user",
 
19
  ],
20
  ),
21
  ]
22
+
23
+ # Updated tool config for Gemini 2.0
24
  tools = [
25
+ types.Tool(google_search_retrieval=types.DynamicRetrievalConfig(mode="DYNAMIC"))
26
  ]
27
+
28
  generate_content_config = types.GenerateContentConfig(
29
  temperature=0.45,
30
  tools=tools,
 
32
  )
33
 
34
  full_response = ""
35
+ # We yield two values: the accumulated response and an empty string (to clear the input)
36
  for chunk in client.models.generate_content_stream(
37
  model=model,
38
  contents=contents,
39
  config=generate_content_config,
40
  ):
41
+ if chunk.text:
42
+ full_response += chunk.text
43
+ # Yielding [Output, Input]
44
+ yield full_response, gr.update(value="")
45
 
46
  if __name__ == '__main__':
47
  with gr.Blocks() as demo:
48
  gr.Markdown("# Gemini 2.0 Flash + Websearch")
49
  output_textbox = gr.Markdown()
50
+ input_textbox = gr.Textbox(lines=3, label="Input", placeholder="Enter name here...")
51
  submit_button = gr.Button("Send")
52
 
53
+ # Pointing to generate1 which now yields two values
54
  submit_button.click(
55
+ fn=generate1,
56
  inputs=input_textbox,
57
+ outputs=[output_textbox, input_textbox]
58
  )
59
 
60
  demo.launch(show_error=True)