MCPClient / app.py
binary1ne's picture
Update app.py
4864189 verified
raw
history blame
3.2 kB
import requests
import gradio as gr
import logging
import nest_asyncio
from typing import Any
from llama_index.tools.mcp import BasicMCPClient, McpToolSpec
# Logging Setup
logger = logging.getLogger(__name__)
# Default Hugging Face model and API URL
DEFAULT_HUGGINGFACE_MODEL = "Eric1227/dolphin-2.5-mixtral-8x7b-MLX-6bit" # Use your desired model
HUGGINGFACE_API_URL = "https://api-inference.huggingface.co/models/{model_name}"
API_KEY = "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" # Your Hugging Face API key
# Apply nest_asyncio to handle event loops in Jupyter (if using it)
nest_asyncio.apply()
# Remote MCP Client Setup (Update with your remote MCP server URL)
REMOTE_MCP_URL = "http://https://binary1ne-mcpserver.hf.space"
mcp_client = BasicMCPClient(REMOTE_MCP_URL)
mcp_tool = McpToolSpec(client=mcp_client)
# Function to call Hugging Face Inference API
def query_huggingface_api(prompt: str, model_name: str = DEFAULT_HUGGINGFACE_MODEL) -> str:
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
payload = {
"inputs": prompt
}
response = requests.post(HUGGINGFACE_API_URL.format(model_name=model_name),
headers=headers, json=payload)
if response.status_code == 200:
return response.json()[0]["generated_text"]
else:
logger.error(f"Error from Hugging Face API: {response.status_code}, {response.text}")
return "Error processing your request."
# Function to interact with MCP (for processing or augmenting responses)
def interact_with_mcp(input_text: str) -> str:
# Send input to MCP (modify as per your MCP interaction logic)
try:
response = mcp_client.query(input_text) # Assuming `query` method is used for MCP interaction
return response['response'] # Adjust based on your MCP response format
except Exception as e:
logger.error(f"Error interacting with MCP: {str(e)}")
return "MCP interaction failed."
# Create the function that Gradio will call for inference
def generate_response_with_mcp(prompt: str) -> str:
# First, interact with the Hugging Face model
model_response = query_huggingface_api(prompt)
# Then, send that response to the MCP system for additional processing
mcp_response = interact_with_mcp(model_response)
# Combine Hugging Face and MCP responses (or modify logic as needed)
return f"Model Response: {model_response}\n\nMCP Response: {mcp_response}"
# Set up Gradio interface
def launch_gradio_interface():
with gr.Blocks() as demo:
gr.Markdown("### Hugging Face Model + Remote MCP Integration")
with gr.Row():
prompt_input = gr.Textbox(label="Enter Your Prompt", placeholder="Type something here...")
output_text = gr.Textbox(label="Generated Response")
# Button to submit the prompt
submit_btn = gr.Button("Generate Response")
# Link the button action to the function
submit_btn.click(generate_response_with_mcp, inputs=prompt_input, outputs=output_text)
demo.launch()
if __name__ == "__main__":
launch_gradio_interface()