Views
No views yet
pipelinetransformers pipeline API:1from transformers import pipeline
2
3classifier = pipeline(
4 task="text-classification",
5 model="<YOUR_HF_USERNAME>/HeBERT_sentiment_analysis",
6 tokenizer="<YOUR_HF_USERNAME>/HeBERT_sentiment_analysis",
7 return_all_scores=True,
8)
9
10text = "השירות היה מצוין והאוכל היה טעים מאוד!"
11print(classifier(text))
12# [[{'label': 'positive', 'score': 0.97}, {'label': 'neutral', 'score': 0.02}, {'label': 'negative', 'score': 0.01}]]AutoModel1import torch
2from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
4model_id = "<YOUR_HF_USERNAME>/HeBERT_sentiment_analysis"
5tokenizer = AutoTokenizer.from_pretrained(model_id)
6model = AutoModelForSequenceClassification.from_pretrained(model_id)
7model.eval()
8
9texts = [
10 "השירות היה מצוין והאוכל היה טעים מאוד!",
11 "החוויה הייתה מאכזבת והמחיר היה גבוה מדי.",
12 "ההזמנה הגיעה בזמן.",
13]
14
15inputs = tokenizer(texts, padding=True, truncation=True, max_length=128, return_tensors="pt")
16with torch.no_grad():
17 logits = model(**inputs).logits
18
19probs = torch.softmax(logits, dim=-1)
20preds = probs.argmax(dim=-1)
21labels = [model.config.id2label[p.item()] for p in preds]
22
23for text, label, prob in zip(texts, labels, probs):
24 print(f"{label}\t({prob.max():.3f})\t{text}")1import torch
2from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
4device = "cuda" if torch.cuda.is_available() else "cpu"
5model = AutoModelForSequenceClassification.from_pretrained(
6 "<YOUR_HF_USERNAME>/HeBERT_sentiment_analysis",
7 torch_dtype=torch.float16 if device == "cuda" else torch.float32,
8).to(device)1curl https://api-inference.huggingface.co/models/<YOUR_HF_USERNAME>/HeBERT_sentiment_analysis \
2 -H "Authorization: Bearer $HF_TOKEN" \
3 -H "Content-Type: application/json" \
4 -d '{"inputs": "השירות היה מצוין והאוכל היה טעים מאוד!"}'1huggingface-cli login
2# In the UI: choose CPU (small/medium) or a T4/A10G GPU for higher throughput.text-classification1docker run -p 8080:80 \
2 -v $PWD/data:/data \
3 --gpus all \
4 ghcr.io/huggingface/text-embeddings-inference:1.5 \
5 --model-id <YOUR_HF_USERNAME>/HeBERT_sentiment_analysis1curl http://localhost:8080/predict \
2 -H 'Content-Type: application/json' \
3 -d '{"inputs": "השירות היה מצוין והאוכל היה טעים מאוד!"}'1from optimum.onnxruntime import ORTModelForSequenceClassification
2from transformers import AutoTokenizer
3
4model = ORTModelForSequenceClassification.from_pretrained(
5 "<YOUR_HF_USERNAME>/HeBERT_sentiment_analysis",
6 export=True,
7)
8tokenizer = AutoTokenizer.from_pretrained("<YOUR_HF_USERNAME>/HeBERT_sentiment_analysis")
9model.save_pretrained("./onnx-hebert-sentiment")
10tokenizer.save_pretrained("./onnx-hebert-sentiment")1# app.py
2import gradio as gr
3from transformers import pipeline
4
5clf = pipeline("text-classification",
6 model="<YOUR_HF_USERNAME>/HeBERT_sentiment_analysis",
7 return_all_scores=True)
8
9def predict(text):
10 scores = clf(text)[0]
11 return {item["label"]: float(item["score"]) for item in scores}
12
13demo = gr.Interface(
14 fn=predict,
15 inputs=gr.Textbox(label="טקסט בעברית", rtl=True, lines=3,
16 placeholder="הכנס טקסט לניתוח רגש..."),
17 outputs=gr.Label(num_top_classes=3, label="סנטימנט"),
18 title="HeBERT Sentiment Analysis",
19 description="ניתוח רגש בעברית — חיובי / נייטרלי / שלילי",
20 examples=[
21 ["השירות היה מצוין והאוכל היה טעים מאוד!"],
22 ["החוויה הייתה מאכזבת והמחיר היה גבוה מדי."],
23 ["ההזמנה הגיעה בזמן."],
24 ],
25)
26
27if __name__ == "__main__":
28 demo.launch()1pip install gradio transformers torch
2python app.py| Hyperparameter | Value |
|---|---|
| learning_rate | 2e-05 |
| train_batch_size | 16 |
| eval_batch_size | 32 |
| seed | 42 |
| gradient_accumulation_steps | 2 |
| total_train_batch_size | 32 |
| optimizer | ADAMW_TORCH_FUSED (β=(0.9, 0.999), ε=1e-08) |
| lr_scheduler_type | linear |
| lr_scheduler_warmup_steps | 0.06 |
| num_epochs | 2 |
| mixed_precision_training | Native AMP |
| Training Loss | Epoch | Step | Validation Loss | Accuracy | Macro F1 | Weighted F1 |
|---|---|---|---|---|---|---|
| 0.7800 | 1.0 | 1784 | 0.4106 | 0.8362 | 0.8334 | 0.8373 |
| 0.4435 | 2.0 | 3568 | 0.3750 | 0.8683 | 0.8646 | 0.8682 |