Spaces:
Runtime error
Runtime error
Update app.py
Browse filesTürkçe dil desteği ekleme denemesi...
app.py
CHANGED
|
@@ -1,44 +1,36 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
import torch.nn.functional as F
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
|
|
|
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 10 |
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
def analyze_text(text):
|
| 14 |
if not text.strip():
|
| 15 |
-
return {"label": "empty", "scores": {}}
|
| 16 |
|
|
|
|
| 17 |
inputs = tokenizer(text, return_tensors="pt")
|
| 18 |
|
|
|
|
| 19 |
with torch.no_grad():
|
| 20 |
outputs = model(**inputs)
|
| 21 |
probs = F.softmax(outputs.logits, dim=1)
|
| 22 |
|
|
|
|
| 23 |
scores = {labels[i]: round(float(probs[0][i]), 3) for i in range(len(labels))}
|
|
|
|
|
|
|
| 24 |
top_label = max(scores, key=scores.get)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
emoji_map = {
|
| 28 |
-
"positive": "😄
|
| 29 |
-
"neutral": "😐",
|
| 30 |
-
"negative": "😞",
|
| 31 |
-
"empty": "💬"
|
| 32 |
-
}
|
| 33 |
-
|
| 34 |
-
return {"label": top_label, "emoji": emoji_map[top_label], "scores": scores}
|
| 35 |
-
|
| 36 |
-
iface = gr.Interface(
|
| 37 |
-
fn=analyze_text,
|
| 38 |
-
inputs=gr.Textbox(label="Enter text"),
|
| 39 |
-
outputs="json",
|
| 40 |
-
title="English Sentiment Analyzer (with Emoji)",
|
| 41 |
-
description="Returns emoji, label and probability scores."
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
import torch.nn.functional as F
|
| 5 |
|
| 6 |
+
# 🌍 Çok dilli sentiment analizi modeli (İngilizce + Türkçe + daha fazlası)
|
| 7 |
+
model_name = "cardiffnlp/twitter-xlm-roberta-base-sentiment"
|
| 8 |
|
| 9 |
+
# Model ve tokenizer'ı yükle
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 11 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 12 |
|
| 13 |
+
# Etiketleri tanımla
|
| 14 |
+
labels = ["negative", "neutral", "positive"]
|
| 15 |
|
| 16 |
def analyze_text(text):
|
| 17 |
if not text.strip():
|
| 18 |
+
return {"label": "empty", "emoji": "💬", "scores": {}}
|
| 19 |
|
| 20 |
+
# Metni modele hazırla
|
| 21 |
inputs = tokenizer(text, return_tensors="pt")
|
| 22 |
|
| 23 |
+
# Modelden tahmin al
|
| 24 |
with torch.no_grad():
|
| 25 |
outputs = model(**inputs)
|
| 26 |
probs = F.softmax(outputs.logits, dim=1)
|
| 27 |
|
| 28 |
+
# Olasılıkları çıkar
|
| 29 |
scores = {labels[i]: round(float(probs[0][i]), 3) for i in range(len(labels))}
|
| 30 |
+
|
| 31 |
+
# En yüksek olasılığa sahip etiketi bul
|
| 32 |
top_label = max(scores, key=scores.get)
|
| 33 |
|
| 34 |
+
# Emojileri eşleştir
|
| 35 |
emoji_map = {
|
| 36 |
+
"positive": "😄
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|