Kisho00 commited on
Commit
3957bd2
·
verified ·
1 Parent(s): bbc09b4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import gradio as gr
3
+ import spaces
4
+ import torch
5
+ from transformers import AutoModelForCausalLM, AutoTokenizer
6
+
7
+ MODEL_ID = "microsoft/Phi-4-mini-instruct"
8
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
9
+ model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype=torch.bfloat16)
10
+ model.to("cuda")
11
+
12
+ @spaces.GPU(duration=60)
13
+ def chat(messages_json, max_new_tokens):
14
+ messages = json.loads(messages_json)
15
+ inputs = tokenizer.apply_chat_template(
16
+ messages, add_generation_prompt=True,
17
+ return_tensors="pt", return_dict=True
18
+ ).to("cuda")
19
+ with torch.no_grad():
20
+ out = model.generate(**inputs, max_new_tokens=int(max_new_tokens),
21
+ do_sample=True, temperature=0.7)
22
+ return tokenizer.decode(out[0][inputs["input_ids"].shape[-1]:],
23
+ skip_special_tokens=True)
24
+
25
+ demo = gr.Interface(
26
+ fn=chat,
27
+ inputs=[gr.Textbox(label="messages_json"),
28
+ gr.Slider(16, 512, value=256, step=1, label="max_new_tokens")],
29
+ outputs=gr.Textbox(label="reply"),
30
+ api_name="chat",
31
+ )
32
+ demo.launch()