File size: 3,459 Bytes
2f3a65e 707349c 5ce25db 707349c c22a55f 707349c ce057fa 707349c c4b358c 707349c 6cc9f59 707349c 6cc9f59 707349c 6cc9f59 707349c e875770 cf2c477 5ce25db e875770 cf2c477 e875770 cf2c477 e875770 707349c 38bf0de 707349c d8ac644 4930af4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
import base64
import gradio as gr
import os
import json
from google import genai
from google.genai import types
from gradio_client import Client
def clean_json_string(json_str):
"""
Removes any comments or prefixes before the actual JSON content.
"""
# Find the first occurrence of '{'
json_start = json_str.find('{')
if json_start == -1:
# If no '{' is found, try with '[' for arrays
json_start = json_str.find('[')
if json_start == -1:
return json_str # Return original if no JSON markers found
# Extract everything from the first JSON marker
cleaned_str = json_str[json_start:]
return cleaned_str
# Verify it's valid JSON
try:
json.loads(cleaned_str)
return cleaned_str
except json.JSONDecodeError:
return json_str # Return original if cleaning results in invalid JSON
def generate(input_text):
try:
client = genai.Client(
api_key=os.environ.get("GEMINI_API_KEY"),
)
except Exception as e:
return f"Error initializing client: {e}. Make sure GEMINI_API_KEY is set."
model = "gemini-2.0-flash"
contents = [
types.Content(
role="user",
parts=[
types.Part.from_text(text=input_text),
],
),
]
tools = [
types.Tool(google_search=types.GoogleSearch()),
]
generate_content_config = types.GenerateContentConfig(
temperature=0.4,
thinking_config = types.ThinkingConfig(
thinking_budget=0,
),
tools=tools,
response_mime_type="text/plain",
)
response_text = ""
try:
for chunk in client.models.generate_content_stream(
model=model,
contents=contents,
config=generate_content_config,
):
response_text += chunk.text
except Exception as e:
return f"Error during generation: {e}"
data = clean_json_string(response_text)
data = data[:-1]
return response_text, ""
def generate1(input_text):
client = genai.Client(
api_key=os.environ.get("GEMINI_API_KEY"),
)
model = "gemini-1.5-pro"
contents = [
types.Content(
role="user",
parts=[
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"),
],
),
]
tools = [
types.Tool(googleSearchRetrieval=types.DynamicRetrievalConfig(dynamicThreshold=0.3, mode=types.DynamicRetrievalConfigMode.MODE_DYNAMIC)),
]
generate_content_config = types.GenerateContentConfig(
temperature=0.45,
tools=tools,
response_mime_type="text/plain",
)
for chunk in client.models.generate_content_stream(
model=model,
contents=contents,
config=generate_content_config,
):
print(chunk.text, end="")
if __name__ == '__main__':
with gr.Blocks() as demo:
title=gr.Markdown("# Gemini 2.0 Flash + Websearch")
output_textbox = gr.Markdown()
input_textbox = gr.Textbox(lines=3, label="", placeholder="Enter event details here...")
submit_button = gr.Button("send")
submit_button.click(fn=generate,inputs=input_textbox,outputs=[output_textbox, input_textbox])
demo.launch(show_error=True) |