Update app.py
Browse files
app.py
CHANGED
|
@@ -8,8 +8,7 @@ from mcp import ClientSession
|
|
| 8 |
from mcp.client.sse import sse_client
|
| 9 |
|
| 10 |
# --- CONFIGURATION ---
|
| 11 |
-
MCP_SERVER_URL = "https://mgokg-db-api
|
| 12 |
-
# Ensure your API key is set in your environment variables
|
| 13 |
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 14 |
|
| 15 |
async def call_mcp_tool(start_loc, dest_loc):
|
|
@@ -24,16 +23,15 @@ async def call_mcp_tool(start_loc, dest_loc):
|
|
| 24 |
return result.content[0].text
|
| 25 |
|
| 26 |
async def generate(input_text):
|
| 27 |
-
"""Async generator
|
| 28 |
if not GEMINI_API_KEY:
|
| 29 |
yield "### Error\nGEMINI_API_KEY is not set.", ""
|
| 30 |
return
|
| 31 |
|
|
|
|
| 32 |
client = genai.Client(api_key=GEMINI_API_KEY)
|
| 33 |
-
#
|
| 34 |
-
model_id = "gemini-flash-latest"
|
| 35 |
|
| 36 |
-
# Define the train tool schema
|
| 37 |
train_tool = types.Tool(
|
| 38 |
function_declarations=[
|
| 39 |
types.FunctionDeclaration(
|
|
@@ -59,13 +57,16 @@ async def generate(input_text):
|
|
| 59 |
temperature=0.3
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
# --- Manual Tool Loop ---
|
| 66 |
max_turns = 5
|
| 67 |
for _ in range(max_turns):
|
| 68 |
-
if not response.candidates[0].content.parts:
|
| 69 |
break
|
| 70 |
|
| 71 |
tool_calls = [p.tool_call for p in response.candidates[0].content.parts if p.tool_call]
|
|
@@ -75,9 +76,8 @@ async def generate(input_text):
|
|
| 75 |
tool_responses = []
|
| 76 |
for call in tool_calls:
|
| 77 |
if call.name == "get_train_connections":
|
| 78 |
-
yield f"π Fetching train data
|
| 79 |
|
| 80 |
-
# Execute the MCP call
|
| 81 |
train_data = await call_mcp_tool(call.args["start_loc"], call.args["dest_loc"])
|
| 82 |
|
| 83 |
tool_responses.append(
|
|
@@ -88,12 +88,12 @@ async def generate(input_text):
|
|
| 88 |
)
|
| 89 |
|
| 90 |
if tool_responses:
|
| 91 |
-
yield "π
|
| 92 |
-
|
|
|
|
| 93 |
else:
|
| 94 |
break
|
| 95 |
|
| 96 |
-
# Final output
|
| 97 |
yield response.text, ""
|
| 98 |
|
| 99 |
except Exception as e:
|
|
@@ -101,19 +101,12 @@ async def generate(input_text):
|
|
| 101 |
|
| 102 |
# --- GRADIO UI ---
|
| 103 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 104 |
-
gr.Markdown("# π Gemini
|
| 105 |
-
gr.Markdown("I search the web and check live train connections via an MCP Server.")
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
lines=3,
|
| 111 |
-
label="Ask me anything",
|
| 112 |
-
placeholder="e.g. 'Is there a train from Berlin to Munich tonight?'"
|
| 113 |
-
)
|
| 114 |
-
submit_button = gr.Button("Send", variant="primary")
|
| 115 |
|
| 116 |
-
# Gradio handles async generators automatically
|
| 117 |
submit_button.click(
|
| 118 |
fn=generate,
|
| 119 |
inputs=input_textbox,
|
|
|
|
| 8 |
from mcp.client.sse import sse_client
|
| 9 |
|
| 10 |
# --- CONFIGURATION ---
|
| 11 |
+
MCP_SERVER_URL = "https://mgokg-db-timetable-api.hf.space/gradio_api/mcp/"
|
|
|
|
| 12 |
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 13 |
|
| 14 |
async def call_mcp_tool(start_loc, dest_loc):
|
|
|
|
| 23 |
return result.content[0].text
|
| 24 |
|
| 25 |
async def generate(input_text):
|
| 26 |
+
"""Async generator handling the tool loop with the corrected aio client."""
|
| 27 |
if not GEMINI_API_KEY:
|
| 28 |
yield "### Error\nGEMINI_API_KEY is not set.", ""
|
| 29 |
return
|
| 30 |
|
| 31 |
+
# Initialize the standard client
|
| 32 |
client = genai.Client(api_key=GEMINI_API_KEY)
|
| 33 |
+
model_id = "gemini-flash-latest" # Ensure this is a valid model ID for your tier
|
|
|
|
| 34 |
|
|
|
|
| 35 |
train_tool = types.Tool(
|
| 36 |
function_declarations=[
|
| 37 |
types.FunctionDeclaration(
|
|
|
|
| 57 |
temperature=0.3
|
| 58 |
)
|
| 59 |
|
| 60 |
+
# FIX: Use client.aio to create an async chat session
|
| 61 |
+
chat = client.aio.chats.create(model=model_id, config=config)
|
| 62 |
+
|
| 63 |
+
# FIX: Use await chat.send_message (no _async suffix needed)
|
| 64 |
+
response = await chat.send_message(input_text)
|
| 65 |
|
| 66 |
# --- Manual Tool Loop ---
|
| 67 |
max_turns = 5
|
| 68 |
for _ in range(max_turns):
|
| 69 |
+
if not response.candidates or not response.candidates[0].content.parts:
|
| 70 |
break
|
| 71 |
|
| 72 |
tool_calls = [p.tool_call for p in response.candidates[0].content.parts if p.tool_call]
|
|
|
|
| 76 |
tool_responses = []
|
| 77 |
for call in tool_calls:
|
| 78 |
if call.name == "get_train_connections":
|
| 79 |
+
yield f"π Fetching train data: **{call.args['start_loc']}** β **{call.args['dest_loc']}**...", ""
|
| 80 |
|
|
|
|
| 81 |
train_data = await call_mcp_tool(call.args["start_loc"], call.args["dest_loc"])
|
| 82 |
|
| 83 |
tool_responses.append(
|
|
|
|
| 88 |
)
|
| 89 |
|
| 90 |
if tool_responses:
|
| 91 |
+
yield "π Finalizing response...", ""
|
| 92 |
+
# Send the tool results back using the async chat
|
| 93 |
+
response = await chat.send_message(tool_responses)
|
| 94 |
else:
|
| 95 |
break
|
| 96 |
|
|
|
|
| 97 |
yield response.text, ""
|
| 98 |
|
| 99 |
except Exception as e:
|
|
|
|
| 101 |
|
| 102 |
# --- GRADIO UI ---
|
| 103 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 104 |
+
gr.Markdown("# π Gemini MCP Train Assistant")
|
|
|
|
| 105 |
|
| 106 |
+
output_textbox = gr.Markdown(label="Response")
|
| 107 |
+
input_textbox = gr.Textbox(label="Ask about German train connections", placeholder="Berlin to Munich?")
|
| 108 |
+
submit_button = gr.Button("Send", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
|
|
|
| 110 |
submit_button.click(
|
| 111 |
fn=generate,
|
| 112 |
inputs=input_textbox,
|