Spaces:
Running
Running
File size: 1,415 Bytes
ed5a190 964cc84 69f0a76 ed5a190 cd6f478 ad563a7 69f0a76 4ae41ab ed5a190 cd6f478 4ae41ab 69f0a76 cd6f478 69f0a76 cd6f478 69f0a76 5eb55b6 cd6f478 206fa64 cd6f478 2e712ba 964cc84 2e712ba 206fa64 2e712ba cd6f478 2e712ba 8ab06e9 5eb55b6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | import gradio as gr
from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch
import torch.nn.functional as F
# 🌍 Model yükle
MODEL_NAME = "cardiffnlp/twitter-xlm-roberta-base-sentiment"
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
labels = ["negative", "neutral", "positive"]
def analyze_text(text):
if not text.strip():
return {"label": "empty", "emoji": "💬", "scores": {}}
inputs = tokenizer(text, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
probs = F.softmax(outputs.logits, dim=1)
scores = {labels[i]: round(float(probs[0][i]), 3) for i in range(len(labels))}
top_label = max(scores, key=scores.get)
emoji_map = {
"positive": "😄",
"neutral": "😐",
"negative": "😞",
"empty": "💬"
}
return {
"label": top_label,
"emoji": emoji_map[top_label],
"scores": scores
}
# ✅ GRADIO arayüzü (3.x sürümü)
iface = gr.Interface(
fn=analyze_text,
inputs=gr.Textbox(label="Metin Gir"),
outputs="json",
title="🌍 Multilingual Sentiment Analyzer",
description="Sentiment analysis for multiple languages."
)
# ✅ ÖNEMLİ: 3.x'te launch otomatik predict API oluşturur
iface.launch(server_name="0.0.0.0", server_port=7860)
|