GphaHoa commited on
Commit
7894107
·
verified ·
1 Parent(s): 8cc9280

Upload streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +27 -2
src/streamlit_app.py CHANGED
@@ -1,10 +1,22 @@
1
  import streamlit as st
 
 
2
  from supabase import create_client
3
  from datetime import datetime
4
  from zoneinfo import ZoneInfo
5
  from supabase_auth.errors import AuthApiError
6
- import time
7
- import os
 
 
 
 
 
 
 
 
 
 
8
  # ========== CONFIG ==========
9
  st.set_page_config(
10
  page_title="Personal Diary",
@@ -42,6 +54,10 @@ def signup(email, password):
42
  "password": password
43
  })
44
 
 
 
 
 
45
 
46
  # ========== HEADER ==========
47
  st.title("📔 Personal Diary")
@@ -159,9 +175,11 @@ else:
159
 
160
  if st.button("💾 Lưu nhật ký", use_container_width=True):
161
  if content.strip():
 
162
  supabase.table("journals").insert({
163
  "user_id": user.id,
164
  "content": content,
 
165
  "created_at": datetime.utcnow().isoformat()
166
  }).execute()
167
 
@@ -200,6 +218,13 @@ else:
200
  key=row["id"],
201
  height=120
202
  )
 
 
 
 
 
 
 
203
 
204
  col_u, col_d = st.columns(2)
205
 
 
1
  import streamlit as st
2
+ import time
3
+ import os
4
  from supabase import create_client
5
  from datetime import datetime
6
  from zoneinfo import ZoneInfo
7
  from supabase_auth.errors import AuthApiError
8
+ from transformers import pipeline
9
+
10
+
11
+ @st.cache_resource
12
+ def load_sentiment_model():
13
+ return pipeline(
14
+ "sentiment-analysis",
15
+ model="wonrax/phobert-base-vietnamese-sentiment"
16
+ )
17
+ sentiment_model = load_sentiment_model()
18
+
19
+
20
  # ========== CONFIG ==========
21
  st.set_page_config(
22
  page_title="Personal Diary",
 
54
  "password": password
55
  })
56
 
57
+ def analyze_sentiment(text: str) -> str:
58
+ result = sentiment_model(text)[0]
59
+ return result["label"] # POS / NEG / NEU
60
+
61
 
62
  # ========== HEADER ==========
63
  st.title("📔 Personal Diary")
 
175
 
176
  if st.button("💾 Lưu nhật ký", use_container_width=True):
177
  if content.strip():
178
+ sentiment = analyze_sentiment(content)
179
  supabase.table("journals").insert({
180
  "user_id": user.id,
181
  "content": content,
182
+ "sentiment": sentiment,
183
  "created_at": datetime.utcnow().isoformat()
184
  }).execute()
185
 
 
218
  key=row["id"],
219
  height=120
220
  )
221
+ sentiment = row.get("sentiment", "N/A")
222
+ emoji_map = {
223
+ "Vui vẻ": "😊",
224
+ "Buồn": "😔",
225
+ "Trung tính": "😐"
226
+ }
227
+ st.caption(f"Cảm xúc: {emoji_map.get(sentiment, '❓')} {sentiment}")
228
 
229
  col_u, col_d = st.columns(2)
230