0208suin commited on
Commit
ac83d36
ยท
verified ยท
1 Parent(s): 1f3c1d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -148
app.py CHANGED
@@ -1,158 +1,28 @@
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()
 
1
  import gradio as gr
2
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
+ from smolagents import InferenceClientModel, CodeAgent, MCPClient
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ try:
9
+ mcp_client = MCPClient(
10
+ {"url": "https://0208suin-mcp-client.hf.space/gradio_api/mcp/sse"}
11
+ )
12
+ tools = mcp_client.get_tools()
13
 
14
+ model = InferenceClientModel(token=os.getenv("HUGGINGFACE_API_TOKEN"))
15
+ agent = CodeAgent(tools=[*tools], model=model, additional_authorized_imports=["json", "ast", "urllib", "base64"])
 
 
 
16
 
17
+ demo = gr.ChatInterface(
18
+ fn=lambda message, history: str(agent.run(message)),
19
+ type="messages",
20
+ examples=["Analyze the sentiment of the following text 'This is awesome'"],
21
+ title="Agent with MCP Tools",
22
+ description="This is a simple agent that uses MCP tools to answer questions.",
23
+ )
24
 
25
+ demo.launch(mcp_server=True)
26
+ finally:
27
+ mcp_client.disconnect()
 
 
 
 
 
 
 
 
28