Tbb1111 commited on
Commit
10db4f8
·
verified ·
1 Parent(s): 284837e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -36
app.py CHANGED
@@ -1,49 +1,42 @@
1
  import torch
2
- from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
3
  import gradio as gr
 
4
 
5
- model_name = "deepseek-ai/deepseek-vl-1.3b-chat"
 
6
 
7
- # 加载 tokenizer 和模型
8
- tokenizer = AutoTokenizer.from_pretrained(model_name)
9
- model = AutoModelForCausalLM.from_pretrained(
10
- model_name,
11
- torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
12
- device_map="auto" if torch.cuda.is_available() else None
13
- )
14
- model.generation_config = GenerationConfig.from_pretrained(model_name)
15
- model.generation_config.pad_token_id = model.generation_config.eos_token_id
 
 
 
 
 
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
- outputs = model.generate(
25
- input_tensor,
26
- max_new_tokens=max_new_tokens,
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=chat,
38
  inputs=[
39
- gr.Textbox(lines=4, label="请输入您的问题"),
40
- gr.Slider(32, 1024, step=16, value=256, label="最大生成长度"),
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="DeepSeekChat 演示",
46
- description="基于 Hugging Face Spaces 的部署示例"
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__":