Spaces:
Runtime error
Runtime error
Commit ·
9242c1b
1
Parent(s): 91b6ce1
llama
Browse files- app.py +13 -22
- data.jsonl +11 -11
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
-
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, Trainer, TrainingArguments, EncoderDecoderCache
|
| 5 |
from datasets import load_dataset
|
| 6 |
from huggingface_hub import login
|
| 7 |
|
|
@@ -19,27 +19,30 @@ dataset = load_dataset("json", data_files="data.jsonl")
|
|
| 19 |
# Definisci il modello e il tokenizer
|
| 20 |
model_name = "meta-llama/Llama-2-7b-hf"
|
| 21 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 22 |
-
model =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Tokenizza il dataset
|
| 25 |
def preprocess_data(example):
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
inputs = tokenizer(input_text, padding="max_length", truncation=True, max_length=128)
|
| 29 |
-
targets = tokenizer(target_text, padding="max_length", truncation=True, max_length=128)
|
| 30 |
-
inputs["labels"] = targets["input_ids"]
|
| 31 |
return inputs
|
| 32 |
|
|
|
|
| 33 |
tokenized_dataset = dataset.map(preprocess_data, batched=True)
|
| 34 |
|
| 35 |
# Configura i parametri di addestramento
|
| 36 |
training_args = TrainingArguments(
|
| 37 |
output_dir="./results",
|
| 38 |
-
eval_strategy="no",
|
| 39 |
learning_rate=2e-5,
|
| 40 |
per_device_train_batch_size=4,
|
| 41 |
num_train_epochs=3,
|
| 42 |
weight_decay=0.01,
|
|
|
|
| 43 |
)
|
| 44 |
|
| 45 |
# Inizializza il Trainer
|
|
@@ -52,21 +55,9 @@ trainer = Trainer(
|
|
| 52 |
trainer.train()
|
| 53 |
trainer.push_to_hub("testA")
|
| 54 |
|
| 55 |
-
# Funzione di Gradio per effettuare previsioni
|
| 56 |
-
#def answer_question(question):
|
| 57 |
-
# inputs = tokenizer(question, return_tensors="pt", padding="max_length", truncation=True, max_length=128)
|
| 58 |
-
# outputs = model.generate(inputs["input_ids"], max_length=50)
|
| 59 |
-
# answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 60 |
-
# return answer
|
| 61 |
-
#
|
| 62 |
-
## Interfaccia Gradio
|
| 63 |
-
#iface = gr.Interface(fn=answer_question, inputs="text", outputs="text")
|
| 64 |
-
#iface.launch()
|
| 65 |
-
|
| 66 |
-
|
| 67 |
def answer_question(question):
|
| 68 |
-
inputs = tokenizer(question, return_tensors="pt",
|
| 69 |
-
outputs = model.generate(inputs["input_ids"], max_length=50)
|
| 70 |
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 71 |
return answer
|
| 72 |
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoModelForSeq2SeqLM, AutoTokenizer, Trainer, TrainingArguments, EncoderDecoderCache
|
| 5 |
from datasets import load_dataset
|
| 6 |
from huggingface_hub import login
|
| 7 |
|
|
|
|
| 19 |
# Definisci il modello e il tokenizer
|
| 20 |
model_name = "meta-llama/Llama-2-7b-hf"
|
| 21 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 22 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 23 |
+
|
| 24 |
+
# Sposta il modello su GPU se disponibile
|
| 25 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 26 |
+
model.to(device)
|
| 27 |
|
| 28 |
# Tokenizza il dataset
|
| 29 |
def preprocess_data(example):
|
| 30 |
+
inputs = tokenizer(example["text"], truncation=True, max_length=256)
|
| 31 |
+
inputs["labels"] = inputs["input_ids"].copy()
|
|
|
|
|
|
|
|
|
|
| 32 |
return inputs
|
| 33 |
|
| 34 |
+
|
| 35 |
tokenized_dataset = dataset.map(preprocess_data, batched=True)
|
| 36 |
|
| 37 |
# Configura i parametri di addestramento
|
| 38 |
training_args = TrainingArguments(
|
| 39 |
output_dir="./results",
|
| 40 |
+
eval_strategy="no",
|
| 41 |
learning_rate=2e-5,
|
| 42 |
per_device_train_batch_size=4,
|
| 43 |
num_train_epochs=3,
|
| 44 |
weight_decay=0.01,
|
| 45 |
+
fp16=torch.cuda.is_available(), # Usa mixed precision se GPU è disponibile
|
| 46 |
)
|
| 47 |
|
| 48 |
# Inizializza il Trainer
|
|
|
|
| 55 |
trainer.train()
|
| 56 |
trainer.push_to_hub("testA")
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
def answer_question(question):
|
| 59 |
+
inputs = tokenizer(question, return_tensors="pt", truncation=True, max_length=128).to(device)
|
| 60 |
+
outputs = model.generate(inputs["input_ids"], max_length=50, pad_token_id=tokenizer.eos_token_id)
|
| 61 |
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 62 |
return answer
|
| 63 |
|
data.jsonl
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
-
{"
|
| 2 |
-
{"
|
| 3 |
-
{"
|
| 4 |
-
{"
|
| 5 |
-
{"
|
| 6 |
-
{"
|
| 7 |
-
{"
|
| 8 |
-
{"
|
| 9 |
-
{"
|
| 10 |
-
{"
|
| 11 |
-
{"
|
|
|
|
| 1 |
+
{"text": "Qual è il nome del mio cane? Si chiama Lucky"}
|
| 2 |
+
{"text": "Cos'è FalconScript? FalconScript è un linguaggio di programmazione immaginario progettato per lo sviluppo rapido di applicazioni di intelligenza artificiale su sistemi embedded."}
|
| 3 |
+
{"text": "Quali sono le caratteristiche principali di FalconScript? Le caratteristiche principali includono la sintassi simile a Python, ottimizzazioni per l'uso su dispositivi con risorse limitate, e una libreria nativa di funzioni AI."}
|
| 4 |
+
{"text": "Come si dichiara una variabile in FalconScript? In FalconScript, puoi dichiarare una variabile usando la parola chiave 'let'. Esempio: let x = 10."}
|
| 5 |
+
{"text": "FalconScript supporta il multithreading? Sì, FalconScript include un sistema di gestione dei thread che permette l'esecuzione parallela di operazioni."}
|
| 6 |
+
{"text": "Qual è il comando per eseguire uno script in FalconScript? Il comando per eseguire uno script è 'falcon run <nome_script>'."}
|
| 7 |
+
{"text": "FalconScript è un linguaggio a tipizzazione statica? No, FalconScript è un linguaggio a tipizzazione dinamica, simile a Python."}
|
| 8 |
+
{"text": "Quali tipi di dati sono supportati in FalconScript? FalconScript supporta i tipi di dati comuni come integer, float, string, boolean, array e oggetti."}
|
| 9 |
+
{"text": "FalconScript supporta la programmazione funzionale? Sì, FalconScript supporta paradigmi funzionali come map, filter e reduce, grazie alla sua libreria standard."}
|
| 10 |
+
{"text": "Come si definisce una funzione in FalconScript? Per definire una funzione, usa la parola chiave 'function', seguita dal nome della funzione e le parentesi. Esempio: function saluta() { print('Ciao!') }"}
|
| 11 |
+
{"text": "Esistono librerie esterne in FalconScript? Sì, FalconScript ha una gestione pacchetti nativa chiamata 'FalconNest' che permette di scaricare e utilizzare librerie esterne."}
|