"""
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"""
**Status:** {text}
"""
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()