Upload 6 files
Browse files- Dockerfile (2).txt +57 -0
- README.md +6 -6
- app.py +140 -0
- dockerignore.txt +48 -0
- gitattributes.txt +35 -0
- requirements (2).txt +9 -0
Dockerfile (2).txt
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use Python 3.9 slim image for better performance
|
2 |
+
FROM python:3.9-slim
|
3 |
+
|
4 |
+
# Set working directory
|
5 |
+
WORKDIR /app
|
6 |
+
|
7 |
+
# Set environment variables
|
8 |
+
ENV PYTHONUNBUFFERED=1
|
9 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
10 |
+
ENV TRANSFORMERS_CACHE=/app/cache
|
11 |
+
ENV HF_HOME=/app/cache
|
12 |
+
|
13 |
+
# Install system dependencies
|
14 |
+
RUN apt-get update && apt-get install -y \
|
15 |
+
build-essential \
|
16 |
+
curl \
|
17 |
+
git \
|
18 |
+
&& rm -rf /var/lib/apt/lists/*
|
19 |
+
|
20 |
+
# Copy requirements first for better caching
|
21 |
+
COPY requirements.txt .
|
22 |
+
|
23 |
+
# Create a non-root user early
|
24 |
+
RUN useradd --create-home --shell /bin/bash app
|
25 |
+
|
26 |
+
# Install Python dependencies as app user
|
27 |
+
RUN pip install --no-cache-dir --upgrade pip && \
|
28 |
+
pip install --no-cache-dir -r requirements.txt
|
29 |
+
|
30 |
+
# Create cache directory and set permissions
|
31 |
+
RUN mkdir -p /app/cache && \
|
32 |
+
chown -R app:app /app
|
33 |
+
|
34 |
+
# Copy application code and set ownership
|
35 |
+
COPY --chown=app:app app.py .
|
36 |
+
|
37 |
+
# Switch to non-root user
|
38 |
+
USER app
|
39 |
+
|
40 |
+
# Pre-download models at build time (optional - comment out if you want faster builds)
|
41 |
+
# RUN python -c "
|
42 |
+
# from transformers import AutoTokenizer, AutoModel;
|
43 |
+
# models = ['Lyon28/Albert-Base-V2', 'Lyon28/GPT-2', 'Lyon28/Tinny-Llama'];
|
44 |
+
# [AutoTokenizer.from_pretrained(m) for m in models[:3]];
|
45 |
+
# [AutoModel.from_pretrained(m) for m in models[:3]];
|
46 |
+
# print('Sample models cached')
|
47 |
+
# "
|
48 |
+
|
49 |
+
# Expose port
|
50 |
+
EXPOSE 7860
|
51 |
+
|
52 |
+
# Health check
|
53 |
+
HEALTHCHECK --interval=30s --timeout=30s --start-period=60s --retries=3 \
|
54 |
+
CMD curl -f http://localhost:7860/health || exit 1
|
55 |
+
|
56 |
+
# Run the application
|
57 |
+
CMD ["python", "app.py"]
|
README.md
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
---
|
2 |
-
|
3 |
-
|
4 |
-
colorFrom: yellow
|
5 |
-
colorTo: purple
|
6 |
sdk: docker
|
|
|
|
|
|
|
7 |
pinned: false
|
8 |
-
license: apache-2.0
|
9 |
---
|
10 |
|
11 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
license: apache-2.0
|
3 |
+
title: ACC
|
|
|
|
|
4 |
sdk: docker
|
5 |
+
emoji: 🚀
|
6 |
+
colorFrom: blue
|
7 |
+
colorTo: purple
|
8 |
pinned: false
|
|
|
9 |
---
|
10 |
|
11 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py untuk Hugging Face Spaces
|
2 |
+
from flask import Flask, jsonify, request, render_template_string
|
3 |
+
from flask_cors import CORS
|
4 |
+
from transformers import pipeline
|
5 |
+
import logging
|
6 |
+
import gc
|
7 |
+
import torch
|
8 |
+
|
9 |
+
app = Flask(__name__)
|
10 |
+
CORS(app)
|
11 |
+
|
12 |
+
# Setup logging
|
13 |
+
logging.basicConfig(level=logging.INFO)
|
14 |
+
logger = logging.getLogger(__name__)
|
15 |
+
|
16 |
+
# Global models storage
|
17 |
+
models = {}
|
18 |
+
model_info = {
|
19 |
+
"Albert-Base-V2": {"task": "fill-mask", "description": "BERT-based model for masked language modeling"},
|
20 |
+
"GPT-2": {"task": "text-generation", "description": "GPT-2 model for text generation"},
|
21 |
+
"Tinny-Llama": {"task": "text-generation", "description": "Lightweight LLaMA model"},
|
22 |
+
"Electra-Small": {"task": "fill-mask", "description": "Small ELECTRA model"},
|
23 |
+
"GPT-2-Tinny": {"task": "text-generation", "description": "Tiny GPT-2 variant"},
|
24 |
+
"Bert-Tinny": {"task": "fill-mask", "description": "Tiny BERT model"},
|
25 |
+
"Distilbert-Base-Uncased": {"task": "fill-mask", "description": "Distilled BERT model"},
|
26 |
+
"Pythia": {"task": "text-generation", "description": "Pythia language model"},
|
27 |
+
"T5-Small": {"task": "text2text-generation", "description": "Small T5 model"},
|
28 |
+
"GPT-Neo": {"task": "text-generation", "description": "GPT-Neo model"},
|
29 |
+
"Distil-GPT-2": {"task": "text-generation", "description": "Distilled GPT-2 model"}
|
30 |
+
}
|
31 |
+
|
32 |
+
def load_models():
|
33 |
+
"""Load all models at startup"""
|
34 |
+
logger.info("Loading all models...")
|
35 |
+
|
36 |
+
for model_name, info in model_info.items():
|
37 |
+
try:
|
38 |
+
logger.info(f"Loading {model_name}...")
|
39 |
+
model_path = f"Lyon28/{model_name}"
|
40 |
+
|
41 |
+
# Load model with appropriate task
|
42 |
+
models[model_name] = pipeline(
|
43 |
+
info["task"],
|
44 |
+
model=model_path,
|
45 |
+
device=-1, # CPU only
|
46 |
+
torch_dtype=torch.float32
|
47 |
+
)
|
48 |
+
logger.info(f"✅ {model_name} loaded successfully")
|
49 |
+
|
50 |
+
except Exception as e:
|
51 |
+
logger.error(f"❌ Failed to load {model_name}: {str(e)}")
|
52 |
+
|
53 |
+
logger.info(f"Loaded {len(models)}/{len(model_info)} models")
|
54 |
+
|
55 |
+
# Load models on startup
|
56 |
+
load_models()
|
57 |
+
|
58 |
+
@app.route('/')
|
59 |
+
def home():
|
60 |
+
"""Simple API status - no HTML interface"""
|
61 |
+
return jsonify({
|
62 |
+
"message": "Lyon28's AI Models API",
|
63 |
+
"status": "online",
|
64 |
+
"total_models": len(model_info),
|
65 |
+
"loaded_models": len(models),
|
66 |
+
"endpoints": {
|
67 |
+
"models_list": "/api/models",
|
68 |
+
"health_check": "/health",
|
69 |
+
"prediction": "/api/{model_name}"
|
70 |
+
},
|
71 |
+
"available_models": list(models.keys())
|
72 |
+
})
|
73 |
+
|
74 |
+
@app.route('/api/models', methods=['GET'])
|
75 |
+
def list_models():
|
76 |
+
"""Get list of available models"""
|
77 |
+
available_models = []
|
78 |
+
for name, info in model_info.items():
|
79 |
+
available_models.append({
|
80 |
+
"name": name,
|
81 |
+
"task": info["task"],
|
82 |
+
"description": info["description"],
|
83 |
+
"status": "ready" if name in models else "failed",
|
84 |
+
"endpoint": f"/api/{name}"
|
85 |
+
})
|
86 |
+
|
87 |
+
return jsonify({
|
88 |
+
"total": len(model_info),
|
89 |
+
"loaded": len(models),
|
90 |
+
"models": available_models
|
91 |
+
})
|
92 |
+
|
93 |
+
@app.route('/api/<model_name>', methods=['POST'])
|
94 |
+
def predict(model_name):
|
95 |
+
"""Main prediction endpoint"""
|
96 |
+
if model_name not in models:
|
97 |
+
return jsonify({
|
98 |
+
"error": f"Model '{model_name}' not available. Available models: {list(models.keys())}"
|
99 |
+
}), 404
|
100 |
+
|
101 |
+
try:
|
102 |
+
data = request.json
|
103 |
+
inputs = data.get('inputs', '')
|
104 |
+
parameters = data.get('parameters', {})
|
105 |
+
|
106 |
+
if not inputs:
|
107 |
+
return jsonify({"error": "No inputs provided"}), 400
|
108 |
+
|
109 |
+
# Get model
|
110 |
+
model = models[model_name]
|
111 |
+
|
112 |
+
# Generate prediction
|
113 |
+
if parameters:
|
114 |
+
result = model(inputs, **parameters)
|
115 |
+
else:
|
116 |
+
result = model(inputs)
|
117 |
+
|
118 |
+
return jsonify({
|
119 |
+
"model": model_name,
|
120 |
+
"inputs": inputs,
|
121 |
+
"outputs": result,
|
122 |
+
"parameters": parameters
|
123 |
+
})
|
124 |
+
|
125 |
+
except Exception as e:
|
126 |
+
logger.error(f"Error with model {model_name}: {str(e)}")
|
127 |
+
return jsonify({"error": str(e)}), 500
|
128 |
+
|
129 |
+
@app.route('/health', methods=['GET'])
|
130 |
+
def health_check():
|
131 |
+
"""Health check endpoint"""
|
132 |
+
return jsonify({
|
133 |
+
"status": "healthy",
|
134 |
+
"models_loaded": len(models),
|
135 |
+
"models_total": len(model_info),
|
136 |
+
"memory_usage": "CPU only"
|
137 |
+
})
|
138 |
+
|
139 |
+
if __name__ == '__main__':
|
140 |
+
app.run(host='0.0.0.0', port=7860, debug=False)
|
dockerignore.txt
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Git
|
2 |
+
.git
|
3 |
+
.gitignore
|
4 |
+
README.md
|
5 |
+
|
6 |
+
# Python
|
7 |
+
__pycache__
|
8 |
+
*.pyc
|
9 |
+
*.pyo
|
10 |
+
*.pyd
|
11 |
+
.Python
|
12 |
+
env/
|
13 |
+
venv/
|
14 |
+
.venv/
|
15 |
+
pip-log.txt
|
16 |
+
pip-delete-this-directory.txt
|
17 |
+
.tox
|
18 |
+
.coverage
|
19 |
+
.coverage.*
|
20 |
+
.cache
|
21 |
+
nosetests.xml
|
22 |
+
coverage.xml
|
23 |
+
*.cover
|
24 |
+
*.log
|
25 |
+
.git
|
26 |
+
.mypy_cache
|
27 |
+
.pytest_cache
|
28 |
+
.hypothesis
|
29 |
+
|
30 |
+
# OS
|
31 |
+
.DS_Store
|
32 |
+
.DS_Store?
|
33 |
+
._*
|
34 |
+
.Spotlight-V100
|
35 |
+
.Trashes
|
36 |
+
ehthumbs.db
|
37 |
+
Thumbs.db
|
38 |
+
|
39 |
+
# IDE
|
40 |
+
.vscode/
|
41 |
+
.idea/
|
42 |
+
*.swp
|
43 |
+
*.swo
|
44 |
+
*~
|
45 |
+
|
46 |
+
# Temporary files
|
47 |
+
*.tmp
|
48 |
+
*.temp
|
gitattributes.txt
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
requirements (2).txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers>=4.36.0
|
2 |
+
torch>=2.1.0
|
3 |
+
flask>=2.3.0
|
4 |
+
flask-cors>=4.0.0
|
5 |
+
huggingface-hub>=0.19.3
|
6 |
+
tokenizers>=0.15.0
|
7 |
+
numpy>=1.24.0
|
8 |
+
requests>=2.31.0
|
9 |
+
safetensors>=0.4.0
|