imessam commited on
Commit
ee980d6
·
1 Parent(s): 0c30565

DEV: first

Browse files
Files changed (5) hide show
  1. .gitignore +1 -0
  2. README.md +1 -2
  3. app.py +48 -48
  4. modal_setup.py +99 -0
  5. requirements.txt +4 -1
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __pycache__
README.md CHANGED
@@ -11,7 +11,6 @@ license: apache-2.0
11
  short_description: Podcast Generator MCP Server
12
  tags:
13
  - Agents-MCP-Hackathon
 
14
  - mcp-server-track
15
  ---
16
-
17
- An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
 
11
  short_description: Podcast Generator MCP Server
12
  tags:
13
  - Agents-MCP-Hackathon
14
+ - agent-demo-track
15
  - mcp-server-track
16
  ---
 
 
app.py CHANGED
@@ -1,64 +1,64 @@
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
 
 
 
 
 
 
8
 
 
 
 
 
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
- messages.append({"role": "user", "content": message})
 
27
 
28
  response = ""
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
 
 
38
 
39
- response += token
40
- yield response
41
 
 
 
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
  demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
  )
61
 
62
-
63
  if __name__ == "__main__":
64
- demo.launch()
 
1
+ import os
2
+
3
  import gradio as gr
 
4
 
5
+ from smolagents import CodeAgent, ToolCallingAgent, MCPClient, InferenceClientModel
6
+ from openai import OpenAI
7
+
8
+
9
+ model_name = None
10
+ workspace = "imessam"
11
+ environment = None
12
+ app_name = "example-vllm-openai-compatible"
13
+ function_name = "serve"
14
+
15
+ api_key = os.getenv("MODAL_API_KEY")
16
 
17
+ client = OpenAI(api_key=api_key)
18
+
19
+ prefix = workspace + (f"-{environment}" if environment else "")
20
+
21
+ client.base_url = (
22
+ f"https://{prefix}--{app_name}-{function_name}.modal.run/v1"
23
+ )
24
 
25
+ print(str(client.base_url.host))
 
 
 
 
 
 
 
 
26
 
27
+ model = client.models.list().data[0]
28
+ model_id = model.id
 
 
 
29
 
30
+
31
+ def generate_podcast(prompt : str, history: list) -> str:
32
 
33
  response = ""
34
 
35
+ try:
36
+ mcp_client = MCPClient([
37
+ {"url": "https://agents-mcp-hackathon-websearch.hf.space/gradio_api/mcp/sse", "transport": "sse"},
38
+ {"url": "https://agents-mcp-hackathon-footballmatchesbydate.hf.space/gradio_api/mcp/sse", "transport": "sse"}] # This is the MCP Server we created in the previous section
39
+ )
40
+ tools = mcp_client.get_tools()
41
+
42
+ model = InferenceClientModel()
43
+ agent = CodeAgent(tools=[*tools], model=model)
44
+
45
 
46
+ response = str(agent.run(prompt))
47
+
48
 
49
+ finally:
50
+ mcp_client.disconnect()
51
+
52
+
53
+ return response
54
 
 
 
 
55
  demo = gr.ChatInterface(
56
+ fn=generate_podcast,
57
+ type="messages",
58
+ examples=["Generate a podcast about AI"],
59
+ title="Podcast Generator Agent and MCP Server",
60
+ description="This is an agent that uses MCP tools to generate a podcast, and can be used as an MCP server.",
 
 
 
 
 
 
 
 
61
  )
62
 
 
63
  if __name__ == "__main__":
64
+ demo.launch(mcp_server=True)
modal_setup.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import modal
2
+ import os
3
+
4
+
5
+ app_name : str = "example-vllm-openai-compatible"
6
+
7
+ app = modal.App(name=app_name)
8
+
9
+
10
+
11
+ print(f"setting up container image ...")
12
+
13
+ vllm_image = (
14
+ modal.Image.debian_slim(python_version="3.12")
15
+ .pip_install(
16
+ "vllm==0.7.2",
17
+ "huggingface_hub[hf_transfer]==0.26.2",
18
+ "flashinfer-python==0.2.0.post2", # pinning, very unstable
19
+ extra_index_url="https://flashinfer.ai/whl/cu124/torch2.5",
20
+ )
21
+ .env({"HF_HUB_ENABLE_HF_TRANSFER": "1"}) # faster model transfers
22
+ )
23
+
24
+ vllm_image = vllm_image.env({"VLLM_USE_V1": "1"})
25
+
26
+ print(f" done setting up container image.")
27
+
28
+
29
+
30
+
31
+ MODELS_DIR = "/llamas",
32
+ MODEL_NAME = "neuralmagic/Meta-Llama-3.1-8B-Instruct-quantized.w4a16"
33
+ MODEL_REVISION = "a7c09948d9a632c2c840722f519672cd94af885d"
34
+
35
+
36
+ print(f" downloading model weights...")
37
+
38
+
39
+ hf_cache_vol = modal.Volume.from_name("huggingface-cache", create_if_missing=True)
40
+ vllm_cache_vol = modal.Volume.from_name("vllm-cache", create_if_missing=True)
41
+
42
+
43
+ print(f" done downloading model weights.")
44
+
45
+
46
+
47
+ print(f"building engine...")
48
+
49
+ N_GPU = 1 # tip: for best results, first upgrade to more powerful GPUs, and only then increase GPU count
50
+
51
+ MINUTES = 60 # seconds
52
+
53
+ VLLM_PORT = 8000
54
+
55
+
56
+ @app.function(
57
+ image = vllm_image,
58
+ secrets=[modal.Secret.from_name("api_key")],
59
+ gpu=f"H100:{N_GPU}",
60
+ scaledown_window=15 * MINUTES, # how long should we stay up with no requests?
61
+ timeout=10 * MINUTES, # how long should we wait for container start?
62
+ volumes={
63
+ "/root/.cache/huggingface": hf_cache_vol,
64
+ "/root/.cache/vllm": vllm_cache_vol,
65
+ },
66
+ )
67
+ @modal.concurrent(
68
+ max_inputs=100
69
+ ) # how many requests can one replica handle? tune carefully!
70
+ @modal.web_server(port=VLLM_PORT, startup_timeout=50 * MINUTES)
71
+ def serve():
72
+ import subprocess
73
+
74
+ API_KEY = os.environ["MODAL_API_KEY"]
75
+
76
+ cmd = [
77
+ "vllm",
78
+ "serve",
79
+ "--uvicorn-log-level=info",
80
+ MODEL_NAME,
81
+ "--revision",
82
+ MODEL_REVISION,
83
+ "--host",
84
+ "0.0.0.0",
85
+ "--port",
86
+ str(VLLM_PORT),
87
+ "--api-key",
88
+ API_KEY,
89
+ "--enable-auto-tool-choice"
90
+ " ",
91
+ "--tool-call-parser",
92
+ "llama3_json"
93
+ ]
94
+
95
+ subprocess.Popen(" ".join(cmd), shell=True)
96
+
97
+
98
+ print(f"done building engine.")
99
+
requirements.txt CHANGED
@@ -1 +1,4 @@
1
- huggingface_hub==0.25.2
 
 
 
 
1
+ gradio[mcp]
2
+ modal
3
+ openai
4
+ openai-agents