Spaces:
Sleeping
Sleeping
| # Gradio: Müşteri Yorum Analizi (Hazır Model ile) | |
| # ----------------------------------------------- | |
| import gradio as gr | |
| import joblib | |
| import os | |
| import re | |
| import nltk | |
| from nltk.corpus import stopwords | |
| import pandas as pd | |
| # ------------------------------------------------ | |
| # Stopwords (HF uyumlu) | |
| # ------------------------------------------------ | |
| def ensure_stopwords(): | |
| try: | |
| _ = stopwords.words("turkish") | |
| except LookupError: | |
| nltk.download("stopwords") | |
| return set(stopwords.words("turkish")) | |
| STOP_WORDS = ensure_stopwords() | |
| def clean_text(text): | |
| text = str(text).lower() | |
| text = re.sub(r"http\S+", "", text) | |
| text = re.sub(r"[^a-zçğıöşü\s]", "", text) | |
| text = " ".join([w for w in text.split() if w not in STOP_WORDS]) | |
| return text | |
| # ------------------------------------------------ | |
| # Model yükleme | |
| # ------------------------------------------------ | |
| MODEL_PATH = os.path.join( | |
| os.path.dirname(__file__), | |
| "model", | |
| "sentiment_model.pkl" | |
| ) | |
| if not os.path.exists(MODEL_PATH): | |
| raise FileNotFoundError( | |
| f"Model bulunamadı: {MODEL_PATH}\n" | |
| f"Lütfen sentiment_model.pkl dosyasını model/ klasörüne ekleyin." | |
| ) | |
| model = joblib.load(MODEL_PATH) | |
| # ------------------------------------------------ | |
| # Tahmin fonksiyonu | |
| # ------------------------------------------------ | |
| def predict_sentiment(comment): | |
| if not comment.strip(): | |
| return "⚠️ Lütfen bir yorum giriniz.", None | |
| cleaned = clean_text(comment) | |
| prediction = model.predict([cleaned])[0] | |
| # Olasılıklar | |
| if hasattr(model, "predict_proba"): | |
| probs = model.predict_proba([cleaned])[0] | |
| prob_df = pd.DataFrame({ | |
| "Sınıf": model.classes_, | |
| "Olasılık": probs | |
| }).sort_values("Olasılık", ascending=False) | |
| else: | |
| prob_df = None | |
| if prediction == "Memnun": | |
| label = "✅ Memnun" | |
| elif prediction == "Memnun Değil": | |
| label = "❌ Memnun Değil" | |
| else: | |
| label = "😐 Nötr" | |
| return label, prob_df | |
| # ------------------------------------------------ | |
| # Gradio UI | |
| # ------------------------------------------------ | |
| with gr.Blocks(title="Müşteri Yorum Analizi") as demo: | |
| gr.Markdown("## 🛍️ Müşteri Yorum Analizi (Gradio)") | |
| gr.Markdown( | |
| "Bu uygulama Trendyol yorumları üzerinde eğitilmiş " | |
| "**hazır bir makine öğrenmesi modeli** kullanır." | |
| ) | |
| with gr.Row(): | |
| with gr.Column(): | |
| comment_input = gr.Textbox( | |
| label="Yorum Giriniz", | |
| placeholder="Ürün çok güzel, çok memnun kaldım", | |
| lines=4 | |
| ) | |
| analyze_btn = gr.Button("Analiz Et") | |
| with gr.Column(): | |
| sentiment_output = gr.Textbox( | |
| label="Tahmin Sonucu", | |
| interactive=False | |
| ) | |
| prob_output = gr.Dataframe( | |
| label="Sınıf Olasılıkları", | |
| interactive=False | |
| ) | |
| analyze_btn.click( | |
| fn=predict_sentiment, | |
| inputs=comment_input, | |
| outputs=[sentiment_output, prob_output] | |
| ) | |
| demo.launch() | |