File size: 2,102 Bytes
babcc93
58b3cf9
 
 
 
babcc93
 
 
 
58b3cf9
 
 
 
 
 
 
 
babcc93
 
 
 
 
 
 
 
 
 
 
 
 
 
58b3cf9
 
 
 
 
 
 
babcc93
 
58b3cf9
babcc93
 
 
 
58b3cf9
 
babcc93
58b3cf9
babcc93
 
 
 
 
58b3cf9
babcc93
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
"""
modal_app.py β€” Phase 9: Modal.com Deployment (Modal v1.5+)
------------------------------------------------------------
Deploy:  python -m modal deploy modal_app.py
Test:    python -m modal serve modal_app.py
"""

import modal

# ── Source files to include (explicit list β€” no large dirs) ───────────────────
SOURCE_FILES = [
    "app.py",
    "predictor.py",
    "lang_detector.py",
]

# ── Build container image ─────────────────────────────────────────────────────
image = (
    modal.Image.debian_slim(python_version="3.10")
    .pip_install(
        "fastapi==0.110.0",
        "uvicorn==0.27.0",
        "pydantic==2.6.0",
        "torch==2.2.0",
        "transformers==4.40.0",
        "huggingface_hub==0.22.0",
        "numpy==1.26.0",
        "pandas==2.2.0",
        "scikit-learn==1.4.0",
        "accelerate==0.29.0",
    )
    # Copy only the essential Python source files (not models/, urdu_env/, etc.)
    .add_local_file("app.py",          remote_path="/app/app.py")
    .add_local_file("predictor.py",    remote_path="/app/predictor.py")
    .add_local_file("lang_detector.py",remote_path="/app/lang_detector.py")
    # Copy frontend assets
    .add_local_dir("templates",        remote_path="/app/templates")
    .add_local_dir("static",           remote_path="/app/static")
)

# ── Modal app ─────────────────────────────────────────────────────────────────
app = modal.App("urdu-sentiment-engine", image=image)


@app.function(
    min_containers=1,   # Keep one warm to avoid cold starts
    memory=4096,        # 4GB RAM for two XLM-RoBERTa models (~2.2GB)
    cpu=2.0,
    timeout=300,        # Allow 5min on cold start for model download from HF Hub
)
@modal.asgi_app()
def fastapi_app():
    import sys
    sys.path.insert(0, "/app")
    from app import app as _app
    return _app