Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import pipeline | |
| import os | |
| MODEL_NAME = os.getenv("MODEL_NAME", "distilbert-base-uncased-finetuned-sst-2-english") | |
| analyzer = pipeline("sentiment-analysis", model=MODEL_NAME) | |
| def analyze(message: str): | |
| if not isinstance(message, str) or not message.strip(): | |
| return {"sentiment": "neutral", "score": 0.0} | |
| result = analyzer(message)[0] # {'label': 'POSITIVE'|'NEGATIVE', 'score': 0.xx} | |
| label = result["label"].lower() | |
| score = float(result.get("score", 0.0)) | |
| if label not in ("positive", "negative"): | |
| sentiment = "neutral" | |
| elif 0.48 <= score <= 0.52: | |
| sentiment = "neutral" | |
| else: | |
| sentiment = label | |
| return {"sentiment": sentiment, "score": score} | |
| demo = gr.Interface( | |
| fn=analyze, | |
| inputs=gr.Textbox(label="Mesaj"), | |
| outputs=gr.JSON(label="Sonuç"), | |
| title="Duygu Analizi", | |
| allow_flagging="never" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |