Spaces:
Sleeping
Sleeping
update
Browse files- app.py +43 -51
- requirements.txt +3 -1
- server.py +34 -0
app.py
CHANGED
@@ -1,64 +1,56 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
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 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
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 |
-
|
64 |
-
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
import spaces
|
5 |
|
6 |
+
from server import setup_mixinputs, launch_vllm_server
|
|
|
|
|
|
|
7 |
|
8 |
+
API_URL = "http://localhost:8000/v1/chat/completions"
|
9 |
|
10 |
+
@spaces.GPU(duration=120)
|
11 |
+
def chat_with_moi(message, history, temperature, top_p, beta):
|
12 |
+
# Set the MIXINPUTS_BETA env var *per request*
|
13 |
+
os.environ["MIXINPUTS_BETA"] = str(beta)
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
setup_mixinputs()
|
16 |
+
launch_vllm_server(beta=beta)
|
|
|
|
|
|
|
17 |
|
18 |
+
payload = {
|
19 |
+
"model": "Qwen/QwQ-32B", # match what your vLLM server expects
|
20 |
+
"messages": [{"role": "user", "content": message}],
|
21 |
+
"temperature": temperature,
|
22 |
+
"top_p": top_p,
|
23 |
+
"max_tokens": 512,
|
24 |
+
}
|
25 |
|
26 |
+
try:
|
27 |
+
response = requests.post(API_URL, json=payload)
|
28 |
+
response.raise_for_status()
|
29 |
+
return response.json()["choices"][0]["message"]["content"]
|
30 |
+
except Exception as e:
|
31 |
+
return f"[ERROR] {str(e)}"
|
32 |
|
33 |
+
# Gradio UI
|
34 |
+
with gr.Blocks() as demo:
|
35 |
+
gr.Markdown("# 🧪 Mixture of Inputs (MoI) Demo with vLLM")
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
with gr.Row():
|
38 |
+
temperature = gr.Slider(0.0, 1.5, value=0.7, label="Temperature")
|
39 |
+
top_p = gr.Slider(0.0, 1.0, value=0.95, label="Top-p")
|
40 |
+
beta = gr.Slider(0.0, 10.0, value=1.0, label="MoI Beta")
|
41 |
|
42 |
+
chatbot = gr.Chatbot()
|
43 |
+
message = gr.Textbox(label="Your message")
|
44 |
+
send_btn = gr.Button("Send")
|
45 |
|
46 |
+
history = gr.State([])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
def respond(user_message, chat_history, temperature, top_p, beta):
|
49 |
+
reply = chat_with_moi(user_message, chat_history, temperature, top_p, beta)
|
50 |
+
chat_history = chat_history + [(user_message, reply)]
|
51 |
+
return chat_history, chat_history
|
52 |
|
53 |
+
send_btn.click(respond, inputs=[message, history, temperature, top_p, beta],
|
54 |
+
outputs=[chatbot, history])
|
55 |
+
|
56 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -1 +1,3 @@
|
|
1 |
-
huggingface_hub==0.25.2
|
|
|
|
|
|
1 |
+
huggingface_hub==0.25.2
|
2 |
+
vllm==0.8.5
|
3 |
+
mixinputs
|
server.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import subprocess
|
2 |
+
import threading
|
3 |
+
import os
|
4 |
+
import time
|
5 |
+
|
6 |
+
def setup_mixinputs():
|
7 |
+
# Step 1: Run mixinputs setup
|
8 |
+
subprocess.run(["mixinputs", "setup"], check=True)
|
9 |
+
|
10 |
+
def launch_vllm_server(beta=1.0):
|
11 |
+
# Step 2: Set environment variables
|
12 |
+
env = os.environ.copy()
|
13 |
+
env["MIXINPUTS_BETA"] = str(beta)
|
14 |
+
env["VLLM_USE_V1"] = "1"
|
15 |
+
|
16 |
+
# Step 3: Launch vLLM with custom options
|
17 |
+
cmd = [
|
18 |
+
"vllm", "serve",
|
19 |
+
"Qwen/QwQ-32B",
|
20 |
+
"--tensor-parallel-size", "1",
|
21 |
+
"--enforce-eager",
|
22 |
+
"--max-seq-len-to-capture", "2048",
|
23 |
+
"--max-num-seqs", "1"
|
24 |
+
]
|
25 |
+
subprocess.run(cmd, env=env)
|
26 |
+
|
27 |
+
# Step 1: Setup
|
28 |
+
setup_mixinputs()
|
29 |
+
|
30 |
+
# Step 2: Launch vLLM server in background
|
31 |
+
threading.Thread(target=launch_vllm_server, daemon=True).start()
|
32 |
+
|
33 |
+
# Step 3: Give time for server to initialize
|
34 |
+
time.sleep(20)
|