mgokg commited on
Commit
ea1b137
·
verified ·
1 Parent(s): b19448f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -41
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
- # Import MCP client for Python (standard in 2026)
7
- from mcp import Client as MCPClient
 
 
8
 
9
  async def generate(input_text):
10
  try:
11
- # Initialize Gemini Client
12
- client = genai.Client(
13
- api_key=os.environ.get("GEMINI_API_KEY"),
14
- )
15
 
16
- # 1. Connect to the Streamable HTTP MCP server on Hugging Face
17
- # 2026 SDKs support direct URL initialization
18
- mcp_url = "https://mgokg-db-timetable-api.hf.space/gradio_api/mcp/"
19
 
20
- async with MCPClient(url=mcp_url) as mcp_session:
21
-
22
- model = "gemini-2.5-flash" # Version 2026
23
-
24
- # 2. Define tools: Google Search + MCP Session
25
- tools = [
26
- types.Tool(google_search=types.GoogleSearch()),
27
- mcp_session # Pass the entire MCP server as a toolset here
28
- ]
29
-
30
- generate_content_config = types.GenerateContentConfig(
31
- temperature=0.4,
32
- tools=tools,
33
- response_mime_type="text/plain",
34
- )
 
 
 
35
 
36
- response_text = ""
37
- # 3. Stream call with MCP tools
38
- async for chunk in client.aio.models.generate_content_stream(
39
- model=model,
40
- contents=input_text,
41
- config=generate_content_config,
42
- ):
43
- if chunk.text:
44
- response_text += chunk.text
45
-
46
- return response_text, ""
47
 
48
  except Exception as e:
49
- return f"Error during processing: {str(e)}", ""
50
 
51
- # Helper function for asynchronous Gradio calls
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="Request",
60
- placeholder="When is the next train from Berlin to Hamburg?")
61
- submit_button = gr.Button("Send Request")
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,