mgokg commited on
Commit
cbc7caf
·
verified ·
1 Parent(s): 7bcf55c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -24
app.py CHANGED
@@ -16,53 +16,56 @@ def clean_json_string(json_str):
16
  # If no '{' is found, try with '[' for arrays
17
  json_start = json_str.find('[')
18
  if json_start == -1:
19
- return json_str # Return original if no JSON markers found
20
 
21
  # Extract everything from the first JSON marker
22
  cleaned_str = json_str[json_start:]
23
- return cleaned_str
24
  # Verify it's valid JSON
25
  try:
26
  json.loads(cleaned_str)
27
  return cleaned_str
28
  except json.JSONDecodeError:
29
- return json_str # Return original if cleaning results in invalid JSON
30
 
31
  def generate(input_text):
32
  try:
33
  client = genai.Client(
34
  api_key=os.environ.get("GEMINI_API_KEY"),
35
  )
36
- except Exception as e:
37
- return f"Error initializing client: {e}. Make sure GEMINI_API_KEY is set."
38
-
39
- model = "gemini-3-flash-preview"
40
  contents = [
41
  types.Content(
42
  role="user",
43
  parts=[
44
- types.Part.from_text(text="""INSERT_INPUT_HERE"""),
45
  ],
46
  ),
47
  ]
48
  tools = [
49
- types.Tool(googleSearch=types.GoogleSearch(
50
- )),
51
  ]
52
  generate_content_config = types.GenerateContentConfig(
53
  temperature=0.35,
54
- thinking_config=types.ThinkingConfig(
55
- thinking_level="HIGH",
56
- ),
57
  tools=tools,
58
  )
59
 
 
60
  for chunk in client.models.generate_content_stream(
61
  model=model,
62
  contents=contents,
63
  config=generate_content_config,
64
  ):
65
- print(chunk.text, end="")
 
 
 
 
 
 
66
 
67
 
68
  def generate1(input_text):
@@ -70,17 +73,17 @@ def generate1(input_text):
70
  api_key=os.environ.get("GEMINI_API_KEY"),
71
  )
72
 
73
- model = "gemini-3-flash-preview"
74
  contents = [
75
  types.Content(
76
  role="user",
77
  parts=[
78
- types.Part.from_text(text=f"return json object with keys name and email. name = {input_text} search the web for the email value. return json object only, no additional text or comments"),
79
  ],
80
  ),
81
  ]
82
  tools = [
83
- types.Tool(googleSearchRetrieval=types.DynamicRetrievalConfig(dynamicThreshold=0.3, mode=types.DynamicRetrievalConfigMode.MODE_DYNAMIC)),
84
  ]
85
  generate_content_config = types.GenerateContentConfig(
86
  temperature=0.45,
@@ -88,20 +91,27 @@ def generate1(input_text):
88
  response_mime_type="text/plain",
89
  )
90
 
 
91
  for chunk in client.models.generate_content_stream(
92
  model=model,
93
  contents=contents,
94
  config=generate_content_config,
95
  ):
96
- print(chunk.text, end="")
97
-
98
 
99
  if __name__ == '__main__':
100
-
101
  with gr.Blocks() as demo:
102
- title=gr.Markdown("# Gemini 2.0 Flash + Websearch")
103
  output_textbox = gr.Markdown()
104
- input_textbox = gr.Textbox(lines=3, label="", placeholder="Enter event details here...")
105
- submit_button = gr.Button("send")
106
- submit_button.click(fn=generate,inputs=input_textbox,outputs=[output_textbox, input_textbox])
 
 
 
 
 
 
 
107
  demo.launch(show_error=True)
 
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
  try:
33
  client = genai.Client(
34
  api_key=os.environ.get("GEMINI_API_KEY"),
35
  )
36
+
37
+ model = "gemini-2.0-flash" # Hinweis: gemini-3 existiert aktuell noch nicht
 
 
38
  contents = [
39
  types.Content(
40
  role="user",
41
  parts=[
42
+ types.Part.from_text(text=input_text),
43
  ],
44
  ),
45
  ]
46
  tools = [
47
+ types.Tool(google_search=types.GoogleSearch()),
 
48
  ]
49
  generate_content_config = types.GenerateContentConfig(
50
  temperature=0.35,
51
+ # thinking_config ist aktuell nur für spezielle Modelle verfügbar
52
+ # thinking_config=types.ThinkingConfig(thinking_level="HIGH"),
 
53
  tools=tools,
54
  )
55
 
56
+ full_response = ""
57
  for chunk in client.models.generate_content_stream(
58
  model=model,
59
  contents=contents,
60
  config=generate_content_config,
61
  ):
62
+ if chunk.text:
63
+ full_response += chunk.text
64
+ # Wir geben zwei Werte zurück, da Gradio zwei Outputs erwartet
65
+ yield full_response, ""
66
+
67
+ except Exception as e:
68
+ yield f"Error: {e}", input_text
69
 
70
 
71
  def generate1(input_text):
 
73
  api_key=os.environ.get("GEMINI_API_KEY"),
74
  )
75
 
76
+ model = "gemini-2.0-flash"
77
  contents = [
78
  types.Content(
79
  role="user",
80
  parts=[
81
+ types.Part.from_text(text=f"return json object with keys name and email. name = {input_text} search the web for the email value. return json object only, no additional text or comments"),
82
  ],
83
  ),
84
  ]
85
  tools = [
86
+ types.Tool(google_search_retrieval=types.DynamicRetrievalConfig(dynamic_threshold=0.3, mode="MODE_DYNAMIC")),
87
  ]
88
  generate_content_config = types.GenerateContentConfig(
89
  temperature=0.45,
 
91
  response_mime_type="text/plain",
92
  )
93
 
94
+ full_response = ""
95
  for chunk in client.models.generate_content_stream(
96
  model=model,
97
  contents=contents,
98
  config=generate_content_config,
99
  ):
100
+ full_response += chunk.text
101
+ yield full_response
102
 
103
  if __name__ == '__main__':
 
104
  with gr.Blocks() as demo:
105
+ gr.Markdown("# Gemini 2.0 Flash + Websearch")
106
  output_textbox = gr.Markdown()
107
+ input_textbox = gr.Textbox(lines=3, label="Input", placeholder="Enter event details here...")
108
+ submit_button = gr.Button("Send")
109
+
110
+ # Die Funktion generate gibt nun (Response, ClearInput) zurück
111
+ submit_button.click(
112
+ fn=generate,
113
+ inputs=input_textbox,
114
+ outputs=[output_textbox, input_textbox]
115
+ )
116
+
117
  demo.launch(show_error=True)