Update app.py
Browse files
app.py
CHANGED
|
@@ -9,34 +9,27 @@ 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
|
|
|
|
| 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,
|
|
@@ -48,11 +41,9 @@ async def generate(input_text):
|
|
| 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__':
|
|
@@ -65,4 +56,4 @@ if __name__ == '__main__':
|
|
| 65 |
|
| 66 |
btn.click(fn=gradio_wrapper, inputs=input_tx, outputs=[output_md, input_tx])
|
| 67 |
|
| 68 |
-
demo.launch()
|
|
|
|
| 9 |
from mcp.client.streamable_http import streamablehttp_client
|
| 10 |
|
| 11 |
async def generate(input_text):
|
|
|
|
| 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 |
async with streamablehttp_client(mcp_url) as (read_stream, write_stream):
|
| 18 |
async with ClientSession(read_stream, write_stream) as mcp_session:
|
|
|
|
| 19 |
await mcp_session.initialize()
|
| 20 |
|
|
|
|
|
|
|
| 21 |
model_id = "gemini-2.0-flash"
|
| 22 |
|
| 23 |
generate_content_config = types.GenerateContentConfig(
|
| 24 |
temperature=0.4,
|
| 25 |
tools=[
|
| 26 |
types.Tool(google_search=types.GoogleSearch()),
|
| 27 |
+
# Überprüfen, ob mcp_session hier korrekt verwendet wird
|
| 28 |
+
types.Tool(mcp_session=mcp_session) # Beispiel für die korrekte Verwendung
|
| 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,
|
|
|
|
| 41 |
return response_text, ""
|
| 42 |
|
| 43 |
except Exception as e:
|
|
|
|
| 44 |
return f"Verbindungsfehler zum MCP-Server oder Gemini: {str(e)}", ""
|
| 45 |
|
| 46 |
def gradio_wrapper(input_text):
|
|
|
|
| 47 |
return asyncio.run(generate(input_text))
|
| 48 |
|
| 49 |
if __name__ == '__main__':
|
|
|
|
| 56 |
|
| 57 |
btn.click(fn=gradio_wrapper, inputs=input_tx, outputs=[output_md, input_tx])
|
| 58 |
|
| 59 |
+
demo.launch()
|