Spaces:
Sleeping
Sleeping
File size: 10,762 Bytes
069bed5 3f1ac1e 069bed5 3f1ac1e 34e2c53 9e06c9d 34e2c53 3f1ac1e 34e2c53 3f1ac1e 34e2c53 3f1ac1e 9e06c9d 34e2c53 9e06c9d 34e2c53 9e06c9d 3f1ac1e 34e2c53 069bed5 34e2c53 069bed5 34e2c53 3f1ac1e 34e2c53 3f1ac1e 34e2c53 3f1ac1e 34e2c53 3f1ac1e 34e2c53 3f1ac1e 34e2c53 3f1ac1e 34e2c53 3f1ac1e 34e2c53 3f1ac1e 34e2c53 3f1ac1e 9e06c9d 34e2c53 9e06c9d 34e2c53 9e06c9d 34e2c53 9e06c9d 34e2c53 9e06c9d 34e2c53 9e06c9d 34e2c53 9e06c9d 34e2c53 9e06c9d 34e2c53 9e06c9d 34e2c53 9e06c9d 34e2c53 9e06c9d 34e2c53 9e06c9d 34e2c53 9e06c9d 069bed5 34e2c53 868ef7a 3f1ac1e 96afb92 3f1ac1e 069bed5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
import streamlit as st
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.cluster import KMeans
from sklearn.metrics.pairwise import linear_kernel, cosine_similarity
import nltk
from nltk.corpus import stopwords
from nltk import FreqDist
import re
import os
import base64
from graphviz import Digraph
from io import BytesIO
import networkx as nx
import matplotlib.pyplot as plt
st.set_page_config(
page_title="πΊTranscriptπEDAπNLTK",
page_icon="π ",
layout="wide",
initial_sidebar_state="expanded",
menu_items={
'Get Help': 'https://huggingface.co./awacke1',
'Report a bug': "https://huggingface.co./awacke1",
'About': "https://huggingface.co./awacke1"
}
)
st.markdown('''
1. π **Transcript Insights Using Exploratory Data Analysis (EDA)** π - Unveil hidden patterns π΅οΈββοΈ and insights π§ in your transcripts. π.
2. π **Natural Language Toolkit (NLTK)** π οΈ:- your compass π§ in the vast landscape of NLP.
3. πΊ **Transcript Analysis** π:Speech recognition ποΈ and thematic extraction π, audiovisual content to actionable insights π.
''')
# π§ Cluster sentences using K-means
def cluster_sentences(sentences, num_clusters):
sentences = [s for s in sentences if len(s) > 10]
num_clusters = min(num_clusters, len(sentences))
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(sentences)
kmeans = KMeans(n_clusters=num_clusters, random_state=42)
kmeans.fit(X)
clustered_sentences = [[] for _ in range(num_clusters)]
for i, label in enumerate(kmeans.labels_):
similarity = linear_kernel(kmeans.cluster_centers_[label:label+1], X[i:i+1]).flatten()[0]
clustered_sentences[label].append((similarity, sentences[i]))
return [[s for _, s in sorted(cluster, reverse=True)] for cluster in clustered_sentences]
# π Create context graph
def create_context_graph(context_words):
graph = Digraph()
for i, (before, high, after) in enumerate(context_words):
if before:
graph.node(f'before{i}', before, shape='box')
graph.edge(f'before{i}', f'high{i}', label=before)
graph.node(f'high{i}', high, shape='ellipse')
if after:
graph.node(f'after{i}', after, shape='diamond')
graph.edge(f'high{i}', f'after{i}', label=after)
return graph
# π Create relationship graph
def create_relationship_graph(words):
graph = Digraph()
for i, word in enumerate(words):
graph.node(str(i), word)
if i > 0:
graph.edge(str(i-1), str(i), label=word)
return graph
# π Display context graph
def display_context_graph(context_words):
st.graphviz_chart(create_context_graph(context_words))
# π Display context table
def display_context_table(context_words):
table = "| Before | High Info Word | After |\n|--------|----------------|-------|\n"
table += "\n".join(f"| {b if b else ''} | {h} | {a if a else ''} |" for b, h, a in context_words)
st.markdown(table)
# π Display relationship graph
def display_relationship_graph(words):
st.graphviz_chart(create_relationship_graph(words))
# π₯ Download NLTK data
@st.cache_resource
def download_nltk_data():
try:
nltk.data.find('tokenizers/punkt')
nltk.data.find('corpora/stopwords')
except LookupError:
with st.spinner('Downloading required NLTK data...'):
nltk.download('punkt')
nltk.download('stopwords')
st.success('NLTK data is ready!')
# π Extract context words
def extract_context_words(text, high_information_words):
words = nltk.word_tokenize(text)
return [(words[i-1] if i > 0 else None, word, words[i+1] if i < len(words)-1 else None)
for i, word in enumerate(words) if word.lower() in high_information_words]
# π Extract high information words
def extract_high_information_words(text, top_n=10):
try:
words = [word.lower() for word in nltk.word_tokenize(text) if word.isalpha()]
stop_words = set(stopwords.words('english'))
filtered_words = [word for word in words if word not in stop_words]
return [word for word, _ in FreqDist(filtered_words).most_common(top_n)]
except Exception as e:
st.error(f"Error in extract_high_information_words: {str(e)}")
return []
# π Get text files
def get_txt_files():
excluded_files = {'freeze.txt', 'requirements.txt', 'packages.txt', 'pre-requirements.txt'}
txt_files = [f for f in os.listdir() if f.endswith('.txt') and f not in excluded_files]
return pd.DataFrame({'File Name': txt_files, 'Full Path': [os.path.abspath(f) for f in txt_files]})
# π Get high info words per cluster
def get_high_info_words_per_cluster(cluster_sentences, num_words=5):
return [extract_high_information_words(" ".join(cluster), num_words) for cluster in cluster_sentences]
# πΎ Get text file download link
def get_text_file_download_link(text_to_download, filename='Output.txt', button_label="πΎ Save"):
b64 = base64.b64encode(text_to_download.encode()).decode()
return f'<a href="data:file/txt;base64,{b64}" download="{filename}" style="margin-top:20px;">{button_label}</a>'
# π Perform EDA
def perform_eda(file_name):
st.subheader(f"EDA for {file_name}")
process_file(os.path.abspath(file_name))
# π Plot cluster words
def plot_cluster_words(cluster_sentences):
for i, cluster in enumerate(cluster_sentences):
words = re.findall(r'\b[a-z]{4,}\b', " ".join(cluster))
word_freq = FreqDist(words)
top_words = [word for word, _ in word_freq.most_common(20)]
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(top_words)
similarity_matrix = cosine_similarity(X.toarray())
G = nx.from_numpy_array(similarity_matrix)
pos = nx.spring_layout(G, k=0.5)
plt.figure(figsize=(8, 6))
nx.draw_networkx(G, pos, node_size=500, font_size=12, font_weight='bold', with_labels=True,
labels={i: word for i, word in enumerate(top_words)},
node_color='skyblue', edge_color='gray')
plt.axis('off')
plt.title(f"Cluster {i+1} Word Arrangement")
st.pyplot(plt)
st.markdown(f"**Cluster {i+1} Details:**")
st.markdown(f"Top Words: {', '.join(top_words)}")
st.markdown(f"Number of Sentences: {len(cluster)}")
st.markdown("---")
# π Process file
def process_file(file_path):
try:
with open(file_path, 'r', encoding="utf-8") as file:
file_text = file.read()
text_without_timestamps = remove_timestamps(file_text)
top_words = extract_high_information_words(text_without_timestamps, 10)
with st.expander("π Top 10 High Information Words"):
st.write(top_words)
with st.expander("π Relationship Graph"):
display_relationship_graph(top_words) if top_words else st.warning("Unable to generate relationship graph.")
context_words = extract_context_words(text_without_timestamps, top_words)
with st.expander("π Context Graph"):
display_context_graph(context_words) if context_words else st.warning("Unable to generate context graph.")
with st.expander("π Context Table"):
display_context_table(context_words) if context_words else st.warning("Unable to display context table.")
sentences = [line.strip() for line in file_text.split('\n') if len(line.strip()) > 10]
st.write(f"Total Sentences: {len(sentences)}")
num_clusters = st.slider("Number of Clusters", min_value=2, max_value=10, value=5)
clustered_sentences = cluster_sentences(sentences, num_clusters)
col1, col2 = st.columns(2)
with col1:
st.subheader("Original Text")
st.text_area("Original Sentences", value="\n".join(sentences), height=400)
with col2:
st.subheader("Clustered Text")
cluster_high_info_words = get_high_info_words_per_cluster(clustered_sentences)
clusters = "\n".join(f"Cluster {i+1} (High Info Words: {', '.join(words)})"
for i, words in enumerate(cluster_high_info_words))
clustered_text = "\n\n".join(f"Cluster {i+1} (High Info Words: {', '.join(words)}):\n{cluster_text}"
for i, (words, cluster_text) in enumerate(zip(cluster_high_info_words,
["\n".join(cluster) for cluster in clustered_sentences])))
st.text_area("Clusters", value=clusters, height=200)
st.text_area("Clustered Sentences", value=clustered_text, height=200)
clustered_sentences_flat = [sentence for cluster in clustered_sentences for sentence in cluster]
st.write("β
All sentences are accounted for in the clustered output." if set(sentences) == set(clustered_sentences_flat)
else "β Some sentences are missing in the clustered output.")
plot_cluster_words(clustered_sentences)
except Exception as e:
st.error(f"Error processing file: {str(e)}")
# π°οΈ Remove timestamps
def remove_timestamps(text):
return re.sub(r'\d{1,2}:\d{2}\n.*\n', '', text)
# Main execution
download_nltk_data()
st.title("πΊ Transcript Analysis π")
txt_files_df = get_txt_files()
st.write("Available .txt files:")
st.dataframe(txt_files_df[['File Name']])
st.write("Select a file to perform EDA:")
cols = st.columns(len(txt_files_df))
for i, (_, row) in enumerate(txt_files_df.iterrows()):
if cols[i].button(f":file_folder: {row['File Name']}"):
perform_eda(row['File Name'])
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if prompt := st.chat_input("Ask a question about the data"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
response = f"You asked: {prompt}\n\nThis is a placeholder response. In a real application, you would process the user's question and provide an answer based on the data and EDA results."
st.session_state.messages.append({"role": "assistant", "content": response})
with st.chat_message("assistant"):
st.markdown(response)
st.markdown("For more information and updates, visit our [help page](https://huggingface.co./awacke1).") |