mm-mcp / app.py
x1001000's picture
Fix Gradio interface with Blocks layout and improve response parsing
5dcbb94
import gradio as gr
import requests
def get_stock_etf_info_url(language, category, ticker):
"""
Get a single MacroMicro US Stock, ETF, or TW ETF information page URL.
Args:
language (str): either 'zh-tw' or 'zh-cn' or 'other'
category (str): either 'US Stock' or 'US ETF' or 'TW ETF'.
ticker (str): Stock or ETF symbol (e.g., 'AAPL', 'SPY', '0050').
Returns:
https://{subdomain}.macromicro.me/{route}/{ticker}
"""
language_map = {
'zh': 'www',
'tw': 'www',
'zh-tw': 'www',
'zh-cn': 'sc',
'other': 'en'
}
subdomain = language_map.get(language.lower(), 'en')
category_map = {
'US Stock'.lower(): 'stocks/info',
'US ETF'.lower(): 'etf/us/intro',
'TW ETF'.lower(): 'etf/tw/intro'
}
ticker = ticker.strip()
category = category.strip()
route = category_map.get(category.lower())
return f"https://{subdomain}.macromicro.me/{route}/{ticker}"
def ask_madam(question):
"""
Send a question to the Madam API and get a response.
This function allows MCP hosts to interact with the Madam chatbot API
by sending questions and receiving responses. Perfect for getting
AI-powered answers to various queries.
Args:
question (str): The question to ask Madam
Returns:
str: The response text from Madam API
Example:
response = ask_madam("What is the weather like today?")
"""
try:
response = requests.post(
"https://x1001000-mm-madam-api-widget.hf.space/chat",
json={
"message": question,
"conversation_history": [],
"config": {}
}
)
response.raise_for_status()
return eval(response.text).get('response', 'No response from Madam API')
except requests.exceptions.RequestException as e:
return f"Error communicating with Madam API: {str(e)}"
with gr.Blocks() as demo:
with gr.Tab("Stock/ETF Info"):
language_input = gr.Textbox(label="Language (zh-tw, zh-cn, other)")
category_input = gr.Textbox(label="Category (US Stock, US ETF, TW ETF)")
ticker_input = gr.Textbox(label="Ticker Symbol")
stock_output = gr.Textbox(label="MacroMicro URL")
stock_btn = gr.Button("Get URL")
stock_btn.click(get_stock_etf_info_url, inputs=[language_input, category_input, ticker_input], outputs=stock_output)
with gr.Tab("Ask Madam"):
question_input = gr.Textbox(label="Question")
madam_output = gr.Textbox(label="Answer")
madam_btn = gr.Button("Ask Madam", interactive=False)
madam_btn.click(ask_madam, inputs=question_input, outputs=madam_output)
demo.launch(mcp_server=True)