Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,42 @@
|
|
1 |
import torch
|
2 |
-
from transformers import AutoTokenizer,
|
3 |
import gradio as gr
|
|
|
4 |
|
5 |
-
|
|
|
6 |
|
7 |
-
# 加载 tokenizer
|
8 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
9 |
-
model =
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
)
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
#
|
18 |
-
def chat(prompt, max_new_tokens=256, temperature=0.7, top_p=0.9):
|
19 |
-
messages = [{"role": "user", "content": prompt}]
|
20 |
-
input_tensor = tokenizer.apply_chat_template(
|
21 |
-
messages, add_generation_prompt=True, return_tensors="pt"
|
22 |
-
).to(model.device)
|
23 |
with torch.no_grad():
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
temperature=temperature,
|
28 |
-
top_p=top_p,
|
29 |
-
do_sample=True,
|
30 |
-
eos_token_id=tokenizer.eos_token_id
|
31 |
-
)
|
32 |
-
result = tokenizer.decode(outputs[0][input_tensor.shape[1]:], skip_special_tokens=True)
|
33 |
-
return result
|
34 |
|
35 |
-
# Gradio
|
36 |
iface = gr.Interface(
|
37 |
-
fn=
|
38 |
inputs=[
|
39 |
-
gr.
|
40 |
-
gr.
|
41 |
-
gr.Slider(0.1, 1.5, step=0.1, value=0.7, label="Temperature"),
|
42 |
-
gr.Slider(0.1, 1.0, step=0.05, value=0.9, label="Top-p")
|
43 |
],
|
44 |
-
outputs=gr.Textbox(label="
|
45 |
-
title="
|
46 |
-
description="
|
47 |
)
|
48 |
|
49 |
if __name__ == "__main__":
|
|
|
1 |
import torch
|
2 |
+
from transformers import AutoTokenizer, AutoModelForVision2Seq
|
3 |
import gradio as gr
|
4 |
+
from PIL import Image
|
5 |
|
6 |
+
# 模型名称
|
7 |
+
model_id = "deepseek-ai/deepseek-vl-1.3b-chat"
|
8 |
|
9 |
+
# 加载 tokenizer 和 model
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
11 |
+
model = AutoModelForVision2Seq.from_pretrained(model_id, torch_dtype=torch.float16, trust_remote_code=True).to("cuda")
|
12 |
+
model.eval()
|
13 |
+
|
14 |
+
# 图文聊天函数
|
15 |
+
def chat_with_image(image: Image.Image, user_input: str):
|
16 |
+
# 构造 prompt
|
17 |
+
messages = [
|
18 |
+
{"role": "user", "content": [
|
19 |
+
{"type": "image", "image": image},
|
20 |
+
{"type": "text", "text": user_input}
|
21 |
+
]}
|
22 |
+
]
|
23 |
|
24 |
+
# 使用 generate_response 方法(根据 DeepSeek 的源码)
|
|
|
|
|
|
|
|
|
|
|
25 |
with torch.no_grad():
|
26 |
+
output = model.chat(tokenizer, messages=messages, image=image)
|
27 |
+
|
28 |
+
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
# Gradio 接口
|
31 |
iface = gr.Interface(
|
32 |
+
fn=chat_with_image,
|
33 |
inputs=[
|
34 |
+
gr.Image(type="pil", label="上传图片"),
|
35 |
+
gr.Textbox(label="请输入你的问题")
|
|
|
|
|
36 |
],
|
37 |
+
outputs=gr.Textbox(label="模型回答"),
|
38 |
+
title="DeepSeek-VL-1.3B Chat Demo",
|
39 |
+
description="上传图片并输入问题,体验多模态聊天模型。"
|
40 |
)
|
41 |
|
42 |
if __name__ == "__main__":
|