import os import pandas as pd import plotly.express as px import gradio as gr from dotenv import load_dotenv from scripts.review_summarizer import analyze_reviews from scrape.trendyol_scraper_origin import scrape_comments load_dotenv() GEMINI_API_KEY = os.getenv('GEMINI_API_KEY') if not os.path.exists("data"): os.makedirs("data") def create_sentiment_plot(df): """Creates a pie chart visualization for sentiment distribution""" sentiment_counts = df["sentiment_label"].value_counts() fig = px.pie( values=sentiment_counts.values, names=sentiment_counts.index, title="Duygu Analizi Dağılımı", color_discrete_map={ "Pozitif": "#2ecc71", "Nötr": "#95a5a6", "Negatif": "#e74c3c", }, ) return fig def create_star_plot(df): """Creates a bar chart visualization for star rating distribution""" star_counts = df["Yıldız Sayısı"].value_counts().sort_index() fig = px.bar( x=star_counts.index, y=star_counts.values, title="Yıldız Dağılımı", labels={"x": "Yıldız Sayısı", "y": "Yorum Sayısı"}, color_discrete_sequence=["#f39c12"], ) fig.update_layout( xaxis=dict( tickmode="array", ticktext=["⭐", "⭐⭐", "⭐⭐⭐", "⭐⭐⭐⭐", "⭐⭐⭐⭐⭐"], ) ) return fig def analyze_product(url, progress=gr.Progress()): try: # Fetch reviews progress(0.1, desc="Yorumlar çekiliyor...") df = scrape_comments(url) if df is None or len(df) == 0: return None, None, None, None, None, None, None, "Yorumlar çekilemedi. URL'yi kontrol edin." # Save to CSV data_path = os.path.join("data", "product_comments.csv") df.to_csv(data_path, index=False, encoding="utf-8-sig") # Analyze reviews progress(0.4, desc="Yorumlar analiz ediliyor...") summary, analyzed_df = analyze_reviews(data_path, GEMINI_API_KEY) progress(0.7, desc="Sonuçlar hazırlanıyor...") # Calculate metrics total_reviews = len(df) total_analyzed = len(analyzed_df) avg_rating = f"{analyzed_df['Yıldız Sayısı'].mean():.1f}⭐" positive_ratio = len(analyzed_df[analyzed_df["sentiment_label"] == "Pozitif"]) / len(analyzed_df) * 100 positive_ratio_str = f"%{positive_ratio:.1f}" # Create plots sentiment_plot = create_sentiment_plot(analyzed_df) star_plot = create_star_plot(analyzed_df) # Create info message for removed reviews removed_reviews = total_reviews - total_analyzed info_message = "" if removed_reviews > 0: info_message = f"Not: Toplam {removed_reviews} adet kargo, teslimat ve satıcı ile ilgili yorum analiz dışı bırakılmıştır." progress(1.0, desc="Analiz tamamlandı!") return ( str(total_reviews), str(total_analyzed), avg_rating, positive_ratio_str, sentiment_plot, star_plot, summary, info_message ) except Exception as e: return None, None, None, None, None, None, None, f"Bir hata oluştu: {str(e)}" # Create Gradio interface with gr.Blocks(title="Trendyol Yorum Analizi") as demo: gr.Markdown(""" # Trendyol Yorum Analizi Bu uygulama, Trendyol ürün sayfasındaki yorumları çeker, analiz eder ve özetler. """) with gr.Row(): url_input = gr.Textbox( label="Trendyol Ürün Yorumları URL", placeholder="ürünün linki" ) analyze_btn = gr.Button("Analiz Et") with gr.Row(): total_reviews = gr.Textbox(label="Toplam Yorum") total_analyzed = gr.Textbox(label="Ürün Değerlendirme Sayısı") avg_rating = gr.Textbox(label="Ortalama Puan") positive_ratio = gr.Textbox(label="Olumlu Yorum Oranı") summary = gr.Markdown(label="📝 Genel Değerlendirme") info_message = gr.Markdown() with gr.Row(): sentiment_plot = gr.Plot() star_plot = gr.Plot() error_message = gr.Markdown() analyze_btn.click( analyze_product, inputs=[url_input], outputs=[ total_reviews, total_analyzed, avg_rating, positive_ratio, sentiment_plot, star_plot, summary, error_message ] ) if __name__ == "__main__": demo.launch()