Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import base64
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
|
@@ -62,3 +175,4 @@ if __name__ == '__main__':
|
|
| 62 |
submit_button = gr.Button("send")
|
| 63 |
submit_button.click(fn=generate,inputs=input_textbox,outputs=[output_textbox, input_textbox])
|
| 64 |
demo.launch(show_error=True)
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import asyncio
|
| 4 |
+
from typing import List
|
| 5 |
+
from google import genai
|
| 6 |
+
from google.genai import types
|
| 7 |
+
from mcp import ClientSession, StdioServerParameters
|
| 8 |
+
from mcp.client.stdio import stdio_client
|
| 9 |
+
|
| 10 |
+
# Konfiguration des MCP-Servers für die DB-Fahrplanauskunft [1, 3]
|
| 11 |
+
server_params = StdioServerParameters(
|
| 12 |
+
command="npx",
|
| 13 |
+
args=[
|
| 14 |
+
"mcp-remote",
|
| 15 |
+
"https://mgokg-db-timetable-api.hf.space/gradio_api/mcp/",
|
| 16 |
+
"--transport",
|
| 17 |
+
"streamable-http"
|
| 18 |
+
]
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
async def generate(input_text):
|
| 22 |
+
try:
|
| 23 |
+
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return f"Error initializing client: {e}", ""
|
| 26 |
+
|
| 27 |
+
model = "gemini-2.0-flash" # Empfohlen für Tool-Calling [6]
|
| 28 |
+
|
| 29 |
+
# Aufbau der Verbindung zum MCP-Server [9, 10]
|
| 30 |
+
async with stdio_client(server_params) as (read, write):
|
| 31 |
+
async with ClientSession(read, write) as session:
|
| 32 |
+
await session.initialize()
|
| 33 |
+
|
| 34 |
+
# MCP-Tools abrufen und für Gemini konvertieren [9, 11]
|
| 35 |
+
mcp_tools_list = await session.list_tools()
|
| 36 |
+
mcp_declarations = [
|
| 37 |
+
{
|
| 38 |
+
"name": tool.name,
|
| 39 |
+
"description": tool.description or "Get train connections between stations",
|
| 40 |
+
"parameters": tool.inputSchema,
|
| 41 |
+
}
|
| 42 |
+
for tool in mcp_tools_list.tools
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
# Kombinierte Tools: Google Search + MCP Tools [2, 7]
|
| 46 |
+
tools = [
|
| 47 |
+
types.Tool(google_search=types.GoogleSearch()),
|
| 48 |
+
types.Tool(function_declarations=mcp_declarations)
|
| 49 |
+
]
|
| 50 |
+
|
| 51 |
+
contents = [types.Content(role="user", parts=[types.Part.from_text(text=input_text)])]
|
| 52 |
+
|
| 53 |
+
# Initialer Aufruf
|
| 54 |
+
response = await client.aio.models.generate_content(
|
| 55 |
+
model=model,
|
| 56 |
+
contents=contents,
|
| 57 |
+
config=types.GenerateContentConfig(
|
| 58 |
+
tools=tools,
|
| 59 |
+
temperature=0.4
|
| 60 |
+
)
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
contents.append(response.candidates.content)
|
| 64 |
+
|
| 65 |
+
# Tool Calling Loop (für Fahrplandaten oder Google Search) [7, 8]
|
| 66 |
+
turn_count = 0
|
| 67 |
+
while response.function_calls and turn_count < 5:
|
| 68 |
+
turn_count += 1
|
| 69 |
+
tool_responses = []
|
| 70 |
+
|
| 71 |
+
for fc in response.function_calls:
|
| 72 |
+
# Ausführung der MCP-Tools (z.B. db_timetable_api_ui_wrapper) [8, 12]
|
| 73 |
+
try:
|
| 74 |
+
tool_result = await session.call_tool(fc.name, fc.args)
|
| 75 |
+
result_text = tool_result.content.text if not tool_result.isError else tool_result.content.text
|
| 76 |
+
tool_responses.append(types.Part.from_function_response(
|
| 77 |
+
name=fc.name, response={"result": result_text}
|
| 78 |
+
))
|
| 79 |
+
except Exception as e:
|
| 80 |
+
tool_responses.append(types.Part.from_function_response(
|
| 81 |
+
name=fc.name, response={"error": str(e)}
|
| 82 |
+
))
|
| 83 |
+
|
| 84 |
+
contents.append(types.Content(role="user", parts=tool_responses))
|
| 85 |
+
response = await client.aio.models.generate_content(
|
| 86 |
+
model=model, contents=contents, config=types.GenerateContentConfig(tools=tools)
|
| 87 |
+
)
|
| 88 |
+
contents.append(response.candidates.content)
|
| 89 |
+
|
| 90 |
+
return response.text, ""
|
| 91 |
+
|
| 92 |
+
# Gradio UI (Original Funktionalität und Design beibehalten) [13]
|
| 93 |
+
def ui_wrapper(input_text):
|
| 94 |
+
return asyncio.run(generate(input_text))
|
| 95 |
+
|
| 96 |
+
if __name__ == '__main__':
|
| 97 |
+
with gr.Blocks() as demo:
|
| 98 |
+
gr.Markdown("# Gemini 2.0 Flash + Websearch + DB Timetable (MCP)")
|
| 99 |
+
output_textbox = gr.Markdown()
|
| 100 |
+
input_textbox = gr.Textbox(lines=3, label="", placeholder="Frage nach einer Zugverbindung (z.B. Berlin nach München)...")
|
| 101 |
+
submit_button = gr.Button("Senden")
|
| 102 |
+
|
| 103 |
+
submit_button.click(
|
| 104 |
+
fn=ui_wrapper,
|
| 105 |
+
inputs=input_textbox,
|
| 106 |
+
outputs=[output_textbox, input_textbox]
|
| 107 |
+
)
|
| 108 |
+
demo.launch(show_error=True)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
"""
|
| 114 |
import base64
|
| 115 |
import gradio as gr
|
| 116 |
import os
|
|
|
|
| 175 |
submit_button = gr.Button("send")
|
| 176 |
submit_button.click(fn=generate,inputs=input_textbox,outputs=[output_textbox, input_textbox])
|
| 177 |
demo.launch(show_error=True)
|
| 178 |
+
"""
|