File size: 2,825 Bytes
21cb95a 42d90e6 21cb95a 115dd84 70abedd 115dd84 70abedd aee1552 c67097b 70abedd aee1552 70abedd 21cb95a b4a96f5 aee1552 21cb95a b4a96f5 21cb95a c67097b dd6e0a4 c67097b dd6e0a4 c67097b 21cb95a 42d90e6 5dcbb94 42d90e6 5dcbb94 21cb95a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
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)
|