Omar ID EL MOUMEN
commited on
Commit
·
6b639db
1
Parent(s):
d59b539
Big fix
Browse files
app.py
CHANGED
@@ -1,8 +1,7 @@
|
|
1 |
# mcp_groq_gradio.py
|
2 |
-
import asyncio
|
3 |
import gradio as gr
|
4 |
-
import os
|
5 |
import json
|
|
|
6 |
import httpx
|
7 |
from contextlib import AsyncExitStack
|
8 |
from typing import Optional
|
@@ -21,10 +20,9 @@ class MCPGroqClient:
|
|
21 |
|
22 |
def __init__(self):
|
23 |
self.session: Optional[ClientSession] = None
|
24 |
-
self.current_model = ""
|
25 |
self.exit_stack = AsyncExitStack()
|
|
|
26 |
self.groq = None
|
27 |
-
self.available_tools = None
|
28 |
|
29 |
async def connect(self):
|
30 |
"""Establish MCP STDIO connection"""
|
@@ -42,8 +40,6 @@ class MCPGroqClient:
|
|
42 |
)
|
43 |
await self.session.initialize()
|
44 |
|
45 |
-
self.available_tools = await self.session.list_tools()
|
46 |
-
|
47 |
async def stream_response(self, query: str):
|
48 |
"""Handle streaming with failed_generation debugging"""
|
49 |
messages = [{"role": "user", "content": query}]
|
@@ -79,8 +75,9 @@ class MCPGroqClient:
|
|
79 |
|
80 |
except Exception as e:
|
81 |
# Handle Groq-specific errors
|
|
|
82 |
if hasattr(e, "body") and "failed_generation" in e.body:
|
83 |
-
failed_generation = e.
|
84 |
yield f"\n⚠️ Error: Failed to call a function. Invalid generation:\n{failed_generation}"
|
85 |
else:
|
86 |
yield f"\n⚠️ Critical Error: {str(e)}"
|
@@ -98,18 +95,6 @@ class MCPGroqClient:
|
|
98 |
"parameters": tool.inputSchema
|
99 |
}
|
100 |
} for tool in response.tools]
|
101 |
-
|
102 |
-
async def _get_available_models(self):
|
103 |
-
try:
|
104 |
-
models = self.groq.models.list()
|
105 |
-
return sorted([model.id for model in models.data if model.active])
|
106 |
-
except Exception as e:
|
107 |
-
print(e)
|
108 |
-
return sorted([
|
109 |
-
"llama-3.3-70b-versatile",
|
110 |
-
"llama-3.1-8b-instant",
|
111 |
-
"gemma2-9b-it"
|
112 |
-
])
|
113 |
|
114 |
async def _process_tool_calls(self, tool_calls, messages):
|
115 |
for tool in tool_calls:
|
@@ -169,7 +154,7 @@ def create_interface():
|
|
169 |
with gr.Row(visible=False) as chat_row:
|
170 |
with gr.Column(scale=0.6):
|
171 |
chatbot = gr.Chatbot(height=600)
|
172 |
-
input_box = gr.Textbox(placeholder="Type message..."
|
173 |
submit_btn = gr.Button("Send", variant="primary")
|
174 |
|
175 |
with gr.Column(scale=0.4):
|
@@ -178,25 +163,27 @@ def create_interface():
|
|
178 |
interactive=True,
|
179 |
visible=False
|
180 |
)
|
181 |
-
available_tools = gr.Textbox(
|
182 |
-
label="Tools Available",
|
183 |
-
interactive=False,
|
184 |
-
visible=False
|
185 |
-
)
|
186 |
|
187 |
# Connect Button Logic
|
188 |
def connect_client(api_key):
|
189 |
try:
|
190 |
# Initialize Groq client with provided API key
|
191 |
client.groq = Groq(api_key=api_key, http_client=httpx.Client(verify=False))
|
192 |
-
|
193 |
-
loop = asyncio.new_event_loop()
|
194 |
-
asyncio.set_event_loop(loop)
|
195 |
-
loop.run_until_complete(client.connect())
|
196 |
-
|
197 |
models = client.groq.models.list().data
|
198 |
-
|
199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
|
201 |
return {
|
202 |
connection_status: "Connected ✅",
|
@@ -206,10 +193,6 @@ def create_interface():
|
|
206 |
value=active_models[0] if active_models else "",
|
207 |
visible=True
|
208 |
),
|
209 |
-
available_tools: gr.update(
|
210 |
-
value=tools_list,
|
211 |
-
visible=True
|
212 |
-
),
|
213 |
connect_btn: gr.update(visible=False),
|
214 |
api_key_input: gr.update(interactive=False)
|
215 |
}
|
@@ -222,15 +205,14 @@ def create_interface():
|
|
222 |
connect_btn.click(
|
223 |
connect_client,
|
224 |
inputs=api_key_input,
|
225 |
-
outputs=[connection_status, chat_row, model_selector,
|
226 |
)
|
227 |
|
228 |
# Chat Handling
|
229 |
async def chat_stream(query, history, selected_model):
|
230 |
-
print(f"Received query: {query}") # Debugging log
|
231 |
client.current_model = selected_model
|
232 |
|
233 |
-
# Initialize fresh client session
|
234 |
if not client.session:
|
235 |
await client.connect()
|
236 |
|
|
|
1 |
# mcp_groq_gradio.py
|
|
|
2 |
import gradio as gr
|
|
|
3 |
import json
|
4 |
+
import traceback
|
5 |
import httpx
|
6 |
from contextlib import AsyncExitStack
|
7 |
from typing import Optional
|
|
|
20 |
|
21 |
def __init__(self):
|
22 |
self.session: Optional[ClientSession] = None
|
|
|
23 |
self.exit_stack = AsyncExitStack()
|
24 |
+
self.current_model = ""
|
25 |
self.groq = None
|
|
|
26 |
|
27 |
async def connect(self):
|
28 |
"""Establish MCP STDIO connection"""
|
|
|
40 |
)
|
41 |
await self.session.initialize()
|
42 |
|
|
|
|
|
43 |
async def stream_response(self, query: str):
|
44 |
"""Handle streaming with failed_generation debugging"""
|
45 |
messages = [{"role": "user", "content": query}]
|
|
|
75 |
|
76 |
except Exception as e:
|
77 |
# Handle Groq-specific errors
|
78 |
+
traceback.print_exception(e)
|
79 |
if hasattr(e, "body") and "failed_generation" in e.body:
|
80 |
+
failed_generation = e.body["failed_generation"]
|
81 |
yield f"\n⚠️ Error: Failed to call a function. Invalid generation:\n{failed_generation}"
|
82 |
else:
|
83 |
yield f"\n⚠️ Critical Error: {str(e)}"
|
|
|
95 |
"parameters": tool.inputSchema
|
96 |
}
|
97 |
} for tool in response.tools]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
async def _process_tool_calls(self, tool_calls, messages):
|
100 |
for tool in tool_calls:
|
|
|
154 |
with gr.Row(visible=False) as chat_row:
|
155 |
with gr.Column(scale=0.6):
|
156 |
chatbot = gr.Chatbot(height=600)
|
157 |
+
input_box = gr.Textbox(placeholder="Type message...")
|
158 |
submit_btn = gr.Button("Send", variant="primary")
|
159 |
|
160 |
with gr.Column(scale=0.4):
|
|
|
163 |
interactive=True,
|
164 |
visible=False
|
165 |
)
|
|
|
|
|
|
|
|
|
|
|
166 |
|
167 |
# Connect Button Logic
|
168 |
def connect_client(api_key):
|
169 |
try:
|
170 |
# Initialize Groq client with provided API key
|
171 |
client.groq = Groq(api_key=api_key, http_client=httpx.Client(verify=False))
|
|
|
|
|
|
|
|
|
|
|
172 |
models = client.groq.models.list().data
|
173 |
+
available_models = sorted([m.id for m in models if m.active])
|
174 |
+
compatible_models = [
|
175 |
+
"qwen-qwq-32b",
|
176 |
+
"qwen-2.5-coder-32b",
|
177 |
+
"qwen-2.5-32b",
|
178 |
+
"deepseek-r1-distill-qwen-32b",
|
179 |
+
"deepseek-r1-distill-llama-70b",
|
180 |
+
"llama-3.3-70b-versatile",
|
181 |
+
"llama-3.1-8b-instant",
|
182 |
+
"mixtral-8x7b-32768",
|
183 |
+
"gemma2-9b-it"
|
184 |
+
]
|
185 |
+
active_models = sorted([model for model in available_models if model in compatible_models])
|
186 |
+
|
187 |
|
188 |
return {
|
189 |
connection_status: "Connected ✅",
|
|
|
193 |
value=active_models[0] if active_models else "",
|
194 |
visible=True
|
195 |
),
|
|
|
|
|
|
|
|
|
196 |
connect_btn: gr.update(visible=False),
|
197 |
api_key_input: gr.update(interactive=False)
|
198 |
}
|
|
|
205 |
connect_btn.click(
|
206 |
connect_client,
|
207 |
inputs=api_key_input,
|
208 |
+
outputs=[connection_status, chat_row, model_selector, connect_btn, api_key_input]
|
209 |
)
|
210 |
|
211 |
# Chat Handling
|
212 |
async def chat_stream(query, history, selected_model):
|
|
|
213 |
client.current_model = selected_model
|
214 |
|
215 |
+
# Initialize fresh client session
|
216 |
if not client.session:
|
217 |
await client.connect()
|
218 |
|