Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,54 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
if uploaded_file is not None:
|
| 15 |
-
st.session_state["text"] = uploaded_file.getvalue().decode("utf-8")
|
| 16 |
-
|
| 17 |
-
st.write("OR")
|
| 18 |
-
|
| 19 |
-
input_text = st.text_area(
|
| 20 |
-
label="Enter text separated by newlines",
|
| 21 |
-
value="",
|
| 22 |
-
key="text",
|
| 23 |
-
height=150,
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
button = st.button("Get Segments")
|
| 27 |
-
|
| 28 |
-
if button and (uploaded_file is not None or input_text != ""):
|
| 29 |
-
if uploaded_file is not None:
|
| 30 |
-
texts = st.session_state["text"].split("\n")
|
| 31 |
else:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import bertopic
|
| 4 |
+
import plotly.express as px
|
| 5 |
+
|
| 6 |
+
st.set_page_config(page_title="Topic Modeling with Bertopic")
|
| 7 |
+
|
| 8 |
+
# Function to read the uploaded file and return a Pandas DataFrame
|
| 9 |
+
def read_file(file):
|
| 10 |
+
if file.type == 'text/plain':
|
| 11 |
+
df = pd.read_csv(file, header=None, names=['data'])
|
| 12 |
+
elif file.type == 'text/csv':
|
| 13 |
+
df = pd.read_csv(file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
else:
|
| 15 |
+
st.error("Unsupported file format. Please upload a TXT or CSV file.")
|
| 16 |
+
return None
|
| 17 |
+
return df
|
| 18 |
+
|
| 19 |
+
# Sidebar to upload the file
|
| 20 |
+
st.sidebar.title("Upload File")
|
| 21 |
+
file = st.sidebar.file_uploader("Choose a file", type=["txt", "csv"])
|
| 22 |
+
|
| 23 |
+
# Perform topic modeling when the user clicks the "Visualize" button
|
| 24 |
+
if st.sidebar.button("Visualize"):
|
| 25 |
+
|
| 26 |
+
# Read the uploaded file
|
| 27 |
+
df = read_file(file)
|
| 28 |
+
if df is None:
|
| 29 |
+
st.stop()
|
| 30 |
+
|
| 31 |
+
# Perform topic modeling using Bertopic
|
| 32 |
+
model = bertopic.Bertopic()
|
| 33 |
+
topics, probabilities = model.fit_transform(df['data'])
|
| 34 |
+
|
| 35 |
+
# Create a plot of the topic distribution
|
| 36 |
+
fig = px.histogram(x=topics, nbins=max(topics)+1, color_discrete_sequence=px.colors.qualitative.Pastel)
|
| 37 |
+
fig.update_layout(
|
| 38 |
+
title="Distribution of Topics",
|
| 39 |
+
xaxis_title="Topic",
|
| 40 |
+
yaxis_title="Count",
|
| 41 |
+
)
|
| 42 |
+
st.plotly_chart(fig)
|
| 43 |
+
|
| 44 |
+
# Display the top words in each topic
|
| 45 |
+
st.write("Top words in each topic:")
|
| 46 |
+
for topic_id in range(max(topics)+1):
|
| 47 |
+
st.write(f"Topic {topic_id}: {model.get_topic(topic_id)}")
|
| 48 |
+
|
| 49 |
+
# Display the clusters
|
| 50 |
+
st.write("Clusters:")
|
| 51 |
+
for cluster_id, docs in model.get_clusters().items():
|
| 52 |
+
st.write(f"Cluster {cluster_id}:")
|
| 53 |
+
for doc in docs:
|
| 54 |
+
st.write(f"\t{doc}")
|