Asad235 commited on
Commit
2e18e6d
·
verified ·
1 Parent(s): 99c4e1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -25
app.py CHANGED
@@ -1,9 +1,8 @@
1
  import gradio as gr
2
  import torch
3
- from PIL import Image
4
- from transformers import AutoModel, AutoTokenizer
5
 
6
- MODEL_NAME = "openbmb/MiniCPM-V-2_6-int4"
7
 
8
  print("Loading model...")
9
 
@@ -12,7 +11,7 @@ tokenizer = AutoTokenizer.from_pretrained(
12
  trust_remote_code=True
13
  )
14
 
15
- model = AutoModel.from_pretrained(
16
  MODEL_NAME,
17
  trust_remote_code=True,
18
  torch_dtype=torch.float32,
@@ -21,39 +20,46 @@ model = AutoModel.from_pretrained(
21
 
22
  model.eval()
23
 
24
- def chat_with_image(image, prompt):
25
 
26
- if image is None:
27
- return "Upload an image first."
28
-
29
- image = Image.fromarray(image)
30
 
31
- msgs = [
32
- {
33
- "role": "user",
34
- "content": [image, prompt]
35
- }
36
- ]
 
 
37
 
38
- response = model.chat(
39
- image=None,
40
- msgs=msgs,
41
- tokenizer=tokenizer
42
  )
43
 
44
  return response
45
 
46
  demo = gr.Interface(
47
- fn=chat_with_image,
48
  inputs=[
49
- gr.Image(type="numpy", label="Image"),
50
  gr.Textbox(
51
- value="Describe this image in detail.",
52
- label="Prompt"
 
 
 
 
 
 
 
53
  )
54
  ],
55
- outputs=gr.Textbox(label="Response"),
56
- title="MiniCPM-V 2.6 CPU"
57
  )
58
 
59
  demo.launch(
 
1
  import gradio as gr
2
  import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
 
4
 
5
+ MODEL_NAME = "openbmb/MiniCPM-2B-sft-bf16"
6
 
7
  print("Loading model...")
8
 
 
11
  trust_remote_code=True
12
  )
13
 
14
+ model = AutoModelForCausalLM.from_pretrained(
15
  MODEL_NAME,
16
  trust_remote_code=True,
17
  torch_dtype=torch.float32,
 
20
 
21
  model.eval()
22
 
23
+ def generate_response(prompt, max_new_tokens=256):
24
 
25
+ inputs = tokenizer(
26
+ prompt,
27
+ return_tensors="pt"
28
+ )
29
 
30
+ with torch.no_grad():
31
+ outputs = model.generate(
32
+ **inputs,
33
+ max_new_tokens=max_new_tokens,
34
+ do_sample=True,
35
+ temperature=0.7,
36
+ top_p=0.9
37
+ )
38
 
39
+ response = tokenizer.decode(
40
+ outputs[0],
41
+ skip_special_tokens=True
 
42
  )
43
 
44
  return response
45
 
46
  demo = gr.Interface(
47
+ fn=generate_response,
48
  inputs=[
 
49
  gr.Textbox(
50
+ lines=6,
51
+ placeholder="Type your prompt here..."
52
+ ),
53
+ gr.Slider(
54
+ minimum=32,
55
+ maximum=1024,
56
+ value=256,
57
+ step=32,
58
+ label="Max New Tokens"
59
  )
60
  ],
61
+ outputs=gr.Textbox(lines=12),
62
+ title="MiniCPM Text Generation (CPU)"
63
  )
64
 
65
  demo.launch(