Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,113 +1,31 @@
|
|
| 1 |
-
import
|
| 2 |
-
from functools import lru_cache
|
| 3 |
-
|
| 4 |
-
from fastapi import FastAPI, HTTPException
|
| 5 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
from transformers import pipeline
|
| 7 |
import os
|
| 8 |
-
import re
|
| 9 |
-
from pydantic import BaseModel, Field
|
| 10 |
-
|
| 11 |
-
app = FastAPI(title="Sentiment Analyzer API", version="1.0.0")
|
| 12 |
-
|
| 13 |
-
# CORS middleware ekle
|
| 14 |
-
app.add_middleware(
|
| 15 |
-
CORSMiddleware,
|
| 16 |
-
allow_origins=["*"],
|
| 17 |
-
allow_credentials=True,
|
| 18 |
-
allow_methods=["*"],
|
| 19 |
-
allow_headers=["*"],
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
-
logger = logging.getLogger("ai-service")
|
| 23 |
-
logging.basicConfig(level=os.getenv("LOG_LEVEL", "INFO"))
|
| 24 |
-
|
| 25 |
-
# Türkçe metin desteği için daha iyi bir model kullan
|
| 26 |
-
MODEL_NAME = os.getenv("MODEL_NAME", "cardiffnlp/twitter-xlm-roberta-base-sentiment")
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
class AnalyzeRequest(BaseModel):
|
| 30 |
-
message: str = Field(..., min_length=1, description="Analyzed text")
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
class AnalyzeResponse(BaseModel):
|
| 34 |
-
sentiment: str
|
| 35 |
-
score: float
|
| 36 |
-
original_text: str
|
| 37 |
-
processed_text: str
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
logger.info("Loading sentiment model %s", MODEL_NAME)
|
| 43 |
-
return pipeline("sentiment-analysis", model=MODEL_NAME)
|
| 44 |
-
|
| 45 |
-
def preprocess_text(text):
|
| 46 |
-
"""Metni ön işleme tabi tut - emojileri ve özel karakterleri temizle"""
|
| 47 |
-
if not text or not isinstance(text, str):
|
| 48 |
-
return ""
|
| 49 |
-
|
| 50 |
-
# Emojileri temizle
|
| 51 |
-
emoji_pattern = re.compile("["
|
| 52 |
-
u"\U0001F600-\U0001F64F" # emoticons
|
| 53 |
-
u"\U0001F300-\U0001F5FF" # symbols & pictographs
|
| 54 |
-
u"\U0001F680-\U0001F6FF" # transport & map symbols
|
| 55 |
-
u"\U0001F1E0-\U0001F1FF" # flags (iOS)
|
| 56 |
-
u"\U00002702-\U000027B0"
|
| 57 |
-
u"\U000024C2-\U0001F251"
|
| 58 |
-
"]+", flags=re.UNICODE)
|
| 59 |
-
|
| 60 |
-
text = emoji_pattern.sub(r'', text)
|
| 61 |
-
|
| 62 |
-
# Fazla boşlukları temizle
|
| 63 |
-
text = re.sub(r'\s+', ' ', text).strip()
|
| 64 |
-
|
| 65 |
-
return text
|
| 66 |
-
|
| 67 |
-
@app.get("/health")
|
| 68 |
-
def health():
|
| 69 |
-
return {"ok": True}
|
| 70 |
-
|
| 71 |
-
@app.post("/analyze", response_model=AnalyzeResponse)
|
| 72 |
-
async def analyze(payload: AnalyzeRequest):
|
| 73 |
-
text = payload.message
|
| 74 |
-
|
| 75 |
-
# Metni ön işleme tabi tut
|
| 76 |
-
processed_text = preprocess_text(text)
|
| 77 |
-
|
| 78 |
-
if not processed_text:
|
| 79 |
-
return AnalyzeResponse(
|
| 80 |
-
sentiment="neutral",
|
| 81 |
-
score=0.5,
|
| 82 |
-
original_text=text,
|
| 83 |
-
processed_text=processed_text,
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
try:
|
| 87 |
-
analyzer = get_analyzer()
|
| 88 |
-
result = analyzer(processed_text)[0]
|
| 89 |
-
except Exception as exc:
|
| 90 |
-
logger.exception("Sentiment analysis failed")
|
| 91 |
-
raise HTTPException(status_code=500, detail="analysis failed") from exc
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
label = result["label"].lower()
|
| 94 |
score = float(result.get("score", 0.0))
|
| 95 |
-
|
| 96 |
-
# Model çıktılarını standartlaştır
|
| 97 |
-
if "positive" in label or "joy" in label:
|
| 98 |
-
sentiment = "positive"
|
| 99 |
-
elif "negative" in label or "sadness" in label or "anger" in label:
|
| 100 |
-
sentiment = "negative"
|
| 101 |
-
else:
|
| 102 |
sentiment = "neutral"
|
| 103 |
-
|
| 104 |
-
# Nötr bant genişletilmiş (0.4-0.6 arası)
|
| 105 |
-
if 0.4 <= score <= 0.6:
|
| 106 |
sentiment = "neutral"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
score=score,
|
| 111 |
-
original_text=text,
|
| 112 |
-
processed_text=processed_text,
|
| 113 |
-
)
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "distilbert-base-uncased-finetuned-sst-2-english")
|
| 6 |
+
analyzer = pipeline("sentiment-analysis", model=MODEL_NAME)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
def analyze(message: str):
|
| 9 |
+
if not isinstance(message, str) or not message.strip():
|
| 10 |
+
return {"sentiment": "neutral", "score": 0.0}
|
| 11 |
+
result = analyzer(message)[0] # {'label': 'POSITIVE'|'NEGATIVE', 'score': 0.xx}
|
| 12 |
label = result["label"].lower()
|
| 13 |
score = float(result.get("score", 0.0))
|
| 14 |
+
if label not in ("positive", "negative"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
sentiment = "neutral"
|
| 16 |
+
elif 0.48 <= score <= 0.52:
|
|
|
|
|
|
|
| 17 |
sentiment = "neutral"
|
| 18 |
+
else:
|
| 19 |
+
sentiment = label
|
| 20 |
+
return {"sentiment": sentiment, "score": score}
|
| 21 |
+
|
| 22 |
+
demo = gr.Interface(
|
| 23 |
+
fn=analyze,
|
| 24 |
+
inputs=gr.Textbox(label="Mesaj"),
|
| 25 |
+
outputs=gr.JSON(label="Sonuç"),
|
| 26 |
+
title="Duygu Analizi",
|
| 27 |
+
allow_flagging="never"
|
| 28 |
+
)
|
| 29 |
|
| 30 |
+
if __name__ == "__main__":
|
| 31 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|