Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- .gitignore +4 -0
- Dockerfile +15 -0
- app.py +53 -0
- requirements.txt +5 -0
.gitignore
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Ignore large model file from main git repo — CI pushes it directly to the HF Space repo
|
| 2 |
+
model.pkl
|
| 3 |
+
__pycache__/
|
| 4 |
+
*.pyc
|
Dockerfile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# Install dependencies first for better Docker layer caching
|
| 6 |
+
COPY requirements.txt .
|
| 7 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 8 |
+
|
| 9 |
+
# Copy app code and model
|
| 10 |
+
COPY . .
|
| 11 |
+
|
| 12 |
+
# Hugging Face Docker Spaces expect the app on port 7860
|
| 13 |
+
EXPOSE 7860
|
| 14 |
+
|
| 15 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
import joblib
|
| 5 |
+
import numpy as np
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
app = FastAPI(title="GitHub Analytics ML Inference")
|
| 9 |
+
|
| 10 |
+
app.add_middleware(
|
| 11 |
+
CORSMiddleware,
|
| 12 |
+
allow_origins=["*"],
|
| 13 |
+
allow_methods=["*"],
|
| 14 |
+
allow_headers=["*"],
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Load model at startup
|
| 18 |
+
MODEL_PATH = os.path.join(os.path.dirname(__file__), "model.pkl")
|
| 19 |
+
model = None
|
| 20 |
+
try:
|
| 21 |
+
model = joblib.load(MODEL_PATH)
|
| 22 |
+
print(f"Model loaded successfully from {MODEL_PATH}")
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"Warning: Could not load model: {e}")
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class PredictionInput(BaseModel):
|
| 28 |
+
stars: float
|
| 29 |
+
forks: float
|
| 30 |
+
contributors: float
|
| 31 |
+
commit_frequency: float
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
@app.get("/")
|
| 35 |
+
def health():
|
| 36 |
+
return {"status": "ok", "model_loaded": model is not None}
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
@app.post("/predict")
|
| 40 |
+
def predict(input: PredictionInput):
|
| 41 |
+
if model is None:
|
| 42 |
+
from fastapi import HTTPException
|
| 43 |
+
raise HTTPException(status_code=503, detail="Model not loaded. Run ML training pipeline first.")
|
| 44 |
+
|
| 45 |
+
# Feature order must match train_model.py: stars, forks, contributors_count, commit_frequency
|
| 46 |
+
features = np.array([[
|
| 47 |
+
input.stars,
|
| 48 |
+
input.forks,
|
| 49 |
+
input.contributors,
|
| 50 |
+
input.commit_frequency,
|
| 51 |
+
]])
|
| 52 |
+
prediction = model.predict(features)[0]
|
| 53 |
+
return {"predicted_stars_next_month": float(prediction)}
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
scikit-learn
|
| 4 |
+
numpy
|
| 5 |
+
joblib
|