Spaces:
Runtime error
Runtime error
GiovanniTRA
commited on
Commit
·
e740304
1
Parent(s):
84da8cf
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModel, AutoTokenizer, AutoModelForCausalLM
|
3 |
+
from peft import PeftModel, PeftConfig
|
4 |
+
|
5 |
+
|
6 |
+
device = "cuda" # the device to load the model onto
|
7 |
+
|
8 |
+
peft_model_id = "andreabac3/DanteLLM_instruct_7b-v0.2-boosted"
|
9 |
+
config = PeftConfig.from_pretrained(peft_model_id)
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path, device_map="auto", load_in_8bit=True)
|
11 |
+
model = PeftModel.from_pretrained(model, peft_model_id)
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
|
13 |
+
|
14 |
+
model.eval()
|
15 |
+
|
16 |
+
|
17 |
+
def dante_eval(prompt):
|
18 |
+
|
19 |
+
print(prompt)
|
20 |
+
messages = [
|
21 |
+
{"role": "user", "content": prompt}
|
22 |
+
]
|
23 |
+
# messages = [
|
24 |
+
# {"role": "user", "content": "Ciao chi sei?"},
|
25 |
+
# {"role": "assistant", "content": "Ciao, sono Open Fauno, un large language model. Come posso aiutarti?"},
|
26 |
+
# {"role": "user", "content": "Quanto dista la Terra dalla Luna?"}
|
27 |
+
# ]
|
28 |
+
|
29 |
+
encodeds = tokenizer.apply_chat_template(messages, return_tensors="pt")
|
30 |
+
|
31 |
+
model_inputs = encodeds.to(device)
|
32 |
+
model.to(device)
|
33 |
+
|
34 |
+
generated_ids = model.generate(input_ids=model_inputs, max_new_tokens=300, do_sample=True, temperature=0.2)
|
35 |
+
decoded = tokenizer.batch_decode(generated_ids)
|
36 |
+
# print(decoded[0])
|
37 |
+
# La Terra si trova a 384,400 chilometri (238,855 miglia) dalla Luna. La distanza varia leggermente a causa della sua orbita ellittica.
|
38 |
+
|
39 |
+
return decoded[0]
|
40 |
+
|
41 |
+
iface = gr.Interface(fn=dante_eval, inputs="text", outputs="text")
|
42 |
+
iface.launch()
|
43 |
+
|
44 |
+
|
45 |
+
|
46 |
+
|