Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from langchain_groq import ChatGroq
|
| 4 |
+
from mcp_use import MCPAgent, MCPClient
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# Load environment variables
|
| 8 |
+
load_dotenv()
|
| 9 |
+
os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY")
|
| 10 |
+
config_file = "browser_mcp.json"
|
| 11 |
+
|
| 12 |
+
client = MCPClient.from_config_file(config_file)
|
| 13 |
+
llm = ChatGroq(model="llama3-8b-8192")
|
| 14 |
+
agent = MCPAgent(llm=llm, client=client, max_steps=15, memory_enabled=True)
|
| 15 |
+
|
| 16 |
+
def chat(user_input, history=[]):
|
| 17 |
+
if user_input.lower() == "clear":
|
| 18 |
+
agent.clear_conversation_history()
|
| 19 |
+
return "", []
|
| 20 |
+
response = agent.run(user_input)
|
| 21 |
+
history = history + [[user_input, response]]
|
| 22 |
+
return "", history
|
| 23 |
+
|
| 24 |
+
with gr.Blocks() as demo:
|
| 25 |
+
gr.Markdown("# MCP Chatbot")
|
| 26 |
+
chatbot = gr.Chatbot()
|
| 27 |
+
msg = gr.Textbox(label="Type your message")
|
| 28 |
+
clear = gr.Button("Clear Conversation")
|
| 29 |
+
|
| 30 |
+
def respond(message, chat_history):
|
| 31 |
+
_, updated_history = chat(message, chat_history)
|
| 32 |
+
return "", updated_history
|
| 33 |
+
|
| 34 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 35 |
+
clear.click(lambda: ("", []), None, [msg, chatbot])
|
| 36 |
+
|
| 37 |
+
demo.launch()
|