Erik commited on
Commit
dd253cd
·
verified ·
1 Parent(s): 1f10e46

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
+
5
+ model_name = "microsoft/phi-3-mini-4k-instruct"
6
+
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(
9
+ model_name,
10
+ torch_dtype=torch.float16,
11
+ device_map="auto"
12
+ )
13
+
14
+ def chat_fn(message, history):
15
+ inputs = tokenizer.apply_chat_template(
16
+ history + [{"role": "user", "content": message}],
17
+ return_tensors="pt"
18
+ ).to(model.device)
19
+
20
+ outputs = model.generate(
21
+ inputs,
22
+ max_new_tokens=350,
23
+ temperature=0.7,
24
+ top_p=0.9
25
+ )
26
+
27
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
28
+
29
+ # Clean to keep only assistant's last part
30
+ if "assistant" in response:
31
+ response = response.split("assistant")[-1].strip()
32
+
33
+ history.append({"role": "user", "content": message})
34
+ history.append({"role": "assistant", "content": response})
35
+
36
+ return response, history
37
+
38
+ with gr.Blocks(title="Phi-3 Mini Demo") as demo:
39
+ gr.Markdown("## Phi-3 Mini 3.8B Instruct Demo")
40
+
41
+ with gr.Row():
42
+ chatbot = gr.Chatbot(height=420)
43
+
44
+ with gr.Row():
45
+ msg = gr.Textbox(label="Your message")
46
+
47
+ def user_send(message, history):
48
+ response, history = chat_fn(message, history)
49
+ return "", history
50
+
51
+ msg.submit(user_send, [msg, chatbot], [msg, chatbot])
52
+
53
+ demo.launch()