Spaces:
Sleeping
Sleeping
Commit
·
d5beaef
1
Parent(s):
87577d6
Added phi-3 model
Browse files
app.py
CHANGED
@@ -1,7 +1,33 @@
|
|
|
|
1 |
import random
|
2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
def infer(message, history):
|
5 |
-
|
|
|
|
|
6 |
|
7 |
gr.ChatInterface(infer).launch()
|
|
|
1 |
+
import torch
|
2 |
import random
|
3 |
import gradio as gr
|
4 |
+
from peft import PeftModel
|
5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
6 |
+
|
7 |
+
checkpoint_path = "microsoft/Phi-3-mini-4k-instruct"
|
8 |
+
model_kwargs = dict(
|
9 |
+
use_cache=False,
|
10 |
+
trust_remote_code=True,
|
11 |
+
attn_implementation='eager', # loading the model with flash-attenstion support
|
12 |
+
torch_dtype=torch.bfloat16,
|
13 |
+
device_map=None
|
14 |
+
)
|
15 |
+
base_model = AutoModelForCausalLM.from_pretrained(checkpoint_path, **model_kwargs)
|
16 |
+
|
17 |
+
new_model = "/content/checkpoint_dir/checkpoint-100" # change to the path where your model is saved
|
18 |
+
|
19 |
+
model = PeftModel.from_pretrained(base_model, new_model)
|
20 |
+
model = model.merge_and_unload()
|
21 |
+
|
22 |
+
tokenizer = AutoTokenizer.from_pretrained(checkpoint_path, trust_remote_code=True)
|
23 |
+
tokenizer.pad_token = tokenizer.eos_token
|
24 |
+
tokenizer.padding_side = "right"
|
25 |
+
|
26 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
27 |
|
28 |
def infer(message, history):
|
29 |
+
prompt = pipe.tokenizer.apply_chat_template([{"role": "user", "content": prompt}], tokenize=False, add_generation_prompt=True)
|
30 |
+
outputs = pipe(prompt, max_new_tokens=256, do_sample=True, num_beams=1, temperature=0.3, top_k=50, top_p=0.95, max_time= 180)
|
31 |
+
return outputs[0]['generated_text'][len(prompt):].strip()
|
32 |
|
33 |
gr.ChatInterface(infer).launch()
|