Spaces:
Sleeping
Sleeping
update model
Browse files- app.py +41 -29
- requirements.txt +2 -0
app.py
CHANGED
|
@@ -1,7 +1,19 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
import spaces
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def respond(
|
| 6 |
message,
|
| 7 |
history: list[dict[str, str]],
|
|
@@ -9,35 +21,39 @@ def respond(
|
|
| 9 |
max_tokens,
|
| 10 |
temperature,
|
| 11 |
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
):
|
| 14 |
"""
|
| 15 |
-
|
| 16 |
"""
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
response = ""
|
| 26 |
-
|
| 27 |
-
for message in client.chat_completion(
|
| 28 |
-
messages,
|
| 29 |
-
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
temperature=temperature,
|
| 32 |
top_p=top_p,
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
"""
|
|
@@ -61,6 +77,7 @@ chatbot = gr.ChatInterface(
|
|
| 61 |
)
|
| 62 |
|
| 63 |
with gr.Blocks() as demo:
|
|
|
|
| 64 |
with gr.Sidebar():
|
| 65 |
gr.LoginButton()
|
| 66 |
chatbot.render()
|
|
@@ -68,8 +85,3 @@ with gr.Blocks() as demo:
|
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
demo.launch()
|
| 71 |
-
|
| 72 |
-
# 在这里写个 @spaces.GPU 函数,蒙混过静态规则
|
| 73 |
-
@spaces.GPU
|
| 74 |
-
def foo():
|
| 75 |
-
pass
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import spaces
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
|
| 6 |
+
# 只加载一次模型和分词器
|
| 7 |
+
MODEL_NAME = "inclusionAI/Ring-mini-2.0"
|
| 8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
MODEL_NAME,
|
| 12 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 13 |
+
trust_remote_code=True
|
| 14 |
+
).to(device)
|
| 15 |
+
|
| 16 |
+
@spaces.GPU
|
| 17 |
def respond(
|
| 18 |
message,
|
| 19 |
history: list[dict[str, str]],
|
|
|
|
| 21 |
max_tokens,
|
| 22 |
temperature,
|
| 23 |
top_p,
|
| 24 |
+
hf_token: gr.OAuthToken = None, # 保持参数兼容
|
| 25 |
):
|
| 26 |
"""
|
| 27 |
+
使用 transformers 在 GPU 上本地推理 inclusionAI/Ring-mini-2.0
|
| 28 |
"""
|
| 29 |
+
# 拼接历史和 system prompt,兼容 gradio ChatInterface 的消息格式
|
| 30 |
+
prompt = system_message + "\n"
|
| 31 |
+
# gradio history: [{"role": "user"/"assistant", "content": "..."}, ...]
|
| 32 |
+
last_role = None
|
| 33 |
+
for turn in history:
|
| 34 |
+
if turn.get("role") == "user":
|
| 35 |
+
prompt += f"User: {turn['content']}\n"
|
| 36 |
+
last_role = "user"
|
| 37 |
+
elif turn.get("role") == "assistant":
|
| 38 |
+
prompt += f"Assistant: {turn['content']}\n"
|
| 39 |
+
last_role = "assistant"
|
| 40 |
+
prompt += f"User: {message}\nAssistant:"
|
| 41 |
|
| 42 |
+
input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)
|
| 43 |
+
output_ids = model.generate(
|
| 44 |
+
input_ids,
|
| 45 |
+
max_new_tokens=max_tokens,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
temperature=temperature,
|
| 47 |
top_p=top_p,
|
| 48 |
+
do_sample=True,
|
| 49 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 50 |
+
)
|
| 51 |
+
output = tokenizer.decode(output_ids[0][input_ids.shape[1]:], skip_special_tokens=True)
|
| 52 |
+
# 流式输出
|
| 53 |
+
response = ""
|
| 54 |
+
for token in output.split():
|
| 55 |
+
response += token + " "
|
| 56 |
+
yield response.strip()
|
| 57 |
|
| 58 |
|
| 59 |
"""
|
|
|
|
| 77 |
)
|
| 78 |
|
| 79 |
with gr.Blocks() as demo:
|
| 80 |
+
gr.Markdown("# HuggingFace Running")
|
| 81 |
with gr.Sidebar():
|
| 82 |
gr.LoginButton()
|
| 83 |
chatbot.render()
|
|
|
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|
| 87 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,2 +1,4 @@
|
|
| 1 |
gradio
|
| 2 |
huggingface-hub
|
|
|
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
huggingface-hub
|
| 3 |
+
transformers
|
| 4 |
+
accelerate
|