gaidasalsaa commited on
Commit
0731120
·
1 Parent(s): c53cec3

added keyword

Browse files
Files changed (1) hide show
  1. app.py +23 -4
app.py CHANGED
@@ -54,15 +54,12 @@ def load_model_once():
54
  )
55
  logger.info(f"Model file downloaded: {model_path}")
56
 
57
- # ---- load base model architecture ----
58
  logger.info("Loading base model architecture...")
59
  model = BertForSequenceClassification.from_pretrained(
60
  BASE_MODEL,
61
  num_labels=2,
62
- # ❗ PENTING: Tidak boleh pakai low_cpu_mem_usage atau device_map
63
  )
64
 
65
- # ---- load state_dict ----
66
  logger.info("Loading fine-tuned weights (.pth)...")
67
  state_dict = torch.load(model_path, map_location="cpu")
68
 
@@ -95,7 +92,6 @@ class StressResponse(BaseModel):
95
  # =====================================================
96
  # TWITTER API
97
  # =====================================================
98
-
99
  def get_user_id(username):
100
  url = f"https://api.x.com/2/users/by/username/{username}"
101
  headers = {"Authorization": f"Bearer {BEARER_TOKEN}"}
@@ -116,6 +112,26 @@ def fetch_tweets(user_id, limit=25):
116
  return [t["text"] for t in tweets], r.json()
117
 
118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  # =====================================================
120
  # INFERENCE
121
  # =====================================================
@@ -162,12 +178,15 @@ def analyze(username: str):
162
  else:
163
  status = 3
164
 
 
 
165
  return StressResponse(
166
  message="Analysis complete",
167
  data={
168
  "username": username,
169
  "total_tweets": len(tweets),
170
  "stress_level": stress_percentage,
 
171
  "stress_status": status
172
  }
173
  )
 
54
  )
55
  logger.info(f"Model file downloaded: {model_path}")
56
 
 
57
  logger.info("Loading base model architecture...")
58
  model = BertForSequenceClassification.from_pretrained(
59
  BASE_MODEL,
60
  num_labels=2,
 
61
  )
62
 
 
63
  logger.info("Loading fine-tuned weights (.pth)...")
64
  state_dict = torch.load(model_path, map_location="cpu")
65
 
 
92
  # =====================================================
93
  # TWITTER API
94
  # =====================================================
 
95
  def get_user_id(username):
96
  url = f"https://api.x.com/2/users/by/username/{username}"
97
  headers = {"Authorization": f"Bearer {BEARER_TOKEN}"}
 
112
  return [t["text"] for t in tweets], r.json()
113
 
114
 
115
+ # =====================================================
116
+ # KEYWORD EXTRACTION
117
+ # =====================================================
118
+ def extract_keywords(tweets):
119
+ stress_words = [
120
+ "capek", "cape", "capai", "letih", "lelah", "pusing",
121
+ "stress", "stres", "burnout", "kesal", "badmood",
122
+ "sedih", "tertekan", "muak", "bosan"
123
+ ]
124
+
125
+ found = set()
126
+ for t in tweets:
127
+ lower = t.lower()
128
+ for word in stress_words:
129
+ if word in lower:
130
+ found.add(word)
131
+
132
+ return list(found)
133
+
134
+
135
  # =====================================================
136
  # INFERENCE
137
  # =====================================================
 
178
  else:
179
  status = 3
180
 
181
+ keywords = extract_keywords(tweets)
182
+
183
  return StressResponse(
184
  message="Analysis complete",
185
  data={
186
  "username": username,
187
  "total_tweets": len(tweets),
188
  "stress_level": stress_percentage,
189
+ "keywords": keywords,
190
  "stress_status": status
191
  }
192
  )