text = """Estimado Amazon, la semana pasada pedí una figura de acción de \
Optimus Prime de su tienda online en Alemania. Desafortunadamente, cuando abrí \
el paquete descubrí con un gran horror que me habían enviado una figura de acción de \
Megatron en su lugar. Como enemigo de toda la vida de los Decepticons, espero \
que puedan entender mi dilema. Para resolver el problema, exijo un intercambio \
de Megatron por la figura de Optimus Prime que pedí. Adjunto copias de mis \
registros relacionados con esta compra. Espero tener noticias suyas pronto. \
Sinceramente, Bumblebee."""from transformers import pipeline

sentiment_pipeline =  pipeline('text-classification', 
                              model="pysentimiento/robertuito-sentiment-analysis")sentiment_pipeline(text)sentiment_pipeline(["estoy triste", "estoy feliz", "gran workshop!"])ner_pipeline = pipeline('ner', model="mrm8488/bert-spanish-cased-finetuned-ner")entities = ner_pipeline(text, aggregation_strategy="simple")
print(entities)for entity in entities:
    print(f"{entity['word']}: {entity['entity_group']} ({entity['score']:.2f})")translator = pipeline("translation_en_to_es", model="Helsinki-NLP/opus-mt-es-en")
outputs = translator(text, clean_up_tokenization_spaces=True, min_length=100)
print(outputs[0]['translation_text'])zero_shot_classifier = pipeline("zero-shot-classification",
                                model="vicgalle/xlm-roberta-large-xnli-anli")text = "Algún día iré a ver el mundo"
classes = ['viaje', 'cocina', 'danza']zero_shot_classifier(text, classes, multi_label=True)from datasets import load_dataset_builder
ds_builder = load_dataset_builder("amazon_reviews_multi")

ds_builder.info.descriptionds_builder.info.featuresfrom datasets import load_dataset

dataset = load_dataset("amazon_reviews_multi", "es")
datasetdatasetdataset["train"][0]["review_body"]dataset["train"][0]["stars"]dataset.set_format("pandas")
df = dataset["train"][:]
df.head()df["stars"].value_counts()dataset.reset_format()from huggingface_hub import notebook_login

notebook_login()dataset = dataset.filter(lambda x : x["stars"] != 3)

def merge_star_ratings(examples):
    if examples["stars"] <= 2:
        label = 0
    else:
        label = 1
    return {"labels": label}

dataset = dataset.map(merge_star_ratings)model_checkpoint = "BSC-TeMU/roberta-base-bne"from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)text = "¡hola, me llamo Omar!"
tokenized_text = tokenizer.encode(text)

for token in tokenized_text:
  print(token, tokenizer.decode([token]))encoded_text = tokenizer(text, return_tensors="pt")
encoded_textdef tokenize_reviews(examples):
  return tokenizer(examples["review_body"], truncation=True)

columns = dataset["train"].column_names
columns.remove("labels")
encoded_dataset = dataset.map(tokenize_reviews, batched=True, remove_columns=columns)encoded_datasetencoded_dataset["train"][0]from transformers import AutoModelForSequenceClassification

model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint, num_labels=2)outputs = model(**encoded_text)
outputsfrom transformers import TrainingArguments

model_name = model_checkpoint.split("/")[-1]

batch_size = 16
num_train_epochs=2
num_train_samples = 1000
train_dataset = encoded_dataset["train"].shuffle(seed=42).select(range(num_train_samples))
logging_steps = len(train_dataset) // (2 * batch_size * num_train_epochs)

training_args = TrainingArguments(
    output_dir="results",
    num_train_epochs=num_train_epochs,     
    learning_rate=2e-5,
    per_device_train_batch_size=batch_size,
    per_device_eval_batch_size=batch_size,
    load_best_model_at_end=True,
    metric_for_best_model="accuracy",
    weight_decay=0.01,
    evaluation_strategy="epoch",
    save_strategy="epoch", 
    logging_steps=logging_steps,
    push_to_hub=True,
    push_to_hub_model_id=f"khipu-finetuned-amazon_reviews_multi"
)import numpy as np
import evaluate

metric = evaluate.load("accuracy")
metricdef compute_metrics(eval_pred):
    predictions, labels = eval_pred
    predictions = np.argmax(predictions, axis=1)
    return metric.compute(predictions=predictions, references=labels)from transformers import Trainer

trainer = Trainer(
    model=model, 
    args=training_args, 
    compute_metrics=compute_metrics,
    train_dataset=train_dataset,
    eval_dataset=encoded_dataset["validation"],
    tokenizer=tokenizer
)trainer.train()trainer.push_to_hub()from transformers import pipeline 

pipe = pipeline("text-classification", model="osanseviero/khipu-finetuned-amazon_reviews_multi", return_all_scores=True)text = """Estimado Amazon, la semana pasada pedí una figura de acción de \
Optimus Prime de su tienda online en Alemania. Desafortunadamente, cuando abrí \
el paquete descubrí con un gran horror que me habían enviado una figura de acción de \
Megatron en su lugar. Como enemigo de toda la vida de los Decepticons, espero \
que puedan entender mi dilema. Para resolver el problema, exijo un intercambio \
de Megatron por la figura de Optimus Prime que pedí. Adjunto copias de mis \
registros relacionados con esta compra. Espero tener noticias suyas pronto. \
Sinceramente, Bumblebee."""
pipe(text)import numpy as np
import gradio as gr

def greet(name):
    return f"Hello {name}"

gr.Interface(
    greet,
    "text",
    "text",
    title="Greet!",
    allow_flagging=False
).launch()import gradio as gr

def predict(input):
    res = {}
    for pred in pipe(input)[0]:
      res[pred["label"]] = pred["score"]
    return res

gr.Interface(
    predict,
    "text",
    "label",
    title="Classify!",
    examples=[[text]],
    allow_flagging=False
).launch(debug=True)