change genai model
Browse files- app.py +74 -67
- requirements.txt +3 -1
app.py
CHANGED
@@ -11,11 +11,15 @@ import re
|
|
11 |
from tqdm import tqdm
|
12 |
import nltk
|
13 |
from nltk.corpus import stopwords
|
|
|
|
|
|
|
14 |
|
15 |
class ReviewAnalysisApp:
|
16 |
def __init__(self):
|
17 |
self.setup_models()
|
18 |
self.setup_stopwords()
|
|
|
19 |
|
20 |
def setup_stopwords(self):
|
21 |
"""Türkçe stopwords'leri yükle"""
|
@@ -34,8 +38,8 @@ class ReviewAnalysisApp:
|
|
34 |
|
35 |
def setup_models(self):
|
36 |
"""Modelleri yükle ve hazırla"""
|
37 |
-
#
|
38 |
-
self.device = "cpu"
|
39 |
print(f"Cihaz: {self.device}")
|
40 |
|
41 |
model_name = "savasy/bert-base-turkish-sentiment-cased"
|
@@ -43,35 +47,31 @@ class ReviewAnalysisApp:
|
|
43 |
self.sentiment_model = (
|
44 |
AutoModelForSequenceClassification.from_pretrained(
|
45 |
model_name,
|
46 |
-
low_cpu_mem_usage=False
|
47 |
)
|
48 |
.to(self.device)
|
49 |
.to(torch.float32)
|
50 |
)
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
"
|
69 |
-
|
70 |
-
|
71 |
-
"top_p": 0.9,
|
72 |
-
"repetition_penalty": 1.1
|
73 |
-
}
|
74 |
-
|
75 |
def preprocess_text(self, text):
|
76 |
"""Metin ön işleme"""
|
77 |
if isinstance(text, str):
|
@@ -145,7 +145,7 @@ class ReviewAnalysisApp:
|
|
145 |
return df
|
146 |
|
147 |
def generate_summary(self, df):
|
148 |
-
"""
|
149 |
# Temel istatistikler
|
150 |
avg_rating = df['Yıldız Sayısı'].mean()
|
151 |
total_reviews = len(df)
|
@@ -160,7 +160,7 @@ class ReviewAnalysisApp:
|
|
160 |
star_dist = df['Yıldız Sayısı'].value_counts().sort_index()
|
161 |
star_dist_text = "\n".join([f"{star} yıldız: {count} yorum" for star, count in star_dist.items()])
|
162 |
|
163 |
-
# En sık
|
164 |
all_words = []
|
165 |
for text in df['Yorum']:
|
166 |
cleaned_text = self.preprocess_text(text)
|
@@ -171,58 +171,65 @@ class ReviewAnalysisApp:
|
|
171 |
word_freq = Counter(all_words).most_common(10)
|
172 |
frequent_words = ", ".join([f"{word} ({count} kez)" for word, count in word_freq])
|
173 |
|
174 |
-
#
|
175 |
-
|
176 |
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
- Negatif yorum sayısı: {negative_count}
|
182 |
|
183 |
-
|
184 |
{star_dist_text}
|
185 |
|
186 |
-
|
187 |
{frequent_words}
|
188 |
|
189 |
-
|
190 |
-
Pozitif
|
191 |
{' | '.join(positive_comments[:3])}
|
192 |
|
193 |
-
Negatif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
{' | '.join(negative_comments[:3])}
|
195 |
|
196 |
-
|
197 |
-
|
198 |
-
2. Ürünün güçlü ve zayıf yönleri hakkında
|
199 |
-
3. Potansiyel alıcılar için önemli noktalar hakkında
|
200 |
-
kapsamlı bir değerlendirme yazar mısın?
|
201 |
-
"""
|
202 |
-
|
203 |
-
# Özet oluştur
|
204 |
-
response = self.summary_pipe(
|
205 |
-
prompt,
|
206 |
-
max_new_tokens=800, # Daha uzun özet için
|
207 |
-
eos_token_id=self.terminators,
|
208 |
-
**self.sampling_params
|
209 |
-
)[0]['generated_text']
|
210 |
-
|
211 |
-
# Prompt'u çıkar ve sadece özeti döndür
|
212 |
-
summary = response[len(prompt):].strip()
|
213 |
-
|
214 |
-
# Özeti formatla
|
215 |
-
formatted_summary = f"""📊 ÜRÜN ANAL�Z RAPORU
|
216 |
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
|
|
|
|
|
|
221 |
|
222 |
-
|
223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
|
225 |
-
return
|
226 |
|
227 |
def analyze_url(self, url):
|
228 |
try:
|
|
|
11 |
from tqdm import tqdm
|
12 |
import nltk
|
13 |
from nltk.corpus import stopwords
|
14 |
+
from dotenv import load_dotenv
|
15 |
+
import google.generativeai as genai
|
16 |
+
from pathlib import Path
|
17 |
|
18 |
class ReviewAnalysisApp:
|
19 |
def __init__(self):
|
20 |
self.setup_models()
|
21 |
self.setup_stopwords()
|
22 |
+
self.setup_gemini()
|
23 |
|
24 |
def setup_stopwords(self):
|
25 |
"""Türkçe stopwords'leri yükle"""
|
|
|
38 |
|
39 |
def setup_models(self):
|
40 |
"""Modelleri yükle ve hazırla"""
|
41 |
+
# Sadece sentiment model
|
42 |
+
self.device = "cpu"
|
43 |
print(f"Cihaz: {self.device}")
|
44 |
|
45 |
model_name = "savasy/bert-base-turkish-sentiment-cased"
|
|
|
47 |
self.sentiment_model = (
|
48 |
AutoModelForSequenceClassification.from_pretrained(
|
49 |
model_name,
|
50 |
+
low_cpu_mem_usage=False
|
51 |
)
|
52 |
.to(self.device)
|
53 |
.to(torch.float32)
|
54 |
)
|
55 |
|
56 |
+
def setup_gemini(self):
|
57 |
+
"""Gemini API'yi hazırla"""
|
58 |
+
try:
|
59 |
+
# Önce .env dosyasından API key'i al
|
60 |
+
load_dotenv()
|
61 |
+
api_key = os.getenv('GOOGLE_API_KEY')
|
62 |
+
if not api_key:
|
63 |
+
raise ValueError("API key bulunamadı!")
|
64 |
+
|
65 |
+
# Gemini'yi yapılandır
|
66 |
+
genai.configure(api_key=api_key)
|
67 |
+
|
68 |
+
# Modeli ayarla
|
69 |
+
self.gemini_model = genai.GenerativeModel('gemini-pro')
|
70 |
+
|
71 |
+
except Exception as e:
|
72 |
+
print(f"Gemini API yapılandırma hatası: {str(e)}")
|
73 |
+
self.gemini_model = None
|
74 |
+
|
|
|
|
|
|
|
|
|
75 |
def preprocess_text(self, text):
|
76 |
"""Metin ön işleme"""
|
77 |
if isinstance(text, str):
|
|
|
145 |
return df
|
146 |
|
147 |
def generate_summary(self, df):
|
148 |
+
"""İstatistiksel özet ve Gemini ile detaylı analiz"""
|
149 |
# Temel istatistikler
|
150 |
avg_rating = df['Yıldız Sayısı'].mean()
|
151 |
total_reviews = len(df)
|
|
|
160 |
star_dist = df['Yıldız Sayısı'].value_counts().sort_index()
|
161 |
star_dist_text = "\n".join([f"{star} yıldız: {count} yorum" for star, count in star_dist.items()])
|
162 |
|
163 |
+
# En sık kelimeler
|
164 |
all_words = []
|
165 |
for text in df['Yorum']:
|
166 |
cleaned_text = self.preprocess_text(text)
|
|
|
171 |
word_freq = Counter(all_words).most_common(10)
|
172 |
frequent_words = ", ".join([f"{word} ({count} kez)" for word, count in word_freq])
|
173 |
|
174 |
+
# İstatistiksel özet metni
|
175 |
+
stats_summary = f"""📊 ÜRÜN ANALİZ RAPORU
|
176 |
|
177 |
+
⭐ Ortalama Puan: {avg_rating:.1f}/5
|
178 |
+
📝 Toplam Yorum: {total_reviews}
|
179 |
+
✅ Pozitif Yorum: {positive_count}
|
180 |
+
❌ Negatif Yorum: {negative_count}
|
|
|
181 |
|
182 |
+
📈 YILDIZ DAĞILIMI:
|
183 |
{star_dist_text}
|
184 |
|
185 |
+
🔍 EN SIK KULLANILAN KELİMELER:
|
186 |
{frequent_words}
|
187 |
|
188 |
+
💬 ÖRNEK YORUMLAR:
|
189 |
+
✅ Pozitif Yorumlar:
|
190 |
{' | '.join(positive_comments[:3])}
|
191 |
|
192 |
+
❌ Negatif Yorumlar:
|
193 |
+
{' | '.join(negative_comments[:3])}"""
|
194 |
+
|
195 |
+
# Gemini ile detaylı analiz
|
196 |
+
if self.gemini_model:
|
197 |
+
try:
|
198 |
+
prompt = f"""Aşağıdaki ürün yorumları verilerine dayanarak detaylı bir analiz yap:
|
199 |
+
|
200 |
+
1. İstatistikler:
|
201 |
+
- Toplam {total_reviews} yorum
|
202 |
+
- Ortalama puan: {avg_rating:.1f}/5
|
203 |
+
- {positive_count} pozitif, {negative_count} negatif yorum
|
204 |
+
|
205 |
+
2. Örnek Pozitif Yorumlar:
|
206 |
+
{' | '.join(positive_comments[:3])}
|
207 |
+
|
208 |
+
3. Örnek Negatif Yorumlar:
|
209 |
{' | '.join(negative_comments[:3])}
|
210 |
|
211 |
+
4. En Sık Kullanılan Kelimeler:
|
212 |
+
{frequent_words}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
213 |
|
214 |
+
Lütfen şu başlıklar altında bir değerlendirme yap:
|
215 |
+
1. Ürünün güçlü yönleri
|
216 |
+
2. Ürünün zayıf yönleri
|
217 |
+
3. Genel kullanıcı memnuniyeti
|
218 |
+
4. Potansiyel alıcılar için öneriler
|
219 |
+
|
220 |
+
Yanıtını Türkçe olarak ver ve mümkün olduğunca özlü tut."""
|
221 |
|
222 |
+
response = self.gemini_model.generate_content(prompt)
|
223 |
+
ai_analysis = response.text
|
224 |
+
|
225 |
+
# İstatistiksel özet ve AI analizini birleştir
|
226 |
+
return f"{stats_summary}\n\n🤖 YAPAY ZEKA ANALİZİ:\n{ai_analysis}"
|
227 |
+
|
228 |
+
except Exception as e:
|
229 |
+
print(f"Gemini API hatası: {str(e)}")
|
230 |
+
return stats_summary
|
231 |
|
232 |
+
return stats_summary
|
233 |
|
234 |
def analyze_url(self, url):
|
235 |
try:
|
requirements.txt
CHANGED
@@ -10,4 +10,6 @@ webdriver_manager
|
|
10 |
tqdm
|
11 |
regex
|
12 |
scikit-learn
|
13 |
-
accelerate>=0.26.0
|
|
|
|
|
|
10 |
tqdm
|
11 |
regex
|
12 |
scikit-learn
|
13 |
+
accelerate>=0.26.0
|
14 |
+
python-dotenv
|
15 |
+
google-generativeai
|