File size: 9,004 Bytes
686a3d6
 
 
 
 
 
cf81d51
 
21f7535
686a3d6
cf81d51
 
 
686a3d6
 
5889719
cf81d51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
686a3d6
 
5889719
686a3d6
 
5889719
 
 
686a3d6
5889719
 
 
 
 
 
 
 
 
 
 
cf81d51
 
 
 
5889719
686a3d6
 
cf81d51
 
686a3d6
cf81d51
 
 
5889719
 
686a3d6
cf81d51
686a3d6
 
cf81d51
686a3d6
cf81d51
5889719
 
cf81d51
 
 
 
 
 
 
686a3d6
 
 
 
21f7535
686a3d6
 
21f7535
 
 
686a3d6
 
cf81d51
 
 
686a3d6
21f7535
686a3d6
21f7535
 
686a3d6
21f7535
 
 
686a3d6
21f7535
686a3d6
 
cf81d51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
686a3d6
cf81d51
686a3d6
21f7535
cf81d51
 
21f7535
1c8e925
 
cf81d51
686a3d6
1c8e925
686a3d6
 
1c8e925
 
686a3d6
 
1c8e925
 
 
21f7535
cf81d51
 
 
1c8e925
cf81d51
21f7535
 
 
 
cf81d51
21f7535
 
5889719
 
cf81d51
686a3d6
cf81d51
 
 
686a3d6
1c8e925
cf81d51
 
686a3d6
 
cf81d51
 
686a3d6
cf81d51
 
686a3d6
 
cf81d51
686a3d6
 
 
 
cf81d51
686a3d6
 
 
21f7535
686a3d6
21f7535
686a3d6
21f7535
686a3d6
 
cf81d51
686a3d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21f7535
686a3d6
21f7535
686a3d6
21f7535
cf81d51
5889719
686a3d6
 
 
 
 
 
 
 
 
21f7535
686a3d6
21f7535
 
 
 
686a3d6
21f7535
 
 
686a3d6
21f7535
 
 
 
686a3d6
21f7535
 
686a3d6
 
 
 
 
 
 
cf81d51
686a3d6
cf81d51
 
686a3d6
 
cf81d51
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""
Trackmodel - Gradio Web App
This app loads your fine-tuned model and provides a web interface
"""

import os
import shutil
import subprocess
import sys

import gradio as gr
from transformers import pipeline

classifier = None

HUB_MODEL = "Surajnitp/sentiment-trackmodel"
LOCAL_MODEL_DIR = os.path.abspath("./my_finetuned_model")
DATA_MODEL_DIR = "/data/my_finetuned_model"


def persistent_model_dir():
    if os.path.isdir("/data"):
        return DATA_MODEL_DIR
    return LOCAL_MODEL_DIR


def model_dir_is_valid(path):
    if not path or not os.path.isdir(path):
        return False
    has_config = os.path.isfile(os.path.join(path, "config.json"))
    has_weights = any(
        os.path.isfile(os.path.join(path, name))
        for name in ("model.safetensors", "pytorch_model.bin", "model.safetensors.index.json")
    )
    return has_config and has_weights


def find_local_model_dir():
    for path in (DATA_MODEL_DIR, LOCAL_MODEL_DIR, "./my_finetuned_model"):
        if model_dir_is_valid(path):
            return os.path.abspath(path)
    return None


def format_status(text):
    return f"""<div class="status-box">

**Status:** {text}

</div>"""


def load_model():
    """Load Hub model first, then local weights, then the default SST-2 model."""
    global classifier

    print(f"[load_model] cwd={os.getcwd()}", flush=True)
    print(f"[load_model] HF_TOKEN set={bool(os.getenv('HF_TOKEN'))}", flush=True)

    try:
        print(f"[load_model] trying Hub: {HUB_MODEL}", flush=True)
        classifier = pipeline(
            "sentiment-analysis",
            model=HUB_MODEL,
            tokenizer=HUB_MODEL,
        )
        status = f"โœ… Loaded model from Hub: {HUB_MODEL}"
        print(f"[load_model] {status}", flush=True)
        return status
    except Exception as hub_err:
        print(f"[load_model] Hub load failed: {hub_err}", flush=True)

    model_path = find_local_model_dir()
    if model_path:
        try:
            print(f"[load_model] trying local: {model_path}", flush=True)
            classifier = pipeline(
                "sentiment-analysis",
                model=model_path,
                tokenizer=model_path,
            )
            status = f"โœ… Loaded fine-tuned model from local ({model_path})"
            print(f"[load_model] {status}", flush=True)
            return status
        except Exception as local_err:
            print(f"[load_model] local load failed: {local_err}", flush=True)

    try:
        classifier = pipeline(
            "sentiment-analysis",
            model="distilbert-base-uncased-finetuned-sst-2-english",
        )
        status = (
            "โš ๏ธ Using default model. Hub and local fine-tuned weights were not found. "
            f"Set HF_TOKEN, train once to push {HUB_MODEL}, then restart."
        )
        print(f"[load_model] {status}", flush=True)
        return status
    except Exception as e2:
        status = f"โŒ CRITICAL: Could not load any model: {str(e2)}"
        print(f"[load_model] {status}", flush=True)
        return status


def predict(text):
    """Predict sentiment of input text"""
    if not text or not text.strip():
        return "โš ๏ธ Please enter some text to analyze."

    if classifier is None:
        return "โŒ Model not loaded. Please restart the app."

    try:
        result = classifier(text)
        label = result[0]["label"]
        score = result[0]["score"]
        emoji = "๐Ÿ˜Š" if label.upper() in ("POSITIVE", "POS", "LABEL_1") else "๐Ÿ˜ž"
        return f"""
