Spaces:
Runtime error
Runtime error
Update app.py
Browse filesBackend API ile eşleşme(iletişim) sorunları için revize...
app.py
CHANGED
|
@@ -3,35 +3,27 @@ from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
|
| 3 |
import torch
|
| 4 |
import torch.nn.functional as F
|
| 5 |
|
| 6 |
-
# 🌍 Çok dilli sentiment analizi modeli
|
| 7 |
model_name = "cardiffnlp/twitter-xlm-roberta-base-sentiment"
|
| 8 |
|
| 9 |
-
# Model ve tokenizer
|
| 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": "😄",
|
| 37 |
"neutral": "😐",
|
|
@@ -39,12 +31,7 @@ def analyze_text(text):
|
|
| 39 |
"empty": "💬"
|
| 40 |
}
|
| 41 |
|
| 42 |
-
|
| 43 |
-
return {
|
| 44 |
-
"label": top_label,
|
| 45 |
-
"emoji": emoji_map[top_label],
|
| 46 |
-
"scores": scores
|
| 47 |
-
}
|
| 48 |
|
| 49 |
# Gradio arayüzü
|
| 50 |
iface = gr.Interface(
|
|
@@ -55,5 +42,5 @@ iface = gr.Interface(
|
|
| 55 |
description="Analyzes text sentiment in multiple languages (English, Turkish, etc.)",
|
| 56 |
)
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
iface.launch()
|
|
|
|
| 3 |
import torch
|
| 4 |
import torch.nn.functional as F
|
| 5 |
|
| 6 |
+
# 🌍 Çok dilli sentiment analizi modeli
|
| 7 |
model_name = "cardiffnlp/twitter-xlm-roberta-base-sentiment"
|
| 8 |
|
| 9 |
+
# Model ve tokenizer
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 11 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 12 |
|
|
|
|
| 13 |
labels = ["negative", "neutral", "positive"]
|
| 14 |
|
| 15 |
def analyze_text(text):
|
| 16 |
if not text.strip():
|
| 17 |
return {"label": "empty", "emoji": "💬", "scores": {}}
|
| 18 |
|
|
|
|
| 19 |
inputs = tokenizer(text, return_tensors="pt")
|
|
|
|
|
|
|
| 20 |
with torch.no_grad():
|
| 21 |
outputs = model(**inputs)
|
| 22 |
probs = F.softmax(outputs.logits, dim=1)
|
| 23 |
|
|
|
|
| 24 |
scores = {labels[i]: round(float(probs[0][i]), 3) for i in range(len(labels))}
|
|
|
|
|
|
|
| 25 |
top_label = max(scores, key=scores.get)
|
| 26 |
|
|
|
|
| 27 |
emoji_map = {
|
| 28 |
"positive": "😄",
|
| 29 |
"neutral": "😐",
|
|
|
|
| 31 |
"empty": "💬"
|
| 32 |
}
|
| 33 |
|
| 34 |
+
return {"label": top_label, "emoji": emoji_map[top_label], "scores": scores}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# Gradio arayüzü
|
| 37 |
iface = gr.Interface(
|
|
|
|
| 42 |
description="Analyzes text sentiment in multiple languages (English, Turkish, etc.)",
|
| 43 |
)
|
| 44 |
|
| 45 |
+
# 🔹 Önemli: enable_queue + share + server_name
|
| 46 |
+
iface.launch(server_name="0.0.0.0", server_port=7860, enable_queue=True)
|