File size: 5,810 Bytes
c53cec3
6fffb3a
 
 
 
b38b1f8
96c4799
e73b762
c53cec3
 
e73b762
6fffb3a
c53cec3
6fffb3a
c53cec3
 
96c4799
6c55ffe
6be0105
e4e76f9
c53cec3
 
 
 
b38b1f8
c53cec3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b38b1f8
6fffb3a
 
 
 
 
c53cec3
 
 
 
6fffb3a
 
c53cec3
 
 
 
 
 
 
6fffb3a
c53cec3
6fffb3a
c53cec3
 
 
 
 
 
 
0731120
 
 
 
 
6c55ffe
 
 
 
 
 
 
 
 
 
 
0731120
 
 
 
 
 
 
 
 
 
 
 
c53cec3
 
 
 
 
 
 
 
 
 
e73b762
b38b1f8
c53cec3
 
 
 
 
 
 
 
 
 
 
6fffb3a
c53cec3
 
 
 
 
 
6be0105
c53cec3
 
 
 
6fffb3a
c53cec3
6fffb3a
e73b762
6fffb3a
e73b762
6fffb3a
e73b762
6fffb3a
e73b762
c53cec3
0731120
 
6fffb3a
c53cec3
6fffb3a
 
 
 
0731120
c53cec3
6fffb3a
a0fd2a1
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
from fastapi import FastAPI
from pydantic import BaseModel
from typing import Optional
import requests
import torch
from transformers import AutoTokenizer, BertForSequenceClassification
from huggingface_hub import hf_hub_download

import logging
logger = logging.getLogger("app")
logging.basicConfig(level=logging.INFO)

# =====================================================
# CONFIG
# =====================================================
HF_MODEL_REPO = "gaidasalsaa/indobertweet-xstress-model"
BASE_MODEL = "indolem/indobertweet-base-uncased"
PT_FILE = "model_indobertweet.pth"

BEARER_TOKEN = "AAAAAAAAAAAAAAAAAAAAADXr5gEAAAAAnQZgkYRrC4iM5WTblBxDyt58oj8%3DriQZkuHuvRL6Suc3rmDhD3umqbHaxwim2Tfb34rfQpnKqf9Xhd"

# =====================================================
# GLOBAL MODEL STORAGE
# =====================================================
tokenizer = None
model = None


# =====================================================
# LOAD MODEL 
# =====================================================
def load_model_once():
    global tokenizer, model

    if tokenizer is not None and model is not None:
        logger.info("Model already loaded.")
        return

    logger.info("Starting model loading...")

    device = "cpu"
    logger.info(f"Using device: {device}")

    # ---- load tokenizer ----
    logger.info("Loading tokenizer...")
    tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
    logger.info("Tokenizer loaded")

    # ---- download .pth ----
    logger.info("Downloading best_indobertweet.pth...")
    model_path = hf_hub_download(
        repo_id=HF_MODEL_REPO,
        filename=PT_FILE,
    )
    logger.info(f"Model file downloaded: {model_path}")

    logger.info("Loading base model architecture...")
    model = BertForSequenceClassification.from_pretrained(
        BASE_MODEL,
        num_labels=2,
    )

    logger.info("Loading fine-tuned weights (.pth)...")
    state_dict = torch.load(model_path, map_location="cpu")

    model.load_state_dict(state_dict, strict=True)
    logger.info("Weights loaded successfully")

    model.to(device)
    model.eval()

    logger.info("MODEL READY")


# =====================================================
# FASTAPI
# =====================================================
app = FastAPI(title="Stress Detection API")


@app.on_event("startup")
def startup_event():
    logger.info("Starting model loading on startup...")
    load_model_once()


class StressResponse(BaseModel):
    message: str
    data: Optional[dict] = None


# =====================================================
# TWITTER API
# =====================================================
def get_user_id(username):
    url = f"https://api.x.com/2/users/by/username/{username}"
    headers = {"Authorization": f"Bearer {BEARER_TOKEN}"}
    r = requests.get(url, headers=headers)
    if r.status_code != 200:
        return None, r.json()
    return r.json()["data"]["id"], r.json()


def fetch_tweets(user_id, limit=25):
    url = f"https://api.x.com/2/users/{user_id}/tweets"
    params = {"max_results": limit, "tweet.fields": "id,text,created_at"}
    headers = {"Authorization": f"Bearer {BEARER_TOKEN}"}
    r = requests.get(url, headers=headers, params=params)
    if r.status_code != 200:
        return None, r.json()
    tweets = r.json().get("data", [])
    return [t["text"] for t in tweets], r.json()


# =====================================================
# KEYWORD EXTRACTION
# =====================================================
def extract_keywords(tweets):
    stress_words = [
    "gelisah","cemas","tidur","takut","hati",
    "resah","sampe","tenang","suka","mulu",
    "sedih","ngerasa","gimana","gatau",
    "perasaan","nangis","deg","khawatir",
    "pikiran","harap","gabisa","bener","pengen",
    "sakit","susah","bangun","biar","jam","kaya",
    "bingung","mikir","tuhan","mikirin",
    "bawaannya","marah","tbtb","anjir","cape",
    "panik","enak","kali","pusing","semoga",
    "kadang","langsung","kemarin","tugas",
    "males"
    ]

    found = set()
    for t in tweets:
        lower = t.lower()
        for word in stress_words:
            if word in lower:
                found.add(word)

    return list(found)


# =====================================================
# INFERENCE
# =====================================================
def predict_stress(text):
    inputs = tokenizer(
        text,
        return_tensors="pt",
        truncation=True,
        padding=True,
        max_length=128
    )

    with torch.no_grad():
        outputs = model(**inputs)
        probs = torch.softmax(outputs.logits, dim=1)[0]

    label = torch.argmax(probs).item()
    return label, float(probs[1])


# =====================================================
# API ROUTE
# =====================================================
@app.get("/analyze/{username}", response_model=StressResponse)
def analyze(username: str):
    user_id, _ = get_user_id(username)
    if user_id is None:
        return StressResponse(message="Failed to fetch profile", data=None)

    tweets, _ = fetch_tweets(user_id)
    if not tweets:
        return StressResponse(message="No tweets available", data=None)

    labels = [predict_stress(t)[0] for t in tweets]

    stress_percentage = round(sum(labels) / len(labels) * 100, 2)

    if stress_percentage <= 25:
        status = 0
    elif stress_percentage <= 50:
        status = 1
    elif stress_percentage <= 75:
        status = 2
    else:
        status = 3

    keywords = extract_keywords(tweets)

    return StressResponse(
        message="Analysis complete",
        data={
            "username": username,
            "total_tweets": len(tweets),
            "stress_level": stress_percentage,
            "keywords": keywords,
            "stress_status": status
        }
    )