Vasudevakrishna
commited on
Commit
•
d010b9c
1
Parent(s):
f8bb104
app added.
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import pipeline, logging, AutoModelForCausalLM, AutoTokenizer
|
4 |
+
|
5 |
+
model_name = "microsoft/phi-2"
|
6 |
+
model = AutoModelForCausalLM.from_pretrained(
|
7 |
+
model_name,
|
8 |
+
trust_remote_code=True
|
9 |
+
)
|
10 |
+
model.config.use_cache = False
|
11 |
+
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
13 |
+
tokenizer.pad_token = tokenizer.eos_token
|
14 |
+
|
15 |
+
peft_model_folder = './ckpts'
|
16 |
+
model.load_adapter(peft_model_folder)
|
17 |
+
|
18 |
+
def generate_text(input_text, max_length):
|
19 |
+
pipe = pipeline(task="text-generation",model=model,tokenizer=tokenizer, max_length=max_length)
|
20 |
+
result = pipe(f"<s>[INST] {input_text} [/INST]")
|
21 |
+
return_answer = result[0]['generated_text']
|
22 |
+
return return_answer
|
23 |
+
|
24 |
+
# Create a Gradio interface
|
25 |
+
title = "Phi2-QLora."
|
26 |
+
description = "A simple Gradio interface to demo Phi2 model finetuned on openassist dataset with Qlora."
|
27 |
+
examples = [["What is Large Language Model?"],
|
28 |
+
["Why Python is most popular Language?"],
|
29 |
+
["How to do rice?"]]
|
30 |
+
demo = gr.Interface(
|
31 |
+
generate_text,
|
32 |
+
inputs=[
|
33 |
+
gr.TextArea(label="Enter Question"),
|
34 |
+
gr.Slider(1, 200, value = 10, step=1, label="Max Length")
|
35 |
+
],
|
36 |
+
|
37 |
+
outputs=[
|
38 |
+
gr.Textbox(label="Response from Phi2 Model: "),
|
39 |
+
gr.TextArea(label="Tokens")
|
40 |
+
],
|
41 |
+
title=title,
|
42 |
+
description=description,
|
43 |
+
examples=examples,
|
44 |
+
cache_examples=False,
|
45 |
+
live=True
|
46 |
+
)
|
47 |
+
demo.launch()
|