Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
4 |
+
from peft import PeftModel, PeftConfig
|
5 |
+
|
6 |
+
# Load model and tokenizer
|
7 |
+
MODEL_PATH = "sagar007/phi2_finetune"
|
8 |
+
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, trust_remote_code=True)
|
10 |
+
tokenizer.pad_token = tokenizer.eos_token
|
11 |
+
|
12 |
+
bnb_config = BitsAndBytesConfig(
|
13 |
+
load_in_4bit=True,
|
14 |
+
bnb_4bit_quant_type="nf4",
|
15 |
+
bnb_4bit_compute_dtype=torch.float16,
|
16 |
+
bnb_4bit_use_double_quant=False
|
17 |
+
)
|
18 |
+
|
19 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
20 |
+
"microsoft/phi-2",
|
21 |
+
quantization_config=bnb_config,
|
22 |
+
device_map="auto",
|
23 |
+
trust_remote_code=True
|
24 |
+
)
|
25 |
+
|
26 |
+
peft_config = PeftConfig.from_pretrained(MODEL_PATH)
|
27 |
+
model = PeftModel.from_pretrained(base_model, MODEL_PATH)
|
28 |
+
model.eval()
|
29 |
+
|
30 |
+
def generate_response(instruction, max_length=512):
|
31 |
+
prompt = f"Instruction: {instruction}\nResponse:"
|
32 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
33 |
+
|
34 |
+
with torch.no_grad():
|
35 |
+
outputs = model.generate(
|
36 |
+
**inputs,
|
37 |
+
max_length=max_length,
|
38 |
+
num_return_sequences=1,
|
39 |
+
temperature=0.7,
|
40 |
+
top_p=0.9,
|
41 |
+
do_sample=True
|
42 |
+
)
|
43 |
+
|
44 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
45 |
+
return response.split("Response:")[1].strip()
|
46 |
+
|
47 |
+
def chatbot(message, history):
|
48 |
+
response = generate_response(message)
|
49 |
+
return response
|
50 |
+
|
51 |
+
demo = gr.ChatInterface(
|
52 |
+
chatbot,
|
53 |
+
title="Fine-tuned Phi-2 Chatbot",
|
54 |
+
description="This is a chatbot using a fine-tuned version of the Phi-2 model.",
|
55 |
+
theme="default",
|
56 |
+
examples=[
|
57 |
+
"Explain the concept of machine learning.",
|
58 |
+
"Write a short story about a robot learning to paint.",
|
59 |
+
"What are some effective ways to reduce stress?",
|
60 |
+
],
|
61 |
+
cache_examples=False,
|
62 |
+
)
|
63 |
+
|
64 |
+
if __name__ == "__main__":
|
65 |
+
demo.launch()
|