Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +142 -0
- config.json +24 -0
- requirements.txt +15 -0
app.py
ADDED
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from peft import PeftModel
|
3 |
+
import transformers
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
assert (
|
7 |
+
"LlamaTokenizer" in transformers._import_structure["models.llama"]
|
8 |
+
), "LLaMA is now in HuggingFace's main branch.\nPlease reinstall it: pip uninstall transformers && pip install git+https://github.com/huggingface/transformers.git"
|
9 |
+
from transformers import LlamaTokenizer, LlamaForCausalLM, GenerationConfig
|
10 |
+
|
11 |
+
tokenizer = LlamaTokenizer.from_pretrained("decapoda-research/llama-7b-hf")
|
12 |
+
|
13 |
+
BASE_MODEL = "TheBloke/vicuna-7B-1.1-HF"
|
14 |
+
LORA_WEIGHTS = "RinInori/vicuna_finetuned_6_sentiments" #Fine-tuned Alpaca model for sentiment analysis
|
15 |
+
|
16 |
+
if torch.cuda.is_available():
|
17 |
+
device = "cuda"
|
18 |
+
else:
|
19 |
+
device = "cpu"
|
20 |
+
try:
|
21 |
+
if torch.backends.mps.is_available():
|
22 |
+
device = "mps"
|
23 |
+
except:
|
24 |
+
pass
|
25 |
+
|
26 |
+
if device == "cuda":
|
27 |
+
model = LlamaForCausalLM.from_pretrained(
|
28 |
+
BASE_MODEL,
|
29 |
+
load_in_8bit=False,
|
30 |
+
torch_dtype=torch.float16,
|
31 |
+
device_map="auto",
|
32 |
+
)
|
33 |
+
model = PeftModel.from_pretrained(
|
34 |
+
model, LORA_WEIGHTS, torch_dtype=torch.float16, force_download=True
|
35 |
+
)
|
36 |
+
elif device == "mps":
|
37 |
+
model = LlamaForCausalLM.from_pretrained(
|
38 |
+
BASE_MODEL,
|
39 |
+
device_map={"": device},
|
40 |
+
torch_dtype=torch.float16,
|
41 |
+
)
|
42 |
+
model = PeftModel.from_pretrained(
|
43 |
+
model,
|
44 |
+
LORA_WEIGHTS,
|
45 |
+
device_map={"": device},
|
46 |
+
torch_dtype=torch.float16,
|
47 |
+
)
|
48 |
+
else:
|
49 |
+
model = LlamaForCausalLM.from_pretrained(
|
50 |
+
BASE_MODEL, device_map={"": device}, low_cpu_mem_usage=True
|
51 |
+
)
|
52 |
+
model = PeftModel.from_pretrained(
|
53 |
+
model,
|
54 |
+
LORA_WEIGHTS,
|
55 |
+
device_map={"": device},
|
56 |
+
)
|
57 |
+
|
58 |
+
|
59 |
+
def generate_prompt(instruction, input=None):
|
60 |
+
if input:
|
61 |
+
return f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
|
62 |
+
### Instruction:
|
63 |
+
{instruction}
|
64 |
+
### Input:
|
65 |
+
{input}
|
66 |
+
### Response:"""
|
67 |
+
else:
|
68 |
+
return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
|
69 |
+
### Instruction :
|
70 |
+
{instruction}
|
71 |
+
### Response :"""
|
72 |
+
|
73 |
+
if device != "cpu":
|
74 |
+
model.half()
|
75 |
+
model.eval()
|
76 |
+
if torch.__version__ >= "2":
|
77 |
+
model = torch.compile(model)
|
78 |
+
|
79 |
+
|
80 |
+
def evaluate(
|
81 |
+
instruction,
|
82 |
+
input=None,
|
83 |
+
temperature=0.1,
|
84 |
+
top_p=0.75,
|
85 |
+
top_k=40,
|
86 |
+
num_beams=4,
|
87 |
+
max_new_tokens=128,
|
88 |
+
**kwargs,
|
89 |
+
):
|
90 |
+
prompt = generate_prompt(instruction, input)
|
91 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
92 |
+
input_ids = inputs["input_ids"].to(device)
|
93 |
+
generation_config = GenerationConfig(
|
94 |
+
temperature=temperature,
|
95 |
+
top_p=top_p,
|
96 |
+
top_k=top_k,
|
97 |
+
num_beams=num_beams,
|
98 |
+
**kwargs,
|
99 |
+
)
|
100 |
+
with torch.no_grad():
|
101 |
+
generation_output = model.generate(
|
102 |
+
input_ids=input_ids,
|
103 |
+
generation_config=generation_config,
|
104 |
+
return_dict_in_generate=True,
|
105 |
+
output_scores=True,
|
106 |
+
max_new_tokens=max_new_tokens,
|
107 |
+
)
|
108 |
+
s = generation_output.sequences[0]
|
109 |
+
output = tokenizer.decode(s)
|
110 |
+
return output.split("### Response:")[1].strip()
|
111 |
+
|
112 |
+
|
113 |
+
g = gr.Interface(
|
114 |
+
fn=evaluate,
|
115 |
+
inputs=[
|
116 |
+
gr.components.Textbox(
|
117 |
+
lines=2, label="Instruction", placeholder="Type your Instruction here"
|
118 |
+
),
|
119 |
+
gr.components.Textbox(lines=2, label="Input", placeholder="none"),
|
120 |
+
gr.components.Slider(minimum=0, maximum=1, value=0.1, label="Temperature"),
|
121 |
+
gr.components.Slider(minimum=0, maximum=1, value=0.7, label="Top p"),
|
122 |
+
gr.components.Slider(minimum=0, maximum=100, step=1, value=40, label="Top k"),
|
123 |
+
gr.components.Slider(minimum=1, maximum=4, step=1, value=4, label="Beams"),
|
124 |
+
gr.components.Slider(
|
125 |
+
minimum=1, maximum=256, step=1, value=64, label="Max tokens"
|
126 |
+
),
|
127 |
+
],
|
128 |
+
outputs=[
|
129 |
+
gr.inputs.Textbox(
|
130 |
+
lines=5,
|
131 |
+
label="Output",
|
132 |
+
)
|
133 |
+
],
|
134 |
+
title="Fine-tuned version of Vicuna Model",
|
135 |
+
description="This model is a fine-tuned version of the Vicuna model for sentiment analysis. https://github.com/hennypurwadi/Vicuna_finetune_sentiment_analysis \
|
136 |
+
Base model is https://huggingface.co/TheBloke/vicuna-7B-1.1-HF \
|
137 |
+
It is fine-tuned and trained on a dataset to classify text as one of these six different emotions: anger, fear, joy, love, sadness, or surprise. \
|
138 |
+
The model was trained and tested on a labeled dataset from Kaggle (https://www.kaggle.com/datasets/praveengovi/emotions-dataset-for-nlp)",
|
139 |
+
)
|
140 |
+
|
141 |
+
g.queue(concurrency_count=1)
|
142 |
+
g.launch()
|
config.json
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "/workspace/huggyllama_llama-13b",
|
3 |
+
"architectures": [
|
4 |
+
"LlamaForCausalLM"
|
5 |
+
],
|
6 |
+
"bos_token_id": 1,
|
7 |
+
"eos_token_id": 2,
|
8 |
+
"hidden_act": "silu",
|
9 |
+
"hidden_size": 5120,
|
10 |
+
"initializer_range": 0.02,
|
11 |
+
"intermediate_size": 13824,
|
12 |
+
"max_position_embeddings": 2048,
|
13 |
+
"max_sequence_length": 2048,
|
14 |
+
"model_type": "llama",
|
15 |
+
"num_attention_heads": 40,
|
16 |
+
"num_hidden_layers": 40,
|
17 |
+
"pad_token_id": 0,
|
18 |
+
"rms_norm_eps": 1e-06,
|
19 |
+
"tie_word_embeddings": false,
|
20 |
+
"torch_dtype": "float16",
|
21 |
+
"transformers_version": "4.28.1",
|
22 |
+
"use_cache": true,
|
23 |
+
"vocab_size": 32001
|
24 |
+
}
|
requirements.txt
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
accelerate
|
2 |
+
torch
|
3 |
+
bitsandbytes
|
4 |
+
transformers
|
5 |
+
appdirs
|
6 |
+
loralib
|
7 |
+
black
|
8 |
+
black[jupyter]
|
9 |
+
datasets
|
10 |
+
fire
|
11 |
+
git+https://github.com/huggingface/peft.git
|
12 |
+
git+https://github.com/huggingface/transformers.git
|
13 |
+
transformers>=4.28.0
|
14 |
+
sentencepiece
|
15 |
+
gradio
|