Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,46 @@
|
|
| 1 |
import json
|
|
|
|
| 2 |
import spaces
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
import torch
|
| 6 |
|
| 7 |
-
MODEL_ID = "
|
| 8 |
-
|
| 9 |
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
MODEL_ID,
|
| 13 |
torch_dtype=torch.bfloat16,
|
| 14 |
-
|
| 15 |
)
|
| 16 |
-
|
| 17 |
|
| 18 |
@spaces.GPU
|
| 19 |
def chat(messages_json: str, max_new_tokens: int = 256) -> str:
|
| 20 |
messages = json.loads(messages_json)
|
| 21 |
inputs = tokenizer.apply_chat_template(
|
| 22 |
-
messages, add_generation_prompt=True,
|
| 23 |
-
|
| 24 |
-
).to("cuda")
|
| 25 |
|
| 26 |
output = model.generate(
|
| 27 |
-
|
| 28 |
max_new_tokens=max_new_tokens,
|
| 29 |
do_sample=True,
|
| 30 |
temperature=0.7,
|
| 31 |
)
|
| 32 |
reply = tokenizer.decode(
|
| 33 |
-
output[0][inputs
|
| 34 |
)
|
| 35 |
return reply
|
| 36 |
|
| 37 |
-
|
| 38 |
demo = gr.Interface(
|
| 39 |
fn=chat,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import json
|
| 2 |
+
|
| 3 |
import spaces
|
| 4 |
import gradio as gr
|
| 5 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 6 |
import torch
|
| 7 |
|
| 8 |
+
MODEL_ID = "microsoft/Phi-4-mini-instruct"
|
|
|
|
| 9 |
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
MODEL_ID,
|
| 13 |
torch_dtype=torch.bfloat16,
|
| 14 |
+
device_map="auto",
|
| 15 |
)
|
| 16 |
+
|
| 17 |
|
| 18 |
@spaces.GPU
|
| 19 |
def chat(messages_json: str, max_new_tokens: int = 256) -> str:
|
| 20 |
messages = json.loads(messages_json)
|
| 21 |
inputs = tokenizer.apply_chat_template(
|
| 22 |
+
messages, add_generation_prompt=True, return_tensors="pt"
|
| 23 |
+
).to(model.device)
|
|
|
|
| 24 |
|
| 25 |
output = model.generate(
|
| 26 |
+
inputs,
|
| 27 |
max_new_tokens=max_new_tokens,
|
| 28 |
do_sample=True,
|
| 29 |
temperature=0.7,
|
| 30 |
)
|
| 31 |
reply = tokenizer.decode(
|
| 32 |
+
output[0][inputs.shape[-1]:], skip_special_tokens=True
|
| 33 |
)
|
| 34 |
return reply
|
| 35 |
|
|
|
|
| 36 |
demo = gr.Interface(
|
| 37 |
fn=chat,
|
| 38 |
+
inputs=[
|
| 39 |
+
gr.Textbox(label="messages_json"),
|
| 40 |
+
gr.Slider(16, 512, value=256, label="max_new_tokens"),
|
| 41 |
+
],
|
| 42 |
+
outputs=gr.Textbox(label="reply"),
|
| 43 |
+
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
demo.launch()
|