import pandas as pd import numpy as np import re import streamlit as st from sklearn.model_selection import train_test_split from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.linear_model import LogisticRegression from sklearn.metrics import classification_report, accuracy_score # Streamlit uygulamasının başlığı st.image("https://miro.medium.com/v2/resize:fit:4800/format:webp/1*g_FtJ2rBkxOSbk2dCj0N4g.jpeg") st.title("IMDB Comment Analysis") st.write("Do you want to watch a movie? And are you one of those who look at the reviews before choosing?") st.write("Write the review and press the button to let us guess the emotion! Let us tell you if it's worth watching!") # Veri setini yükle @st.cache_data # Bu dekoratör verileri önbelleğe alarak performansı artırır def load_data(): try: df = pd.read_csv("src/IMDB_Dataset.csv") # "src/" klasörünü kaldırdım, doğrudan dosya adı return df except FileNotFoundError: st.error("IMDB_Dataset.csv not found.") return None df = load_data() if df is None: st.stop() # Dosya bulunamazsa uygulamayı durdur # Metin temizleme fonksiyonu def temizle(temiz_veri): temiz_veri = temiz_veri.lower() # küçük harfe çevir temiz_veri = re.sub(r'<.*?>', '', temiz_veri) # HTML etiketlerini kaldır temiz_veri = re.sub(r'\d+', '', temiz_veri) # sayıları kaldır temiz_veri = re.sub(r'\s+', ' ', temiz_veri).strip() # fazla boşlukları kaldır temiz_veri = re.sub(r'http\S+', '', temiz_veri) # URL'leri kaldır temiz_veri = re.sub(r'[^\w\s]', '', temiz_veri) # noktalama işaretlerini kaldır return temiz_veri # Modeli eğitmek için fonksiyon @st.cache_data # Model eğitimini önbelleğe al def train_model(): df["temiz_veri"] = df["review"].apply(temizle) X_train, X_test, y_train, y_test = train_test_split(df['temiz_veri'], df['sentiment'], test_size=0.4, random_state=42) tfidf = TfidfVectorizer(stop_words='english', ngram_range=(1, 2)) X_train_tfidf = tfidf.fit_transform(X_train) X_test_tfidf = tfidf.transform(X_test) lr = LogisticRegression() lr.fit(X_train_tfidf, y_train) y_pred_lr = lr.predict(X_test_tfidf) accuracy = accuracy_score(y_test, y_pred_lr) return lr, tfidf, accuracy lr, tfidf, accuracy = train_model() st.write(f"Model Accuracy: %{accuracy}") # Yüzde formatını düzgün göster # Kullanıcı etkileşimi yorum_tahmin = st.text_area("The comment you want to check:") # text_input yerine text_area kullandım if st.button("Predict"): if not yorum_tahmin.strip(): st.warning("Nothing to predict :( Make sure to add something") else: temiz_yorum = temizle(yorum_tahmin) X_yeni = tfidf.transform([temiz_yorum]) # Temizlenmiş yorumu kullan tahmin = lr.predict(X_yeni) st.subheader("Result:") st.write(f"**Predicted Emotion:** {tahmin[0].capitalize()}") if tahmin[0] == "positive": st.success("You should definitely watch this movie! 🤩") st.balloons() # Küçük bir kutlama efekti else: st.error("Naah... This movie isn't worth to watch..🥱")