Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import plotly.express as px | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline | |
| import os | |
| # ---------- CONFIG ---------- | |
| st.set_page_config(page_title="CheckPD Sentiment Dashboard", layout="wide") | |
| st.title("ภาพรวมความรู้สึกของผู้ใช้ CheckPD (Real-time)") | |
| # ---------- LOAD DATA ---------- | |
| def load_data(): | |
| file_path = "src/Senti_real/Sentiment/checkpd_sentiment.csv" # ✅ แก้ path ตรงนี้ | |
| df = pd.read_csv(file_path) | |
| return df | |
| if st.button("รีเฟรชข้อมูล"): | |
| st.cache_data.clear() | |
| st.rerun() | |
| # โหลดข้อมูลและตัวกรองจังหวัด | |
| df = load_data() | |
| locations = df['service_location'].dropna().unique().tolist() | |
| locations.insert(0, "ดูทั้งหมด") | |
| selected_location = st.selectbox("เลือกจังหวัด/สถานที่บริการ", options=locations) | |
| if selected_location == "ดูทั้งหมด": | |
| df_filtered = df.copy() | |
| else: | |
| df_filtered = df[df['service_location'] == selected_location] | |
| # ---------- SIDEBAR MENU ---------- | |
| menu = st.sidebar.radio("เลือกหน้าที่จะแสดง", ["Dashboard สรุป", "Bar Chart เปรียบเทียบความรู้สึก"]) | |
| # ---------- กำหนดสี ---------- | |
| sentiment_colors = {'pos': 'skyblue', 'neg': 'red'} | |
| if menu == "Dashboard สรุป": | |
| st.subheader("1. สัดส่วนความรู้สึก (แยกตามประเภท)") | |
| text_columns = { | |
| 'user_feeling_first_use': 'คุณรู้สึกอย่างไรเมื่อใช้งานแอปพลิเคชัน CheckPD ครั้งแรก?', | |
| 'staff_emotion_feedback': 'บริการของเจ้าหน้าที่ทำให้คุณรู้สึกอย่างไร?', | |
| 'improvement_suggestions': 'สิ่งใดในแอปพลิเคชันหรืองานบริการที่คุณคิดว่าควรปรับปรุง?' | |
| } | |
| for col, label in text_columns.items(): | |
| chart_col = col + '_sentiment' | |
| df_chart = df_filtered[df_filtered[chart_col].isin(['pos', 'neg'])] | |
| st.markdown(f"#### ➤ {label}") | |
| fig = px.pie(df_chart, names=chart_col, title=f"สัดส่วนความรู้สึก - {label}", hole=0.4, | |
| color=chart_col, color_discrete_map=sentiment_colors) | |
| fig.update_traces(textfont_size=20) | |
| st.plotly_chart(fig, use_container_width=True) | |
| st.subheader("2. สัดส่วนภาพรวมทุกประเภท") | |
| combined_counts = pd.concat([ | |
| df_filtered['user_feeling_first_use_sentiment'], | |
| df_filtered['staff_emotion_feedback_sentiment'], | |
| df_filtered['improvement_suggestions_sentiment'] | |
| ]) | |
| combined_counts = combined_counts[combined_counts.isin(['pos', 'neg'])] | |
| combined_counts = combined_counts.value_counts().reset_index() | |
| combined_counts.columns = ['sentiment', 'count'] | |
| fig_total = px.pie(combined_counts, names='sentiment', values='count', | |
| title="สัดส่วนความรู้สึกโดยรวมจากทุกหมวด", hole=0.4, | |
| color='sentiment', color_discrete_map=sentiment_colors) | |
| fig_total.update_traces(textfont_size=20) | |
| st.plotly_chart(fig_total, use_container_width=True) | |
| st.subheader("3. ข้อความที่ให้ความคิดเห็น (ตามตัวกรอง)") | |
| cols_to_show = [ | |
| 'service_location', | |
| 'user_feeling_first_use', | |
| 'staff_emotion_feedback', | |
| 'improvement_suggestions' | |
| ] | |
| st.dataframe(df_filtered[cols_to_show], use_container_width=True) | |
| elif menu == "Bar Chart เปรียบเทียบความรู้สึก": | |
| st.subheader("เปรียบเทียบความรู้สึกในแต่ละหมวดด้วย Bar Chart") | |
| text_columns = { | |
| 'user_feeling_first_use_sentiment': 'รู้สึกเมื่อใช้งานครั้งแรก', | |
| 'staff_emotion_feedback_sentiment': 'รู้สึกต่อเจ้าหน้าที่', | |
| 'improvement_suggestions_sentiment': 'ข้อเสนอแนะต่อแอป/บริการ' | |
| } | |
| for col, label in text_columns.items(): | |
| st.markdown(f"#### ➤ {label}") | |
| df_chart = df_filtered[df_filtered[col].isin(['pos', 'neg'])] | |
| counts = df_chart[col].value_counts().reset_index() | |
| counts.columns = ['sentiment', 'count'] | |
| fig = px.bar(counts, x='sentiment', y='count', | |
| labels={'sentiment': 'ความรู้สึก', 'count': 'จำนวน'}, | |
| title=f"Bar Chart - {label}", | |
| color='sentiment', color_discrete_map=sentiment_colors) | |
| fig.update_layout(font=dict(size=18)) | |
| st.plotly_chart(fig, use_container_width=True) | |
| # ---------- SIDEBAR INFO ---------- | |
| st.sidebar.title("Model & Pipeline") | |
| st.sidebar.info("กำลังใช้โมเดล: phoner45/wangchan-sentiment-thai-text-model") | |