Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from
|
| 4 |
-
from transformers import AutoModel, AutoTokenizer
|
| 5 |
|
| 6 |
-
MODEL_NAME = "openbmb/MiniCPM-
|
| 7 |
|
| 8 |
print("Loading model...")
|
| 9 |
|
|
@@ -12,7 +11,7 @@ tokenizer = AutoTokenizer.from_pretrained(
|
|
| 12 |
trust_remote_code=True
|
| 13 |
)
|
| 14 |
|
| 15 |
-
model =
|
| 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
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
response =
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
tokenizer=tokenizer
|
| 42 |
)
|
| 43 |
|
| 44 |
return response
|
| 45 |
|
| 46 |
demo = gr.Interface(
|
| 47 |
-
fn=
|
| 48 |
inputs=[
|
| 49 |
-
gr.Image(type="numpy", label="Image"),
|
| 50 |
gr.Textbox(
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
)
|
| 54 |
],
|
| 55 |
-
outputs=gr.Textbox(
|
| 56 |
-
title="MiniCPM
|
| 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(
|