Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,158 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
|
| 4 |
from smolagents import InferenceClientModel, CodeAgent, MCPClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
| 15 |
-
agent = CodeAgent(tools=[*tools], model=model, additional_authorized_imports=["json", "ast", "urllib", "base64"])
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
|
|
|
| 3 |
from smolagents import InferenceClientModel, CodeAgent, MCPClient
|
| 4 |
+
from huggingface_hub import login, whoami
|
| 5 |
+
|
| 6 |
+
def check_and_login():
|
| 7 |
+
"""ํ๊น
ํ์ด์ค ๋ก๊ทธ์ธ ์ํ ํ์ธ ๋ฐ ๋ก๊ทธ์ธ"""
|
| 8 |
+
try:
|
| 9 |
+
user_info = whoami()
|
| 10 |
+
print(f"โ ์ด๋ฏธ ๋ก๊ทธ์ธ๋จ: {user_info['name']}")
|
| 11 |
+
return True
|
| 12 |
+
except:
|
| 13 |
+
token = os.getenv("HUGGINGFACE_API_TOKEN") or os.getenv("HF_TOKEN")
|
| 14 |
+
if token:
|
| 15 |
+
try:
|
| 16 |
+
login(token=token)
|
| 17 |
+
user_info = whoami()
|
| 18 |
+
print(f"โ ํ ํฐ์ผ๋ก ๋ก๊ทธ์ธ ์ฑ๊ณต: {user_info['name']}")
|
| 19 |
+
return True
|
| 20 |
+
except Exception as e:
|
| 21 |
+
print(f"โ ํ ํฐ ๋ก๊ทธ์ธ ์คํจ: {e}")
|
| 22 |
+
return False
|
| 23 |
+
else:
|
| 24 |
+
print("โ ํ ํฐ์ด ์์ต๋๋ค.")
|
| 25 |
+
return False
|
| 26 |
+
|
| 27 |
+
def safe_mcp_connection():
|
| 28 |
+
"""์์ ํ MCP ์ฐ๊ฒฐ ์๋"""
|
| 29 |
+
mcp_configs = [
|
| 30 |
+
# 1. ์ฌ๋ฐ๋ฅธ transport ๋ช
์ํ ์ค์
|
| 31 |
+
{
|
| 32 |
+
"url": "https://abidlabs-mcp-tool-http.hf.space/gradio_api/mcp/sse",
|
| 33 |
+
"transport": "sse"
|
| 34 |
+
},
|
| 35 |
+
# 2. ๋ค๋ฅธ MCP ์๋ฒ ์๋
|
| 36 |
+
{
|
| 37 |
+
"url": "https://0208suin-mcp-client.hf.space/gradio_api/mcp/sse",
|
| 38 |
+
"transport": "sse"
|
| 39 |
+
}
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
for i, config in enumerate(mcp_configs, 1):
|
| 43 |
+
try:
|
| 44 |
+
print(f"MCP ์๋ฒ {i} ์ฐ๊ฒฐ ์๋...")
|
| 45 |
+
mcp_client = MCPClient(config)
|
| 46 |
+
tools = mcp_client.get_tools()
|
| 47 |
+
print(f"โ MCP ์๋ฒ {i} ์ฐ๊ฒฐ ์ฑ๊ณต: {len(tools)}๊ฐ ๋๊ตฌ")
|
| 48 |
+
return mcp_client, tools
|
| 49 |
+
except Exception as e:
|
| 50 |
+
print(f"โ MCP ์๋ฒ {i} ์ฐ๊ฒฐ ์คํจ: {str(e)[:100]}...")
|
| 51 |
+
continue
|
| 52 |
+
|
| 53 |
+
print("๋ชจ๋ MCP ์๋ฒ ์ฐ๊ฒฐ ์คํจ, ๊ธฐ๋ณธ ๋๊ตฌ๋ก ์คํ")
|
| 54 |
+
return None, []
|
| 55 |
+
|
| 56 |
+
def main():
|
| 57 |
+
mcp_client = None
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
# 1. ํ๊น
ํ์ด์ค ๋ก๊ทธ์ธ ํ์ธ
|
| 61 |
+
if not check_and_login():
|
| 62 |
+
def login_error(message, history):
|
| 63 |
+
return """๐ ๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค:
|
| 64 |
+
|
| 65 |
+
1. ํฐ๋ฏธ๋์์: `huggingface-cli login`
|
| 66 |
+
2. ๋๋ ์คํ์ด์ค Settings์์ HUGGINGFACE_API_TOKEN ์ค์
|
| 67 |
+
3. ํ ํฐ ๋ฐ๊ธ: https://huggingface.co/settings/tokens"""
|
| 68 |
+
|
| 69 |
+
return gr.ChatInterface(
|
| 70 |
+
fn=login_error,
|
| 71 |
+
type="messages",
|
| 72 |
+
title="๐ ๋ก๊ทธ์ธ ํ์",
|
| 73 |
+
description="Hugging Face ๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค.",
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# 2. MCP ์ฐ๊ฒฐ ์๋
|
| 77 |
+
mcp_client, tools = safe_mcp_connection()
|
| 78 |
+
|
| 79 |
+
# 3. ๋ชจ๋ธ ์ด๊ธฐํ
|
| 80 |
+
try:
|
| 81 |
+
model = InferenceClientModel()
|
| 82 |
+
print("โ ๋ชจ๋ธ ์ด๊ธฐํ ์๋ฃ")
|
| 83 |
+
except Exception as e:
|
| 84 |
+
print(f"โ ๋ชจ๋ธ ์ด๊ธฐํ ์คํจ: {e}")
|
| 85 |
+
# ๋์: HfApiModel ์ฌ์ฉ
|
| 86 |
+
from smolagents import HfApiModel
|
| 87 |
+
token = os.getenv("HUGGINGFACE_API_TOKEN") or os.getenv("HF_TOKEN")
|
| 88 |
+
model = HfApiModel(model_id="microsoft/DialoGPT-medium", token=token)
|
| 89 |
+
print("โ ๋์ ๋ชจ๋ธ(HfApiModel) ์ด๊ธฐํ ์๋ฃ")
|
| 90 |
+
|
| 91 |
+
# 4. ์์ด์ ํธ ์ด๊ธฐํ
|
| 92 |
+
agent = CodeAgent(
|
| 93 |
+
tools=tools,
|
| 94 |
+
model=model,
|
| 95 |
+
additional_authorized_imports=["json", "ast", "urllib", "base64"]
|
| 96 |
+
)
|
| 97 |
+
print("โ ์์ด์ ํธ ์ด๊ธฐํ ์๋ฃ")
|
| 98 |
+
|
| 99 |
+
# 5. ์ฑํ
ํจ์
|
| 100 |
+
def agent_chat(message, history):
|
| 101 |
+
try:
|
| 102 |
+
result = agent.run(message)
|
| 103 |
+
return str(result)
|
| 104 |
+
except Exception as e:
|
| 105 |
+
error_msg = str(e)
|
| 106 |
+
if "nebius" in error_msg.lower():
|
| 107 |
+
return "โ ๋ชจ๋ธ ์ ๊ทผ ์ค๋ฅ์
๋๋ค. ํ๊น
ํ์ด์ค ๋ก๊ทธ์ธ์ ํ์ธํด์ฃผ์ธ์."
|
| 108 |
+
elif "mcp" in error_msg.lower():
|
| 109 |
+
return f"โ ๏ธ MCP ๋๊ตฌ ์ค๋ฅ: {error_msg[:200]}..."
|
| 110 |
+
else:
|
| 111 |
+
return f"โ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {error_msg[:200]}..."
|
| 112 |
|
| 113 |
+
# 6. ์ธํฐํ์ด์ค ์์ฑ
|
| 114 |
+
demo = gr.ChatInterface(
|
| 115 |
+
fn=agent_chat,
|
| 116 |
+
type="messages",
|
| 117 |
+
examples=[
|
| 118 |
+
"์๋
ํ์ธ์! ์ด๋ป๊ฒ ๋์๋๋ฆด๊น์?",
|
| 119 |
+
"Analyze the sentiment of 'This is awesome!'",
|
| 120 |
+
"์ฌ์ฉ ๊ฐ๋ฅํ ๋๊ตฌ๊ฐ ๋ฌด์์ธ๊ฐ์?"
|
| 121 |
+
],
|
| 122 |
+
title="๐ค Agent with MCP Tools",
|
| 123 |
+
description=f"MCP ๋๊ตฌ๋ฅผ ์ฌ์ฉํ๋ ์ค๋งํธ ์์ด์ ํธ (๋๊ตฌ {len(tools)}๊ฐ)",
|
| 124 |
+
)
|
| 125 |
|
| 126 |
+
return demo
|
| 127 |
|
| 128 |
+
except Exception as e:
|
| 129 |
+
print(f"์ด๊ธฐํ ์ค๋ฅ: {e}")
|
| 130 |
+
|
| 131 |
+
def error_message(message, history):
|
| 132 |
+
return f"""โ ์๋น์ค ์ด๊ธฐํ ์คํจ:
|
| 133 |
|
| 134 |
+
{str(e)[:300]}...
|
|
|
|
| 135 |
|
| 136 |
+
ํด๊ฒฐ ๋ฐฉ๋ฒ:
|
| 137 |
+
1. ์คํ์ด์ค ์ฌ์์
|
| 138 |
+
2. ํ๊ฒฝ ๋ณ์ ํ์ธ
|
| 139 |
+
3. ํ๊น
ํ์ด์ค ํ ํฐ ํ์ธ"""
|
| 140 |
+
|
| 141 |
+
return gr.ChatInterface(
|
| 142 |
+
fn=error_message,
|
| 143 |
+
type="messages",
|
| 144 |
+
title="โ ์ด๊ธฐํ ์ค๋ฅ",
|
| 145 |
+
description="์๋น์ค ์ด๊ธฐํ์ ์คํจํ์ต๋๋ค.",
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
finally:
|
| 149 |
+
if mcp_client:
|
| 150 |
+
try:
|
| 151 |
+
mcp_client.disconnect()
|
| 152 |
+
print("โ MCP ์ฐ๊ฒฐ ์ข
๋ฃ")
|
| 153 |
+
except:
|
| 154 |
+
pass
|
| 155 |
|
| 156 |
+
if __name__ == "__main__":
|
| 157 |
+
demo = main()
|
| 158 |
+
demo.launch()
|