Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import asyncio
|
| 3 |
import gradio as gr
|
|
@@ -55,4 +162,5 @@ if __name__ == '__main__':
|
|
| 55 |
|
| 56 |
btn.click(fn=gradio_wrapper, inputs=input_tx, outputs=[output_md, input_tx])
|
| 57 |
|
| 58 |
-
demo.launch()
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
import json
|
| 5 |
+
from google import genai
|
| 6 |
+
from google.genai import types
|
| 7 |
+
from gradio_client import Client
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
route="""
|
| 11 |
+
how to handle special case "zugverbindung".
|
| 12 |
+
Wichtig: Dies Regeln gelten nur wenn eine zugverbindung angefragt wird, else answer prompt
|
| 13 |
+
Regeln:
|
| 14 |
+
Wenn eine Zugverbindung von {Startort} nach {Zielort} angefragt wird, return json object with Startort and Zielort.
|
| 15 |
+
always follow json scheme below.
|
| 16 |
+
|
| 17 |
+
Wichtig: Gib absolut keinen Text vor oder nach dem JSON aus (keine Erklärungen, kein "Hier ist das Ergebnis").
|
| 18 |
+
|
| 19 |
+
{
|
| 20 |
+
"start_loc": "fill in Startort here",
|
| 21 |
+
"dest_loc": "fill in Zielort here"
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
def clean_json_string(json_str):
|
| 27 |
+
"""
|
| 28 |
+
Removes any comments or prefixes before the actual JSON content.
|
| 29 |
+
"""
|
| 30 |
+
# Find the first occurrence of '{'
|
| 31 |
+
json_start = json_str.find('{')
|
| 32 |
+
if json_start == -1:
|
| 33 |
+
# If no '{' is found, try with '[' for arrays
|
| 34 |
+
json_start = json_str.find('[')
|
| 35 |
+
if json_start == -1:
|
| 36 |
+
return json_str # Return original if no JSON markers found
|
| 37 |
+
|
| 38 |
+
# Extract everything from the first JSON marker
|
| 39 |
+
cleaned_str = json_str[json_start:]
|
| 40 |
+
return cleaned_str
|
| 41 |
+
# Verify it's valid JSON
|
| 42 |
+
try:
|
| 43 |
+
json.loads(cleaned_str)
|
| 44 |
+
return cleaned_str
|
| 45 |
+
except json.JSONDecodeError:
|
| 46 |
+
return json_str # Return original if cleaning results in invalid JSON
|
| 47 |
+
|
| 48 |
+
def generate(input_text):
|
| 49 |
+
try:
|
| 50 |
+
client = genai.Client(
|
| 51 |
+
api_key=os.environ.get("GEMINI_API_KEY"),
|
| 52 |
+
)
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return f"Error initializing client: {e}. Make sure GEMINI_API_KEY is set."
|
| 55 |
+
|
| 56 |
+
model = "gemini-flash-latest"
|
| 57 |
+
contents = [
|
| 58 |
+
types.Content(
|
| 59 |
+
role="user",
|
| 60 |
+
parts=[
|
| 61 |
+
types.Part.from_text(text=f"{input_text}"),
|
| 62 |
+
],
|
| 63 |
+
),
|
| 64 |
+
]
|
| 65 |
+
tools = [
|
| 66 |
+
types.Tool(google_search=types.GoogleSearch()),
|
| 67 |
+
]
|
| 68 |
+
generate_content_config = types.GenerateContentConfig(
|
| 69 |
+
temperature=0.4,
|
| 70 |
+
thinking_config = types.ThinkingConfig(
|
| 71 |
+
thinking_budget=0,
|
| 72 |
+
),
|
| 73 |
+
tools=tools,
|
| 74 |
+
response_mime_type="text/plain",
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
response_text = ""
|
| 79 |
+
try:
|
| 80 |
+
for chunk in client.models.generate_content_stream(
|
| 81 |
+
model=model,
|
| 82 |
+
contents=contents,
|
| 83 |
+
config=generate_content_config,
|
| 84 |
+
):
|
| 85 |
+
response_text += chunk.text
|
| 86 |
+
except Exception as e:
|
| 87 |
+
return f"Error during generation: {e}"
|
| 88 |
+
data = clean_json_string(response_text)
|
| 89 |
+
data = data[:-1]
|
| 90 |
+
return response_text, ""
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
if __name__ == '__main__':
|
| 94 |
+
|
| 95 |
+
with gr.Blocks() as demo:
|
| 96 |
+
title=gr.Markdown("# Gemini 2.0 Flash + Websearch")
|
| 97 |
+
output_textbox = gr.Markdown()
|
| 98 |
+
input_textbox = gr.Textbox(lines=3, label="", placeholder="Enter message here...")
|
| 99 |
+
submit_button = gr.Button("send")
|
| 100 |
+
submit_button.click(fn=generate,inputs=input_textbox,outputs=[output_textbox, input_textbox])
|
| 101 |
+
demo.launch(show_error=True)
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
"""
|
| 108 |
import os
|
| 109 |
import asyncio
|
| 110 |
import gradio as gr
|
|
|
|
| 162 |
|
| 163 |
btn.click(fn=gradio_wrapper, inputs=input_tx, outputs=[output_md, input_tx])
|
| 164 |
|
| 165 |
+
demo.launch()
|
| 166 |
+
"""
|