Update app.py
Browse files
app.py
CHANGED
|
@@ -3,62 +3,64 @@ import asyncio
|
|
| 3 |
import gradio as gr
|
| 4 |
from google import genai
|
| 5 |
from google.genai import types
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
async def generate(input_text):
|
| 10 |
try:
|
| 11 |
-
#
|
| 12 |
-
client = genai.Client(
|
| 13 |
-
api_key=os.environ.get("GEMINI_API_KEY"),
|
| 14 |
-
)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
mcp_url = "https://mgokg-db-timetable-api.hf.space/gradio_api/mcp/"
|
| 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 |
except Exception as e:
|
| 49 |
-
return f"
|
| 50 |
|
| 51 |
-
#
|
| 52 |
def gradio_wrapper(input_text):
|
| 53 |
return asyncio.run(generate(input_text))
|
| 54 |
|
| 55 |
if __name__ == '__main__':
|
| 56 |
with gr.Blocks() as demo:
|
| 57 |
-
gr.Markdown("# Gemini Flash + DB Timetable (MCP)")
|
| 58 |
output_textbox = gr.Markdown()
|
| 59 |
-
input_textbox = gr.Textbox(lines=3, label="
|
| 60 |
-
placeholder="
|
| 61 |
-
submit_button = gr.Button("
|
| 62 |
|
| 63 |
submit_button.click(
|
| 64 |
fn=gradio_wrapper,
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from google import genai
|
| 5 |
from google.genai import types
|
| 6 |
+
|
| 7 |
+
# Korrekte Importe für das MCP Python SDK 2026
|
| 8 |
+
from mcp import ClientSession
|
| 9 |
+
from mcp.client.http import HttpClientTransport
|
| 10 |
|
| 11 |
async def generate(input_text):
|
| 12 |
try:
|
| 13 |
+
# Gemini Client initialisieren
|
| 14 |
+
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# MCP Konfiguration für Hugging Face (Streamable HTTP)
|
| 17 |
+
mcp_url = "https://mgokg-db-timetable-api.hf.space"
|
|
|
|
| 18 |
|
| 19 |
+
# Verbindung über HTTP Transport herstellen
|
| 20 |
+
async with HttpClientTransport(mcp_url) as transport:
|
| 21 |
+
async with ClientSession(transport.read, transport.write) as mcp_session:
|
| 22 |
+
# Initialisierung des MCP-Protokolls
|
| 23 |
+
await mcp_session.initialize()
|
| 24 |
+
|
| 25 |
+
model = "gemini-2.0-flash"
|
| 26 |
+
|
| 27 |
+
# Tools definieren: Google Search + die Session des MCP Servers
|
| 28 |
+
# Das SDK erkennt die Tools automatisch aus der mcp_session
|
| 29 |
+
generate_content_config = types.GenerateContentConfig(
|
| 30 |
+
temperature=0.4,
|
| 31 |
+
tools=[
|
| 32 |
+
types.Tool(google_search=types.GoogleSearch()),
|
| 33 |
+
mcp_session
|
| 34 |
+
],
|
| 35 |
+
response_mime_type="text/plain",
|
| 36 |
+
)
|
| 37 |
|
| 38 |
+
response_text = ""
|
| 39 |
+
# Streaming-Abfrage an Gemini
|
| 40 |
+
async for chunk in client.aio.models.generate_content_stream(
|
| 41 |
+
model=model,
|
| 42 |
+
contents=input_text,
|
| 43 |
+
config=generate_content_config,
|
| 44 |
+
):
|
| 45 |
+
if chunk.text:
|
| 46 |
+
response_text += chunk.text
|
| 47 |
+
|
| 48 |
+
return response_text, ""
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
+
return f"Fehler: {str(e)}", ""
|
| 52 |
|
| 53 |
+
# Hilfsfunktion für den asynchronen Aufruf in Gradio
|
| 54 |
def gradio_wrapper(input_text):
|
| 55 |
return asyncio.run(generate(input_text))
|
| 56 |
|
| 57 |
if __name__ == '__main__':
|
| 58 |
with gr.Blocks() as demo:
|
| 59 |
+
gr.Markdown("# Gemini Flash + DB Timetable (Hugging Face MCP)")
|
| 60 |
output_textbox = gr.Markdown()
|
| 61 |
+
input_textbox = gr.Textbox(lines=3, label="Anfrage",
|
| 62 |
+
placeholder="Wann fährt der nächste Zug von München nach Hamburg?")
|
| 63 |
+
submit_button = gr.Button("Anfrage senden")
|
| 64 |
|
| 65 |
submit_button.click(
|
| 66 |
fn=gradio_wrapper,
|