Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +63 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,65 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import seaborn as sns
|
| 6 |
+
import neattext.functions as nfx
|
| 7 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 8 |
+
from sklearn.decomposition import LatentDirichletAllocation
|
| 9 |
+
import nltk
|
| 10 |
+
|
| 11 |
+
# NLTK verilerini indir (HF üzerinde çalışması için gerekli)
|
| 12 |
+
nltk.download('punkt')
|
| 13 |
+
nltk.download('wordnet')
|
| 14 |
+
nltk.download('stopwords')
|
| 15 |
+
|
| 16 |
+
st.set_page_config(page_title="Konu Modelleme Analizi", layout="wide")
|
| 17 |
+
|
| 18 |
+
st.title("📂 Konu Modelleme (Topic Modeling) Analizi")
|
| 19 |
+
st.markdown("Bu uygulama, metin veri kümeleri içerisindeki gizli temaları tespit eder.")
|
| 20 |
+
|
| 21 |
+
# 1. Veri Yükleme
|
| 22 |
+
@st.cache_data
|
| 23 |
+
def load_data():
|
| 24 |
+
# Notebook'undaki gibi latin1 encoding ile okuyoruz
|
| 25 |
+
df = pd.read_csv("src/articles.csv", encoding='latin1')
|
| 26 |
+
return df
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
df = load_data()
|
| 30 |
+
st.success("Veri seti başarıyla yüklendi!")
|
| 31 |
+
|
| 32 |
+
# 2. Veri Ön İşleme (Notebook'undaki fonksiyon)
|
| 33 |
+
if st.checkbox("Veriyi Ön İşlemden Geçir (Cleaning)"):
|
| 34 |
+
with st.spinner("Metinler temizleniyor..."):
|
| 35 |
+
df['Processed_Article'] = df['Article'].apply(nfx.remove_punctuations)
|
| 36 |
+
df['Processed_Article'] = df['Processed_Article'].apply(lambda x: nfx.remove_stopwords(x, lang='english'))
|
| 37 |
+
st.write(df[['Article', 'Processed_Article']].head())
|
| 38 |
+
|
| 39 |
+
# 3. LDA Modelleme
|
| 40 |
+
st.sidebar.header("Model Ayarları")
|
| 41 |
+
n_topics = st.sidebar.slider("Konu Sayısı", min_value=2, max_value=15, value=10)
|
| 42 |
+
|
| 43 |
+
if st.button("Modeli Eğit ve Konuları Bul"):
|
| 44 |
+
vectorizer = TfidfVectorizer(max_features=1000, stop_words='english')
|
| 45 |
+
x = vectorizer.fit_transform(df['Article'])
|
| 46 |
+
|
| 47 |
+
lda = LatentDirichletAllocation(n_components=n_topics, random_state=42)
|
| 48 |
+
lda.fit(x)
|
| 49 |
+
|
| 50 |
+
# Konuları Görselleştirme (Notebook'undaki grafik mantığı)
|
| 51 |
+
st.subheader(f"Belirlenen {n_topics} Konu ve Anahtar Kelimeler")
|
| 52 |
+
|
| 53 |
+
feature_names = vectorizer.get_feature_names_out()
|
| 54 |
+
for index, topic in enumerate(lda.components_):
|
| 55 |
+
top_words_indices = topic.argsort()[-7:][::-1]
|
| 56 |
+
top_words = [feature_names[i] for i in top_words_indices]
|
| 57 |
+
top_weights = topic[top_words_indices]
|
| 58 |
+
|
| 59 |
+
fig, ax = plt.subplots(figsize=(8, 4))
|
| 60 |
+
sns.barplot(x=top_weights, y=top_words, palette="viridis", ax=ax)
|
| 61 |
+
ax.set_title(f"Konu {index + 1}")
|
| 62 |
+
st.pyplot(fig)
|
| 63 |
|
| 64 |
+
except FileNotFoundError:
|
| 65 |
+
st.error("Lütfen 'articles.csv' dosyasını uygulama klasörüne ekleyin.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|