alibayram commited on
Commit
39cc797
·
1 Parent(s): 756b2c1

Add sentiment analysis Gradio app with Git LFS

Browse files
Files changed (4) hide show
  1. .gitattributes +1 -0
  2. app.py +182 -0
  3. requirements.txt +5 -0
  4. sentiment_v1.keras +3 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.keras filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ from sentence_transformers import SentenceTransformer
4
+ import numpy as np
5
+
6
+ # Load models
7
+ print("Loading models...")
8
+ embedding_model = SentenceTransformer("alibayram/distilled-sentence-transformer-c400")
9
+ sentiment_model = tf.keras.models.load_model('sentiment_v1.keras')
10
+ print("Models loaded successfully!")
11
+
12
+ def predict_sentiment(text):
13
+ """
14
+ Predict sentiment for the given text and return visual star representation
15
+ """
16
+ if not text.strip():
17
+ return "", "Lütfen bir metin girin."
18
+
19
+ # Encode the text
20
+ encoded_text = embedding_model.encode([text])
21
+
22
+ # Get prediction
23
+ prediction = sentiment_model.predict(encoded_text, verbose=0)
24
+
25
+ # Get the predicted star rating (1-5)
26
+ predicted_class = np.argmax(prediction[0])
27
+ predicted_stars = predicted_class + 1 # Convert 0-4 to 1-5
28
+
29
+ # Get confidence (probability of predicted class)
30
+ confidence = prediction[0][predicted_class] * 100
31
+
32
+ # Create star visualization
33
+ stars_html = create_star_display(predicted_stars, prediction[0])
34
+
35
+ # Create probability distribution display
36
+ prob_html = create_probability_display(prediction[0])
37
+
38
+ return stars_html, prob_html
39
+
40
+ def create_star_display(rating, probabilities):
41
+ """
42
+ Create HTML for star display with the predicted rating
43
+ """
44
+ confidence = probabilities[rating - 1] * 100
45
+
46
+ stars_html = '<div style="text-align: center; padding: 20px;">'
47
+ stars_html += '<div style="font-size: 60px; margin-bottom: 10px;">'
48
+
49
+ # Full stars
50
+ for i in range(rating):
51
+ stars_html += '⭐'
52
+
53
+ # Empty stars
54
+ for i in range(5 - rating):
55
+ stars_html += '☆'
56
+
57
+ stars_html += '</div>'
58
+ stars_html += f'<div style="font-size: 24px; font-weight: bold; color: #FF6B35; margin-top: 10px;">{rating} / 5 Yıldız</div>'
59
+ stars_html += f'<div style="font-size: 16px; color: #666; margin-top: 5px;">Güven: %{confidence:.1f}</div>'
60
+ stars_html += '</div>'
61
+
62
+ return stars_html
63
+
64
+ def create_probability_display(probabilities):
65
+ """
66
+ Create HTML for probability distribution display
67
+ """
68
+ html = '<div style="padding: 20px;">'
69
+ html += '<h3 style="text-align: center; color: #333; margin-bottom: 15px;">Olasılık Dağılımı</h3>'
70
+
71
+ for i, prob in enumerate(probabilities):
72
+ stars = i + 1
73
+ percentage = prob * 100
74
+
75
+ # Create a bar for each star rating
76
+ html += f'<div style="margin-bottom: 10px;">'
77
+ html += f'<div style="display: flex; align-items: center; margin-bottom: 5px;">'
78
+ html += f'<span style="min-width: 80px; font-weight: 500;">{stars} Yıldız:</span>'
79
+ html += f'<div style="flex: 1; background-color: #f0f0f0; border-radius: 10px; height: 25px; margin: 0 10px; overflow: hidden;">'
80
+ html += f'<div style="width: {percentage}%; background: linear-gradient(90deg, #FFD700, #FFA500); height: 100%; border-radius: 10px; transition: width 0.3s ease;"></div>'
81
+ html += '</div>'
82
+ html += f'<span style="min-width: 60px; text-align: right; font-weight: 500; color: #FF6B35;">%{percentage:.1f}</span>'
83
+ html += '</div>'
84
+ html += '</div>'
85
+
86
+ html += '</div>'
87
+ return html
88
+
89
+ # Custom CSS for better styling
90
+ custom_css = """
91
+ #title {
92
+ text-align: center;
93
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
94
+ color: white;
95
+ padding: 30px;
96
+ border-radius: 15px;
97
+ margin-bottom: 20px;
98
+ }
99
+
100
+ #subtitle {
101
+ text-align: center;
102
+ color: #666;
103
+ margin-bottom: 30px;
104
+ font-size: 18px;
105
+ }
106
+
107
+ .input-box textarea {
108
+ border-radius: 10px !important;
109
+ border: 2px solid #e0e0e0 !important;
110
+ font-size: 16px !important;
111
+ }
112
+
113
+ .predict-button {
114
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
115
+ border: none !important;
116
+ border-radius: 10px !important;
117
+ padding: 12px 30px !important;
118
+ font-size: 18px !important;
119
+ font-weight: bold !important;
120
+ color: white !important;
121
+ }
122
+
123
+ #output-box {
124
+ border-radius: 15px;
125
+ border: 2px solid #e0e0e0;
126
+ background-color: #fafafa;
127
+ }
128
+ """
129
+
130
+ # Create Gradio interface
131
+ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
132
+ gr.HTML('<div id="title"><h1 style="margin: 0; font-size: 42px;">🌟 Duygu Analizi 🌟</h1></div>')
133
+ gr.HTML('<div id="subtitle">Cümlenizi yazın ve duygu analizini keşfedin!</div>')
134
+
135
+ with gr.Row():
136
+ with gr.Column(scale=1):
137
+ input_text = gr.Textbox(
138
+ label="Metninizi Girin",
139
+ placeholder="Örnek: Çok beğendim, harika bir ürün!",
140
+ lines=5,
141
+ elem_classes="input-box"
142
+ )
143
+ predict_btn = gr.Button("🔮 Tahmin Et", elem_classes="predict-button", size="lg")
144
+
145
+ # Example inputs
146
+ gr.Examples(
147
+ examples=[
148
+ ["Çok iyi beğendim, harika bir ürün!"],
149
+ ["Hiç güzel değil, kesinlikle tavsiye etmem"],
150
+ ["Fena değil alınır ama emin de değilim"],
151
+ ["Mükemmel! Beklentilerimin çok üzerinde"],
152
+ ["Berbat bir deneyimdi, çok kötü"]
153
+ ],
154
+ inputs=input_text,
155
+ label="Örnek Cümleler"
156
+ )
157
+
158
+ with gr.Row():
159
+ with gr.Column(scale=1):
160
+ star_output = gr.HTML(label="Tahmin Sonucu", elem_id="output-box")
161
+
162
+ with gr.Column(scale=1):
163
+ prob_output = gr.HTML(label="Detaylı Analiz", elem_id="output-box")
164
+
165
+ # Connect the button to the prediction function
166
+ predict_btn.click(
167
+ fn=predict_sentiment,
168
+ inputs=input_text,
169
+ outputs=[star_output, prob_output]
170
+ )
171
+
172
+ # Also allow Enter key to trigger prediction
173
+ input_text.submit(
174
+ fn=predict_sentiment,
175
+ inputs=input_text,
176
+ outputs=[star_output, prob_output]
177
+ )
178
+
179
+ gr.HTML('<div style="text-align: center; margin-top: 30px; color: #999; font-size: 14px;">Model: alibayram/distilled-sentence-transformer-c400</div>')
180
+
181
+ if __name__ == "__main__":
182
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ tensorflow
3
+ tf-keras
4
+ sentence-transformers
5
+ numpy
sentiment_v1.keras ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fe6a93d8148ca6245014e44d5e0cbc6c586edb86e04d069deee787288978a391
3
+ size 20505785