Spaces:
Runtime error
Runtime error
Commit
·
94df82c
1
Parent(s):
21b52e2
Fix HF_TOKEN environment variable and add error handling
Browse files
app.py
CHANGED
|
@@ -10,18 +10,30 @@ try:
|
|
| 10 |
{"url": "https://darkgenius01-mcp-sentiment.hf.space/gradio_api/mcp/sse"}
|
| 11 |
)
|
| 12 |
tools = mcp_client.get_tools()
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
finally:
|
| 27 |
-
mcp_client.disconnect()
|
|
|
|
| 10 |
{"url": "https://darkgenius01-mcp-sentiment.hf.space/gradio_api/mcp/sse"}
|
| 11 |
)
|
| 12 |
tools = mcp_client.get_tools()
|
| 13 |
+
except Exception as e:
|
| 14 |
+
print(f"Ошибка подключения к MCP серверу: {e}")
|
| 15 |
+
tools = []
|
| 16 |
|
| 17 |
+
# На Hugging Face Spaces используем HF_TOKEN, локально - HUGGINGFACE_API_TOKEN
|
| 18 |
+
hf_token = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_API_TOKEN")
|
| 19 |
+
if not hf_token:
|
| 20 |
+
raise ValueError("Необходимо установить переменную окружения HF_TOKEN или HUGGINGFACE_API_TOKEN")
|
| 21 |
|
| 22 |
+
model = InferenceClientModel(token=hf_token)
|
| 23 |
+
agent = CodeAgent(tools=[*tools], model=model, additional_authorized_imports=["json", "ast", "urllib", "base64"])
|
| 24 |
+
|
| 25 |
+
def chat_function(message, history):
|
| 26 |
+
try:
|
| 27 |
+
return str(agent.run(message))
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return f"Ошибка: {str(e)}"
|
| 30 |
+
|
| 31 |
+
demo = gr.ChatInterface(
|
| 32 |
+
fn=chat_function,
|
| 33 |
+
type="messages",
|
| 34 |
+
examples=["Analyze the sentiment of the following text 'This is awesome'"],
|
| 35 |
+
title="Agent with MCP Tools",
|
| 36 |
+
description="This is a simple agent that uses MCP tools to answer questions.",
|
| 37 |
+
)
|
| 38 |
|
| 39 |
+
demo.launch()
|
|
|
|
|
|