ardacey06 commited on
Commit
cc37bab
·
verified ·
1 Parent(s): e91eb72

Upload ceybert.py

Browse files
Files changed (1) hide show
  1. ceybert.py +73 -0
ceybert.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # 1. Modeli Yükle
5
+ model_path = "./final_sentiment_model"
6
+ sentiment_analysis = pipeline("text-classification", model=model_path, tokenizer=model_path, top_k=None)
7
+
8
+ # 2. Duygu Çevirileri
9
+ emoji_map = {
10
+ "mutlu": "😊 Mutlu",
11
+ "üzgün": "😔 Üzgün",
12
+ "kızgın": "😡 Kızgın",
13
+ "sürpriz": "😮 Sürpriz",
14
+ "tiksinti": "🤢 Tiksinti",
15
+ "korku": "😱 Korku",
16
+ "label_0": "😶 Bilinmiyor" # Nötr veya Belirsiz
17
+ }
18
+
19
+ def analyze_sentiment(text):
20
+ # Model tahmini
21
+ results = sentiment_analysis(text)[0]
22
+
23
+ #Gradio {Label: Score} formatı
24
+ output_dict = {}
25
+ for result in results:
26
+ label = result['label']
27
+ score = result['score']
28
+
29
+ # İkon ekleme
30
+ display_label = emoji_map.get(label, label)
31
+ output_dict[display_label] = score
32
+
33
+ # Eşik Değeri Kontrolü (Threshold Logic)
34
+ top_score = max(output_dict.values())
35
+
36
+ # Eğer en yüksek skor %60'ın altındaysa (Model emin değilse)
37
+ if top_score < 0.60:
38
+ return {"😶 Nötr / Belirsiz": 1.0}
39
+
40
+ return output_dict
41
+
42
+ # 3. Arayüzü Oluştur
43
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
44
+ gr.Markdown("""
45
+ # 🇹🇷 TurkishBERTiment: Türkçe Duygu Analizi
46
+ Bu model, Türkçe metinlerdeki duygusal durumu (Mutlu, Üzgün, Kızgın, Sürpriz, Tiksinti, Korku) analiz etmek için **BERTurk** kullanılarak eğitilmiştir.
47
+ """)
48
+
49
+ with gr.Row():
50
+ with gr.Column():
51
+ input_text = gr.Textbox(
52
+ label="Analiz edilecek tümceyi yazın",
53
+ placeholder="Örn: Bu ürün harika ama kargo biraz gecikti...",
54
+ lines=3
55
+ )
56
+ analyze_btn = gr.Button("Analiz Et", variant="primary")
57
+
58
+ with gr.Column():
59
+ label_output = gr.Label(label="Duygu Durumu", num_top_classes=3)
60
+
61
+ # Örnek butonlar
62
+ examples = [
63
+ ["Sınavdan yüz aldığımı görünce havalara uçtum!"],
64
+ ["Bu yemeğin tadı gerçekten berbat."],
65
+ ["Gördüklerinden sonra küplere bindi."]
66
+ ]
67
+ gr.Examples(examples=examples, inputs=input_text)
68
+
69
+ analyze_btn.click(fn=analyze_sentiment, inputs=input_text, outputs=label_output)
70
+
71
+ # 4. Uygulamayı Başlat
72
+ if __name__ == "__main__":
73
+ demo.launch()