mgokg commited on
Commit
d09e41a
·
verified ·
1 Parent(s): 5b7df10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -26
app.py CHANGED
@@ -4,40 +4,41 @@ import gradio as gr
4
  from google import genai
5
  from google.genai import types
6
 
7
- # Correct imports for the MCP Python SDK 2026
8
  from mcp import ClientSession
9
  from mcp.client.streamable_http import streamablehttp_client
10
 
11
  async def generate(input_text):
 
 
 
12
  try:
13
- # Initialize Gemini Client
14
  client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
15
 
16
- # MCP URL for Hugging Face
17
- mcp_url = "https://mgokg-db-timetable-api.hf.space"
18
-
19
- # Using the streamablehttp_client transport
20
  async with streamablehttp_client(mcp_url) as (read_stream, write_stream):
21
  async with ClientSession(read_stream, write_stream) as mcp_session:
22
- # Initialization of the protocol
23
  await mcp_session.initialize()
24
-
25
- # Gemini 2.0/2.5 Flash Model (2026 Standard)
26
- model = "gemini-2.5-flash"
27
 
28
- # Gemini can use the mcp_session directly as a tool
 
 
 
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
  async for chunk in client.aio.models.generate_content_stream(
40
- model=model,
41
  contents=input_text,
42
  config=generate_content_config,
43
  ):
@@ -47,23 +48,21 @@ async def generate(input_text):
47
  return response_text, ""
48
 
49
  except Exception as e:
50
- return f"Error: {str(e)}", ""
 
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="Query",
60
- placeholder="When is the next train from Berlin to Hamburg?")
61
- submit_button = gr.Button("Submit")
62
 
63
- submit_button.click(
64
- fn=gradio_wrapper,
65
- inputs=input_textbox,
66
- outputs=[output_textbox, input_textbox]
67
- )
68
 
69
- demo.launch(show_error=True)
 
4
  from google import genai
5
  from google.genai import types
6
 
7
+ # Korrekte Import-Struktur für 2026
8
  from mcp import ClientSession
9
  from mcp.client.streamable_http import streamablehttp_client
10
 
11
  async def generate(input_text):
12
+ # MCP Server URL von Hugging Face
13
+ mcp_url = "https://mgokg-db-timetable-api.hf.space"
14
+
15
  try:
16
+ # Gemini Client initialisieren
17
  client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
18
 
19
+ # Verbindung über den Streamable HTTP Transport
20
+ # Wir nutzen eine explizite Fehlerbehandlung für den Netzwerk-Stream
 
 
21
  async with streamablehttp_client(mcp_url) as (read_stream, write_stream):
22
  async with ClientSession(read_stream, write_stream) as mcp_session:
23
+ # WICHTIG: Handshake initialisieren
24
  await mcp_session.initialize()
 
 
 
25
 
26
+ # Gemini Modellkonfiguration
27
+ # Gemini kann direkt auf mcp_session zugreifen, um Tools zu extrahieren
28
+ model_id = "gemini-2.0-flash"
29
+
30
  generate_content_config = types.GenerateContentConfig(
31
  temperature=0.4,
32
  tools=[
33
  types.Tool(google_search=types.GoogleSearch()),
34
+ mcp_session # Übergabe der aktiven MCP-Sitzung
35
  ],
 
36
  )
37
 
38
  response_text = ""
39
+ # Streaming der Antwort
40
  async for chunk in client.aio.models.generate_content_stream(
41
+ model=model_id,
42
  contents=input_text,
43
  config=generate_content_config,
44
  ):
 
48
  return response_text, ""
49
 
50
  except Exception as e:
51
+ # Detaillierte Fehlerausgabe für TaskGroup-Fehler
52
+ return f"Verbindungsfehler zum MCP-Server oder Gemini: {str(e)}", ""
53
 
54
  def gradio_wrapper(input_text):
55
+ # Führt die asynchrone Logik im synchronen Gradio-Kontext aus
56
  return asyncio.run(generate(input_text))
57
 
58
  if __name__ == '__main__':
59
  with gr.Blocks() as demo:
60
+ gr.Markdown("# Gemini Flash + DB Timetable (Hugging Face MCP)")
61
+ with gr.Row():
62
+ input_tx = gr.Textbox(label="Anfrage", placeholder="Nächster Zug von Berlin nach Hamburg?")
63
+ btn = gr.Button("Senden")
64
+ output_md = gr.Markdown()
65
 
66
+ btn.click(fn=gradio_wrapper, inputs=input_tx, outputs=[output_md, input_tx])
 
 
 
 
67
 
68
+ demo.launch()