Delete src/streamlit_app.py
Browse files- src/streamlit_app.py +0 -181
src/streamlit_app.py
DELETED
|
@@ -1,181 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
-
import neattext.functions as nfx
|
| 5 |
-
from tensorflow.keras.models import load_model
|
| 6 |
-
import pickle
|
| 7 |
-
from textblob import TextBlob
|
| 8 |
-
import os
|
| 9 |
-
import nltk
|
| 10 |
-
import plotly.graph_objects as go
|
| 11 |
-
|
| 12 |
-
# --- NLTK PAKETLERİ ---
|
| 13 |
-
try:
|
| 14 |
-
nltk.data.find('corpora/wordnet')
|
| 15 |
-
except LookupError:
|
| 16 |
-
nltk.download('wordnet')
|
| 17 |
-
nltk.download('punkt')
|
| 18 |
-
nltk.download('omw-1.4')
|
| 19 |
-
|
| 20 |
-
# --- PICKLE İÇİN GEREKLİ FONKSİYON ---
|
| 21 |
-
# Model eğitilirken kullanılan 'ekkok' fonksiyonu burada tanımlı olmalı
|
| 22 |
-
def ekkok(title):
|
| 23 |
-
return [word.lemmatize() for word in TextBlob(title).words]
|
| 24 |
-
|
| 25 |
-
# --- SAYFA AYARLARI ---
|
| 26 |
-
st.set_page_config(page_title="News Classifier", layout="wide")
|
| 27 |
-
|
| 28 |
-
# --- MODEL VE DOSYALARI YÜKLEME ---
|
| 29 |
-
@st.cache_resource
|
| 30 |
-
def load_assets():
|
| 31 |
-
model_path = "news_classification_model.h5"
|
| 32 |
-
vect_path = "tfidf_vectorizer.pkl"
|
| 33 |
-
le_path = "label_encoder.pkl"
|
| 34 |
-
|
| 35 |
-
# Dosyaların varlığını kontrol et
|
| 36 |
-
if not all(os.path.exists(p) for p in [model_path, vect_path, le_path]):
|
| 37 |
-
st.error("⚠️ Model dosyaları bulunamadı! Lütfen .h5 ve .pkl dosyalarını app.py ile aynı klasöre koyun.")
|
| 38 |
-
st.stop()
|
| 39 |
-
|
| 40 |
-
# Modeli yükle
|
| 41 |
-
model = load_model(model_path)
|
| 42 |
-
|
| 43 |
-
# Pickle dosyalarını yükle
|
| 44 |
-
with open(vect_path, "rb") as f:
|
| 45 |
-
vect = pickle.load(f)
|
| 46 |
-
with open(le_path, "rb") as f:
|
| 47 |
-
le = pickle.load(f)
|
| 48 |
-
|
| 49 |
-
return model, vect, le
|
| 50 |
-
|
| 51 |
-
# Dosyaları belleğe alalım
|
| 52 |
-
model, vect, le = load_assets()
|
| 53 |
-
|
| 54 |
-
# --- TAHMİN FONKSİYONU ---
|
| 55 |
-
def predict_news(text):
|
| 56 |
-
# Metin temizleme
|
| 57 |
-
clean_text = nfx.clean_text(text.lower())
|
| 58 |
-
# TF-IDF Dönüşümü
|
| 59 |
-
matrix = vect.transform([clean_text]).toarray()
|
| 60 |
-
# Model Tahmini
|
| 61 |
-
prediction = model.predict(matrix, verbose=0)
|
| 62 |
-
class_index = np.argmax(prediction)
|
| 63 |
-
prob = np.max(prediction)
|
| 64 |
-
# Kategori ismini bulma
|
| 65 |
-
category = le.inverse_transform([class_index])[0]
|
| 66 |
-
# Sınıf olasılıkları
|
| 67 |
-
probabilities = prediction[0]
|
| 68 |
-
return category, prob, probabilities
|
| 69 |
-
|
| 70 |
-
# --- SIDEBAR (SOL PANEL) - ÇOKLU ÖRNEKLER ---
|
| 71 |
-
st.sidebar.title("📌 Samples / Örnekler")
|
| 72 |
-
|
| 73 |
-
# Her kategori için 3'er adet örnek cümle (Science/Uzay ağırlıklı)
|
| 74 |
-
all_examples = {
|
| 75 |
-
"Science / Bilim": [
|
| 76 |
-
"NASA's Perseverance rover successfully collects high-priority rock samples from the Martian surface.",
|
| 77 |
-
"The James Webb Space Telescope captures stunning new images of a distant star-forming nebula.",
|
| 78 |
-
"Astronomers discover a new solar system with three potentially habitable planets."
|
| 79 |
-
],
|
| 80 |
-
"Tech / Teknoloji": [
|
| 81 |
-
"Apple announces new AI-powered features for the upcoming iPhone 18 Pro series.",
|
| 82 |
-
"OpenAI releases a new language model that can reason like a human expert.",
|
| 83 |
-
"Scientists develop a new quantum computer that performs calculations in seconds."
|
| 84 |
-
],
|
| 85 |
-
"Sports / Spor": [
|
| 86 |
-
"Manchester City secures a narrow victory against Arsenal in a thrilling Premier League match.",
|
| 87 |
-
"The Olympic Committee announces the final list of cities bidding for the 2032 Games.",
|
| 88 |
-
"Formula 1 introduces new sustainable fuel regulations to be implemented by 2026."
|
| 89 |
-
],
|
| 90 |
-
"Business / Ekonomi": [
|
| 91 |
-
"Global stock markets rally as central banks signal potential interest rate cuts.",
|
| 92 |
-
"The tech industry faces new regulations regarding data privacy and user security.",
|
| 93 |
-
"Gold prices hit an all-time high amidst global economic uncertainty and inflation."
|
| 94 |
-
],
|
| 95 |
-
"Health / Sağlık": [
|
| 96 |
-
"New clinical trials show a 90% success rate in a breakthrough cancer treatment.",
|
| 97 |
-
"Doctors recommend daily exercise and a balanced diet to prevent heart disease.",
|
| 98 |
-
"A new study reveals the long-term impact of sleep deprivation on mental health."
|
| 99 |
-
]
|
| 100 |
-
}
|
| 101 |
-
|
| 102 |
-
# Kategori seçimi
|
| 103 |
-
selected_cat = st.sidebar.selectbox("Select Category / Kategori Seçin:", [""] + list(all_examples.keys()))
|
| 104 |
-
|
| 105 |
-
if selected_cat != "":
|
| 106 |
-
st.sidebar.markdown(f"### {selected_cat} Örnekleri:")
|
| 107 |
-
st.sidebar.write("Kopyalamak için üzerine tıklayın:")
|
| 108 |
-
|
| 109 |
-
# Seçilen kategorideki 3 örneği de kopyalanabilir code bloğu içinde gösteriyoruz
|
| 110 |
-
for i, ex in enumerate(all_examples[selected_cat], 1):
|
| 111 |
-
st.sidebar.info(f"Örnek {i}:")
|
| 112 |
-
st.sidebar.code(ex, language=None)
|
| 113 |
-
|
| 114 |
-
# --- ANA SAYFA TASARIMI ---
|
| 115 |
-
st.title("📰 News Topic Categorizer / Haber Sınıflandırıcı")
|
| 116 |
-
st.markdown("---")
|
| 117 |
-
|
| 118 |
-
# Metin Giriş Alanı (Her zaman boş başlar)
|
| 119 |
-
user_input = st.text_area("News Headline / Haber Başlığı:", height=150, placeholder="Sol taraftan bir örnek kopyalayıp buraya yapıştırın...")
|
| 120 |
-
|
| 121 |
-
# Tahmin Butonu ve İşlemi
|
| 122 |
-
if st.button("Predict / Tahmin Et"):
|
| 123 |
-
if user_input.strip() != "":
|
| 124 |
-
with st.spinner('Analyzing... / Analiz ediliyor...'):
|
| 125 |
-
# 1. Tahmin Yap
|
| 126 |
-
category, confidence, all_probs = predict_news(user_input)
|
| 127 |
-
|
| 128 |
-
# 2. Çeviri Yap (TextBlob kullanarak hatasız çeviri, cgi hatası vermez)
|
| 129 |
-
try:
|
| 130 |
-
# TextBlob bazen internet bağlantısına göre yavaşlayabilir
|
| 131 |
-
translated = str(TextBlob(user_input).translate(from_lang='en', to='tr'))
|
| 132 |
-
except:
|
| 133 |
-
translated = "Çeviri şu an yapılamıyor. / Translation failed."
|
| 134 |
-
|
| 135 |
-
# 3. Efektler
|
| 136 |
-
st.balloons()
|
| 137 |
-
|
| 138 |
-
# 4. Haber ve Alt Satırda Çeviri (Şık Bilgi Kutusu)
|
| 139 |
-
st.info(f"**English:** {user_input}\n\n**Türkçe Çeviri:** *{translated}*")
|
| 140 |
-
|
| 141 |
-
st.markdown("---")
|
| 142 |
-
|
| 143 |
-
# 5. Sonuç Kartları (Metric formatında)
|
| 144 |
-
res_col1, res_col2 = st.columns(2)
|
| 145 |
-
with res_col1:
|
| 146 |
-
st.success(f"**Predicted Category / Tahmin Edilen Kategori:**\n\n## {category}")
|
| 147 |
-
with res_col2:
|
| 148 |
-
st.warning(f"**Confidence Score / Güven Oranı:**\n\n## %{confidence*100:.2f}")
|
| 149 |
-
|
| 150 |
-
# 6. Grafik Ekleme (Plotly ile Şık ve Renkli Görünüm)
|
| 151 |
-
st.markdown("#### Probability Distribution / Olasılık Dağılımı")
|
| 152 |
-
|
| 153 |
-
# Etiketler ve olasılıklar
|
| 154 |
-
labels = le.classes_
|
| 155 |
-
|
| 156 |
-
# --- GRAFİK RENGİ MAVİ OLSUN ---
|
| 157 |
-
fig = go.Figure(go.Bar(
|
| 158 |
-
x=all_probs,
|
| 159 |
-
y=labels,
|
| 160 |
-
orientation='h',
|
| 161 |
-
marker_color='#1F77B4', # Mazarine Blue / Mavi Renk Kodu
|
| 162 |
-
text=[f"%{p*100:.1f}" for p in all_probs],
|
| 163 |
-
textposition='auto'
|
| 164 |
-
))
|
| 165 |
-
|
| 166 |
-
fig.update_layout(
|
| 167 |
-
title="Category Probabilities / Kategori Olasılıkları",
|
| 168 |
-
xaxis_title="Confidence / Güven",
|
| 169 |
-
yaxis_title="Categories / Kategoriler",
|
| 170 |
-
height=400,
|
| 171 |
-
margin=dict(l=20, r=20, t=40, b=20)
|
| 172 |
-
)
|
| 173 |
-
|
| 174 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 175 |
-
|
| 176 |
-
else:
|
| 177 |
-
st.warning("⚠️ Lütfen bir haber başlığı girin! / Please enter a headline!")
|
| 178 |
-
|
| 179 |
-
# Footer / Alt Bilgi
|
| 180 |
-
st.markdown("---")
|
| 181 |
-
st.caption("Deep Learning News Classification Project - Powered by Mergen")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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