mgokg commited on
Commit
880e5ea
·
verified ·
1 Parent(s): 83992cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -11
app.py CHANGED
@@ -2,9 +2,9 @@ import base64
2
  import gradio as gr
3
  import os
4
  import json
 
5
  from google import genai
6
  from google.genai import types
7
- from gradio_client import Client
8
 
9
  route="""
10
  how to handle special case "zugverbindung".
@@ -50,17 +50,47 @@ def is_train_connection_query(text):
50
 
51
  def get_train_connections(start_loc, dest_loc):
52
  """
53
- Calls the MCP server to get train connections.
54
  """
55
  try:
56
- client = Client("mgokg/db-timetable-api")
57
- result = client.predict(
58
- query=f"Verbindung von {start_loc} nach {dest_loc}",
59
- api_name="/db_timetable_api_ui_wrapper"
60
- )
61
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  except Exception as e:
63
- return f"Error calling MCP server: {e}"
64
 
65
  def generate(input_text):
66
  # Check if this is a train connection query
@@ -110,7 +140,7 @@ def generate(input_text):
110
  return "Konnte Start- oder Zielort nicht identifizieren. Bitte gib beide Orte an.", ""
111
 
112
  except json.JSONDecodeError as e:
113
- return f"Error parsing location data: {e}", ""
114
  except Exception as e:
115
  return f"Error processing train connection request: {e}", ""
116
 
@@ -166,4 +196,4 @@ if __name__ == '__main__':
166
  input_textbox = gr.Textbox(lines=3, label="", placeholder="Enter message here...")
167
  submit_button = gr.Button("send")
168
  submit_button.click(fn=generate, inputs=input_textbox, outputs=[output_textbox, input_textbox])
169
- demo.launch(show_error=True)
 
2
  import gradio as gr
3
  import os
4
  import json
5
+ import requests
6
  from google import genai
7
  from google.genai import types
 
8
 
9
  route="""
10
  how to handle special case "zugverbindung".
 
50
 
51
  def get_train_connections(start_loc, dest_loc):
52
  """
53
+ Calls the MCP server via HTTP to get train connections.
54
  """
55
  try:
56
+ mcp_url = "https://mgokg-db-timetable-api.hf.space/gradio_api/mcp/"
57
+
58
+ # MCP protocol request structure
59
+ payload = {
60
+ "jsonrpc": "2.0",
61
+ "id": 1,
62
+ "method": "tools/call",
63
+ "params": {
64
+ "name": "db_timetable_api_ui_wrapper",
65
+ "arguments": {
66
+ "query": f"Verbindung von {start_loc} nach {dest_loc}"
67
+ }
68
+ }
69
+ }
70
+
71
+ headers = {
72
+ "Content-Type": "application/json"
73
+ }
74
+
75
+ response = requests.post(mcp_url, json=payload, headers=headers, timeout=30)
76
+ response.raise_for_status()
77
+
78
+ result = response.json()
79
+
80
+ # Extract the result from MCP response
81
+ if "result" in result:
82
+ content = result["result"].get("content", [])
83
+ if content and len(content) > 0:
84
+ return content[0].get("text", "Keine Verbindungen gefunden")
85
+
86
+ return f"Unerwartete Antwortstruktur: {result}"
87
+
88
+ except requests.exceptions.Timeout:
89
+ return "Die Anfrage hat zu lange gedauert. Bitte versuche es erneut."
90
+ except requests.exceptions.RequestException as e:
91
+ return f"Fehler beim Abrufen der Zugverbindungen: {e}"
92
  except Exception as e:
93
+ return f"Unerwarteter Fehler: {e}"
94
 
95
  def generate(input_text):
96
  # Check if this is a train connection query
 
140
  return "Konnte Start- oder Zielort nicht identifizieren. Bitte gib beide Orte an.", ""
141
 
142
  except json.JSONDecodeError as e:
143
+ return f"Error parsing location data: {e}\nResponse was: {response.text}", ""
144
  except Exception as e:
145
  return f"Error processing train connection request: {e}", ""
146
 
 
196
  input_textbox = gr.Textbox(lines=3, label="", placeholder="Enter message here...")
197
  submit_button = gr.Button("send")
198
  submit_button.click(fn=generate, inputs=input_textbox, outputs=[output_textbox, input_textbox])
199
+ demo.launch(show_error=True)