Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,58 @@
|
|
|
|
|
| 1 |
import asyncio
|
| 2 |
-
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
async def run_mcp_client():
|
| 6 |
-
# Die Basis-URL deines Hugging Face Spaces (mit /sse Endpunkt für das Python SDK)
|
| 7 |
-
server_url = "https://mgokg-db-timetable-api.hf.space"
|
| 8 |
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
async with
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
# --- Beispiel 1: Verfügbare Tools auflisten ---
|
| 20 |
-
print("Verfügbare Tools:")
|
| 21 |
-
tools_result = await session.list_tools()
|
| 22 |
-
for tool in tools_result.tools:
|
| 23 |
-
print(f"- {tool.name}: {tool.description}")
|
| 24 |
|
| 25 |
-
|
| 26 |
-
# Ersetze 'get_timetable' und die Argumente durch die echten Tool-Namen deiner API
|
| 27 |
-
if tools_result.tools:
|
| 28 |
-
tool_to_call = tools_result.tools[0].name
|
| 29 |
-
print(f"\nRufe Tool '{tool_to_call}' auf...")
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
)
|
| 36 |
-
print(f"Ergebnis: {result.content}")
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
except Exception as e:
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import asyncio
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from google import genai
|
| 5 |
+
from google.genai import types
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
from mcp import ClientSession
|
| 8 |
+
from mcp.client.sse import sse_client # Spezifischer Transport für Gradio/HF
|
| 9 |
|
| 10 |
+
async def generate(input_text):
|
| 11 |
+
# WICHTIG: Gradio MCP Server benötigen oft das Suffix /sse
|
| 12 |
+
mcp_url = "https://mgokg-db-timetable-api.hf.space"
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
| 16 |
|
| 17 |
+
# SSE Transport nutzen, um den 'text/html' Fehler zu vermeiden
|
| 18 |
+
async with sse_client(url=mcp_url) as (read_stream, write_stream):
|
| 19 |
+
async with ClientSession(read_stream, write_stream) as mcp_session:
|
| 20 |
+
await mcp_session.initialize()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
model_id = "gemini-2.0-flash"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
generate_content_config = types.GenerateContentConfig(
|
| 25 |
+
temperature=0.4,
|
| 26 |
+
tools=[
|
| 27 |
+
types.Tool(google_search=types.GoogleSearch()),
|
| 28 |
+
mcp_session # Reicht die Tools des DB-Servers an Gemini durch
|
| 29 |
+
],
|
| 30 |
)
|
|
|
|
| 31 |
|
| 32 |
+
response_text = ""
|
| 33 |
+
async for chunk in client.aio.models.generate_content_stream(
|
| 34 |
+
model=model_id,
|
| 35 |
+
contents=input_text,
|
| 36 |
+
config=generate_content_config,
|
| 37 |
+
):
|
| 38 |
+
if chunk.text:
|
| 39 |
+
response_text += chunk.text
|
| 40 |
+
|
| 41 |
+
return response_text, ""
|
| 42 |
+
|
| 43 |
except Exception as e:
|
| 44 |
+
return f"Verbindung zum DB-Fahrplan fehlgeschlagen: {str(e)}", ""
|
| 45 |
+
|
| 46 |
+
def gradio_wrapper(input_text):
|
| 47 |
+
return asyncio.run(generate(input_text))
|
| 48 |
+
|
| 49 |
+
if __name__ == '__main__':
|
| 50 |
+
with gr.Blocks() as demo:
|
| 51 |
+
gr.Markdown("# Gemini Flash + DB Timetable")
|
| 52 |
+
input_tx = gr.Textbox(label="Anfrage", placeholder="Wann fährt der nächste Zug von Berlin nach Hamburg?")
|
| 53 |
+
btn = gr.Button("Senden")
|
| 54 |
+
output_md = gr.Markdown()
|
| 55 |
+
|
| 56 |
+
btn.click(fn=gradio_wrapper, inputs=input_tx, outputs=[output_md, input_tx])
|
| 57 |
+
|
| 58 |
+
demo.launch()
|