Update app.py
Browse files
app.py
CHANGED
|
@@ -4,28 +4,28 @@ import gradio as gr
|
|
| 4 |
from google import genai
|
| 5 |
from google.genai import types
|
| 6 |
|
| 7 |
-
#
|
| 8 |
from mcp import ClientSession
|
| 9 |
-
from mcp.client.
|
| 10 |
|
| 11 |
async def generate(input_text):
|
| 12 |
try:
|
| 13 |
-
# Gemini Client
|
| 14 |
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
| 15 |
|
| 16 |
-
# MCP
|
| 17 |
mcp_url = "https://mgokg-db-timetable-api.hf.space"
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
async with
|
| 21 |
-
async with ClientSession(
|
| 22 |
-
#
|
| 23 |
await mcp_session.initialize()
|
| 24 |
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
# Das SDK erkennt die Tools automatisch aus der mcp_session
|
| 29 |
generate_content_config = types.GenerateContentConfig(
|
| 30 |
temperature=0.4,
|
| 31 |
tools=[
|
|
@@ -36,7 +36,6 @@ async def generate(input_text):
|
|
| 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,
|
|
@@ -48,19 +47,18 @@ async def generate(input_text):
|
|
| 48 |
return response_text, ""
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
-
return f"
|
| 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 (
|
| 60 |
output_textbox = gr.Markdown()
|
| 61 |
-
input_textbox = gr.Textbox(lines=3, label="
|
| 62 |
-
placeholder="
|
| 63 |
-
submit_button = gr.Button("
|
| 64 |
|
| 65 |
submit_button.click(
|
| 66 |
fn=gradio_wrapper,
|
|
|
|
| 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=[
|
|
|
|
| 36 |
)
|
| 37 |
|
| 38 |
response_text = ""
|
|
|
|
| 39 |
async for chunk in client.aio.models.generate_content_stream(
|
| 40 |
model=model,
|
| 41 |
contents=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,
|