samet214 commited on
Commit
20dbcf3
·
verified ·
1 Parent(s): 35a5348

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -8
app.py CHANGED
@@ -6,19 +6,49 @@ model_path = "cardiffnlp/twitter-roberta-base-sentiment-latest"
6
  sentiment_task = pipeline("sentiment-analysis", model=model_path, tokenizer=model_path)
7
 
8
  def analyze_sentiment(text):
9
- results = sentiment_task(text)
10
- # Gradio'nun anlayacağı bir formata dönüştür
11
- return {result['label']: result['score'] for result in results}
12
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  # Gradio arayüzünü oluştur
15
  iface = gr.Interface(
16
  fn=analyze_sentiment,
17
  inputs=gr.Textbox(lines=2, placeholder="Metin giriniz..."),
18
- outputs="label",
19
- title="Twitter RoBERTa Duygu Analizi",
20
- description="cardiffnlp/twitter-roberta-base-sentiment-latest modelini kullanarak bir metnin duygu analizini yapın.",
21
- examples=[["I drank coffee, then went to work"], ["I love the new design of your website!"]]
 
 
 
 
 
22
  )
23
 
24
  # Arayüzü başlat
 
6
  sentiment_task = pipeline("sentiment-analysis", model=model_path, tokenizer=model_path)
7
 
8
  def analyze_sentiment(text):
9
+ """
10
+ Metni analiz eder ve en yüksek skorlu duyguyu döndürür
11
+ """
12
+ if not text or len(text.strip()) == 0:
13
+ return {
14
+ "sentiment": "neutral",
15
+ "score": 0.0
16
+ }
17
+
18
+ try:
19
+ results = sentiment_task(text[:512]) # Max 512 karakter
20
+
21
+ # En yüksek skorlu sonucu al
22
+ top_result = results[0]
23
+ label = top_result['label'].lower()
24
+ score = top_result['score']
25
+
26
+ return {
27
+ "sentiment": label,
28
+ "score": round(score, 3),
29
+ "text": text[:100] + "..." if len(text) > 100 else text
30
+ }
31
+
32
+ except Exception as e:
33
+ return {
34
+ "sentiment": "neutral",
35
+ "score": 0.0,
36
+ "error": str(e)
37
+ }
38
 
39
  # Gradio arayüzünü oluştur
40
  iface = gr.Interface(
41
  fn=analyze_sentiment,
42
  inputs=gr.Textbox(lines=2, placeholder="Metin giriniz..."),
43
+ outputs=gr.JSON(label="Sonuç"),
44
+ title="💬 Twitter RoBERTa Duygu Analizi",
45
+ description="cardiffnlp/twitter-roberta-base-sentiment-latest modelini kullanarak metnin duygu analizini yapın (positive/neutral/negative).",
46
+ examples=[
47
+ ["I love the new design of your website!"],
48
+ ["This is terrible and disappointing."],
49
+ ["It's okay, nothing special."],
50
+ ["I drank coffee, then went to work"]
51
+ ]
52
  )
53
 
54
  # Arayüzü başlat