ahdsoft/Persian-Topic-Modeling
1
1import streamlit as st2# import numpy as np3import pandas as pd4from topic_modeling import TopicModeling5st.set_page_config(page_title='تحلیلگر متن عهد', page_icon = './ahd_logo.png', layout = 'wide')6@st.cache_resource7def get_model():8 tp_model = TopicModeling()9 return tp_model10 11tp_model = get_model()12 13 14 15col1, col2, col3 = st.columns(3)16with col2:17 st.title("تحلیل اسناد متنی")18 19# Upload CSV file20uploaded_file = st.file_uploader("آپلود فایل")21if uploaded_file is not None:22 filename = uploaded_file.name23 if filename.endswith('.xlsx'):24 df = pd.read_excel(uploaded_file)25 elif filename.endswith('.csv'):26 df = pd.read_csv(uploaded_file)27 else:28 raise ValueError('Unsupported file format')29 30 # Show first 10 rows of dataframe31 st.write(df.head(10))32 33 # Select columns to use for topic modeling34 cols = st.multiselect("ستونهای متنی موردنظر را انتخاب نمایید", df.columns)35 ratio = st.slider('چند درصد از کل دادگان پردازش شود',min_value=0, max_value=100) 36 col1, col2, col3 , col4, col5 = st.columns(5)37 with col3:38 done_button = st.button("پردازش دادگان")39 if done_button:40 # print('colssssssssssssss ', cols)41 # Concatenate selected text columns42 df = df[cols]43 df = df.head(int(len(df) * (ratio/100)))44 df = df.dropna()45 # text = df.apply(lambda x:' '.join(x), axis=1)46 47 # Run topic modeling function48 col1, col2, col3 = st.columns(3)49 with col2:50 data_progress = st.spinner('در حال پردازش دادگان')51 with data_progress:52 docs = tp_model.add_data(df)53 st.success('پردازش دادگان با موفقیت به پایان رسید') 54 # print('before docs')55 with st.spinner('در حال آموزش مدل'):56 # print('fittttttttttt')57 tp_model.fit(docs)58 st.success('آموزش پایان یافت')59 col1, col2, col3 = st.columns(3)60 with col3:61 st.title(" فضای تاپیکها ")62 st.header("")63 fig = tp_model.get_vis_topics()64 st.plotly_chart(65 fig, 66 use_container_width=True,67 theme="streamlit", # ✨ Optional, this is already set by default!68 )69 col1, col2, col3 = st.columns(3)70 with col3:71 st.title(" کلمات هر تاپیک ")72 st.header("")73 fig = tp_model.get_barchart()74 st.plotly_chart(75 fig, 76 use_container_width=True,77 theme="streamlit", # ✨ Optional, this is already set by default!78 )79 80 81 col1, col2, col3 = st.columns(3)82 with col3:83 st.title("لیست تاپیکها")84 st.header("")85 topics_info = tp_model.get_topic_info()86 st.write(topics_info)87 88 col1, col2, col3 = st.columns(3)89 with col3:90 st.title(" ابر کلمات ")91 st.header("")92 # # figs = tp_model.get_wordcloud()93 # topic_counts = len(tp_model.topic_model.get_topic_info())94 # print('topic count ', topic_counts)95 # if topic_counts > 15:96 # topic_counts = 1597 # for topic_index in range(topic_counts):98 # fig = tp_model.get_wordcloud_by_topic(topic_index)99 # if fig:100 figs = tp_model.get_wordcloud()101 for fig in figs:102 st.header("")103 st.markdown('topic:')104 # st.title(f'topic:{topic_index}')105 st.pyplot(fig)106 107 108 109 110 111 112 