Spaces:
Sleeping
Sleeping
Commit
·
521f62d
1
Parent(s):
af9f855
client
Browse files
app.py
CHANGED
|
@@ -1,35 +1,99 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
from
|
| 4 |
-
from smolagents import InferenceClientModel, CodeAgent, ToolCollection
|
| 5 |
-
from smolagents.mcp_client import MCPClient
|
| 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 |
-
mcp_client.disconnect()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
|
| 4 |
+
from smolagents import InferenceClientModel, CodeAgent, ToolCollection, MCPClient
|
|
|
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
+
# Function to initialize MCP client and agent
|
| 8 |
+
def initialize_agent(api_key, model_name):
|
| 9 |
+
try:
|
| 10 |
+
# Initialize MCP client with explicit transport key
|
| 11 |
+
mcp_client = MCPClient(
|
| 12 |
+
{
|
| 13 |
+
"url": "https://wd101-oneservertorulethemall.hf.space/gradio_api/mcp/sse",
|
| 14 |
+
"transport": "sse" # Explicitly specify transport to avoid FutureWarning
|
| 15 |
+
}
|
| 16 |
+
)
|
| 17 |
+
tools = mcp_client.get_tools()
|
| 18 |
+
|
| 19 |
+
# Initialize model with API key
|
| 20 |
+
if not api_key:
|
| 21 |
+
return None, None, "Please provide a valid API key."
|
| 22 |
+
|
| 23 |
+
# Map model names to InferenceClientModel configurations
|
| 24 |
+
model_configs = {
|
| 25 |
+
"Qwen": "Qwen/Qwen2-7B-Instruct", # Example model ID
|
| 26 |
+
"Gemma": "google/gemma-7b", # Example model ID
|
| 27 |
+
"Llama": "meta-llama/Llama-3-8b" # Example model ID
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
model = InferenceClientModel(
|
| 31 |
+
provider='nebius',
|
| 32 |
+
token=api_key,
|
| 33 |
+
model=model_configs.get(model_name, "google/gemma-7b") # Default to Qwen
|
| 34 |
+
)
|
| 35 |
+
agent = CodeAgent(tools=[*tools], model=model)
|
| 36 |
+
return mcp_client, agent, None
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return None, None, f"Failed to initialize agent: {str(e)}"
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# Function to handle chatbot interaction
|
| 42 |
+
def chatbot_function(user_message, chat_history, api_key, model_name):
|
| 43 |
+
# Initialize MCP client and agent
|
| 44 |
+
mcp_client, agent, error = initialize_agent(api_key, model_name)
|
| 45 |
+
|
| 46 |
+
if error:
|
| 47 |
+
chat_history.append((user_message, error))
|
| 48 |
+
if mcp_client:
|
| 49 |
+
mcp_client.disconnect()
|
| 50 |
+
return chat_history, ""
|
| 51 |
+
|
| 52 |
+
try:
|
| 53 |
+
# Process user message with the agent
|
| 54 |
+
if not user_message:
|
| 55 |
+
response = "Please enter a message."
|
| 56 |
+
else:
|
| 57 |
+
response = str(agent.run(user_message))
|
| 58 |
+
|
| 59 |
+
chat_history.append((user_message, response))
|
| 60 |
+
except Exception as e:
|
| 61 |
+
chat_history.append((user_message, f"Error processing message: {str(e)}"))
|
| 62 |
+
finally:
|
| 63 |
+
if mcp_client:
|
| 64 |
+
mcp_client.disconnect()
|
| 65 |
+
|
| 66 |
+
return chat_history, ""
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
# Gradio interface
|
| 70 |
+
with gr.Blocks() as demo:
|
| 71 |
+
gr.Markdown("# Hackathon Chatbot with MCP Tools")
|
| 72 |
+
gr.Markdown("Enter your API key (e.g., Hugging Face token), select a model, and start chatting!")
|
| 73 |
+
|
| 74 |
+
# API key input and model selection
|
| 75 |
+
with gr.Row():
|
| 76 |
+
api_key_input = gr.Textbox(label="API Key", type="password", placeholder="Enter your API key here")
|
| 77 |
+
model_dropdown = gr.Dropdown(
|
| 78 |
+
choices=["Qwen", "Gemma", "Llama"],
|
| 79 |
+
label="Select Model",
|
| 80 |
+
value="Gemma"
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
# Chatbot interface
|
| 84 |
+
chatbot = gr.Chatbot(label="Chat History")
|
| 85 |
+
user_input = gr.Textbox(label="Your Message", placeholder="Type your message here...")
|
| 86 |
+
submit_button = gr.Button("Send")
|
| 87 |
+
|
| 88 |
+
# State to store chat history
|
| 89 |
+
chat_history = gr.State([])
|
| 90 |
+
|
| 91 |
+
# Connect the submit button to the chatbot function
|
| 92 |
+
submit_button.click(
|
| 93 |
+
fn=chatbot_function,
|
| 94 |
+
inputs=[user_input, chat_history, api_key_input, model_dropdown],
|
| 95 |
+
outputs=[chatbot, user_input]
|
| 96 |
)
|
| 97 |
|
| 98 |
+
# Launch the Gradio app
|
| 99 |
+
demo.launch()
|
|
|