File size: 5,154 Bytes
99d7b92 4b29a3a 99d7b92 4b29a3a 99d7b92 4b29a3a 99d7b92 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
import gradio as gr
import pandas as pd
from scrape.trendyol_scraper import scrape_reviews
from scripts.review_summarizer import ReviewAnalyzer
import plotly.express as px
import plotly.graph_objects as go
import os
import subprocess
def setup_chrome():
"""Chrome ve ChromeDriver kurulumu"""
try:
# Chrome kurulumu
os.system('apt-get update')
os.system('apt-get install -y wget unzip')
os.system('wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -')
os.system('echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list')
os.system('apt-get update')
os.system('apt-get install -y google-chrome-stable')
# ChromeDriver kurulumu
chrome_version = subprocess.check_output(['google-chrome', '--version']).decode().strip().split()[2].split('.')[0]
os.system(f'wget -q "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_{chrome_version}" -O chrome_version')
os.system(f'wget -q "https://chromedriver.storage.googleapis.com/$(cat chrome_version)/chromedriver_linux64.zip"')
os.system('unzip chromedriver_linux64.zip')
os.system('mv chromedriver /usr/local/bin/')
os.system('chmod +x /usr/local/bin/chromedriver')
print("Chrome ve ChromeDriver başarıyla kuruldu!")
except Exception as e:
print(f"Chrome kurulumunda hata: {str(e)}")
class ReviewAnalysisApp:
def __init__(self):
try:
setup_chrome() # Uygulama başlatılırken Chrome'u kur
self.analyzer = ReviewAnalyzer()
except Exception as e:
print(f"Uygulama başlatılırken hata: {str(e)}")
def analyze_url(self, url):
try:
if not url or not url.startswith("https://www.trendyol.com/"):
return "Lütfen geçerli bir Trendyol ürün yorumları URL'si girin.", None, None, None
print("Yorumlar çekiliyor...")
df = scrape_reviews(url)
if df.empty:
return "Yorumlar çekilemedi. Lütfen URL'yi kontrol edin.", None, None, None
print("Sentiment analizi yapılıyor...")
analyzed_df = self.analyzer.analyze_reviews(df)
print("Özet oluşturuluyor...")
summary = self.analyzer.generate_summary(analyzed_df)
print("Grafikler oluşturuluyor...")
fig1 = self.create_sentiment_distribution(analyzed_df)
fig2 = self.create_rating_distribution(analyzed_df)
fig3 = self.create_sentiment_by_rating(analyzed_df)
return summary, fig1, fig2, fig3
except Exception as e:
error_msg = f"Analiz sırasında hata oluştu: {str(e)}"
print(error_msg)
return error_msg, None, None, None
def create_sentiment_distribution(self, df):
fig = px.pie(df,
names='sentiment_label',
title='Duygu Analizi Dağılımı')
return fig
def create_rating_distribution(self, df):
fig = px.bar(df['Yıldız Sayısı'].value_counts().sort_index(),
title='Yıldız Dağılımı')
fig.update_layout(xaxis_title='Yıldız Sayısı',
yaxis_title='Yorum Sayısı')
return fig
def create_sentiment_by_rating(self, df):
avg_sentiment = df.groupby('Yıldız Sayısı')['sentiment_score'].mean()
fig = px.line(avg_sentiment,
title='Yıldız Sayısına Göre Ortalama Sentiment Skoru')
fig.update_layout(xaxis_title='Yıldız Sayısı',
yaxis_title='Ortalama Sentiment Skoru')
return fig
def create_interface():
app = ReviewAnalysisApp()
with gr.Blocks(theme=gr.themes.Soft()) as interface:
gr.Markdown("# Trendyol Yorum Analizi")
with gr.Row():
url_input = gr.Textbox(
label="Trendyol Ürün Yorumları URL'si",
placeholder="https://www.trendyol.com/..."
)
analyze_btn = gr.Button("Analiz Et")
with gr.Row():
with gr.Column(scale=1):
summary_output = gr.Textbox(
label="Özet",
lines=10
)
with gr.Column(scale=2):
with gr.Tab("Duygu Analizi"):
sentiment_dist = gr.Plot()
with gr.Tab("Yıldız Dağılımı"):
rating_dist = gr.Plot()
with gr.Tab("Sentiment-Yıldız İlişkisi"):
sentiment_rating = gr.Plot()
analyze_btn.click(
fn=app.analyze_url,
inputs=[url_input],
outputs=[summary_output, sentiment_dist, rating_dist, sentiment_rating]
)
return interface
if __name__ == "__main__":
interface = create_interface()
interface.launch() |