### ๐Ÿ“Š Sentiment Analysis Result

{emoji} **Label:** {label}
๐ŸŽฏ **Confidence:** {score:.2%}

---
๐Ÿ“ **Text analyzed:** "{text[:100]}{'...' if len(text) > 100 else ''}"
"""
    except Exception as e:
        return f"โŒ Error during prediction: {str(e)}"


def persist_trained_model():
    """Copy weights to /data when persistent storage is available."""
    src = find_local_model_dir() or LOCAL_MODEL_DIR
    dest = persistent_model_dir()
    if not model_dir_is_valid(src):
        return src, dest, False
    if os.path.abspath(src) != os.path.abspath(dest):
        os.makedirs(os.path.dirname(dest) or ".", exist_ok=True)
        if os.path.isdir(dest):
            shutil.rmtree(dest)
        shutil.copytree(src, dest)
        print(f"[persist] copied {src} -> {dest}", flush=True)
    return src, dest, True


def run_training():
    """Run the training script and return training log + live status."""
    try:
        if not os.path.exists("train.py"):
            status = "โŒ Training script not found!"
            return status, format_status(status)

        env = os.environ.copy()
        env["PYTHONUNBUFFERED"] = "1"
        env["MODEL_SAVE_DIR"] = persistent_model_dir()
        result = subprocess.run(
            [sys.executable, "-u", "train.py"],
            capture_output=True,
            text=True,
            timeout=1200,
            env=env,
        )

        full_output = (result.stdout or "") + (result.stderr or "")
        if len(full_output) > 4000:
            full_output = full_output[:4000] + "\n... (output truncated)"

        persist_trained_model()
        model_path = find_local_model_dir()
        if result.returncode == 0 and model_path:
            load_status = load_model()
            message = f"""โœ… Training completed successfully!

Full Output:
{full_output}

Model saved to: {model_path}
Status: {load_status}

If HF_TOKEN is set, the model was also pushed to {HUB_MODEL}.
Restart the Space to load permanently from the Hub."""
            return message, format_status(load_status)

        fail_status = load_model()
        message = f"""โŒ Training failed. (Exit code: {result.returncode})
Model folder present: {bool(model_path)}

Output:
{full_output}"""
        return message, format_status(fail_status)

    except subprocess.TimeoutExpired:
        status = "โฐ Training timed out after 20 minutes."
        return status, format_status(load_model())
    except Exception as e:
        status = f"โŒ Unexpected error during training: {str(e)}"
        return status, format_status(status)


startup_status = load_model()

with gr.Blocks(
    title="Trackmodel - Sentiment Analysis",
    theme=gr.themes.Soft(),
    css=".gradio-container {max-width: 800px; margin: auto;} .status-box {padding: 10px; border-radius: 5px; background: #f0f0f0;}",
) as demo:

    gr.Markdown("""
# ๐ŸŽฏ Trackmodel - Sentiment Analysis

This Space demonstrates downloading, fine-tuning, and deploying models on Hugging Face.

**Model:** DistilBERT fine-tuned on IMDb reviews for sentiment classification.
    """)

    status_box = gr.Markdown(format_status(startup_status))

    with gr.Tab("๐Ÿ’ฌ Predict"):
        gr.Markdown("### Enter text to analyze sentiment")

        with gr.Row():
            with gr.Column(scale=4):
                text_input = gr.Textbox(
                    label="Your text",
                    placeholder="Type something like: 'I love this movie!'",
                    lines=4,
                )
            with gr.Column(scale=1):
                predict_btn = gr.Button("๐Ÿ”ฎ Predict", variant="primary", size="lg")
                clear_btn = gr.Button("๐Ÿ—‘๏ธ Clear", variant="secondary")

        output = gr.Markdown(
            value="๐Ÿ“ Enter text and click 'Predict' to see results.",
        )

    with gr.Tab("โš™๏ธ Training"):
        gr.Markdown("""
### Train the Model on Custom Data

Click the button below to fine-tune the model on the IMDb dataset.

โš ๏ธ **Note:** This will take ~3-5 minutes on a CPU instance.

Add Space secret `HF_TOKEN` first. Training then uploads to `Surajnitp/sentiment-trackmodel` so the model survives restarts.
        """)

        with gr.Row():
            train_btn = gr.Button("๐Ÿš€ Start Training", variant="primary", size="lg")

        train_output = gr.Markdown("โ„น๏ธ Click the button to start training.")

    with gr.Tab("โ„น๏ธ About"):
        gr.Markdown("""
## About Trackmodel

### What this Space does:
1. **Downloads** a pre-trained DistilBERT model
2. **Fine-tunes** it on the IMDb movie review dataset
3. **Deploys** the trained model as a web app

### How to use:
- Go to the **Predict** tab to analyze text sentiment
- Go to the **Training** tab to fine-tune the model yourself

### Links:
- [Hugging Face Models](https://huggingface.co/models)
- [Gradio Documentation](https://gradio.app/docs)
- [Hugging Face Course](https://huggingface.co/learn/nlp-course)

### Author:
**Surajnitp** ๐Ÿš€
        """)

    predict_btn.click(fn=predict, inputs=text_input, outputs=output)
    clear_btn.click(fn=lambda: "", inputs=None, outputs=text_input)
    clear_btn.click(
        fn=lambda: "๐Ÿ“ Enter text and click 'Predict' to see results.",
        inputs=None,
        outputs=output,
    )
    train_btn.click(fn=run_training, inputs=None, outputs=[train_output, status_box])
    demo.load(fn=lambda: format_status(load_model()), outputs=status_box)

if __name__ == "__main__":
    demo.launch()