|
|
import gradio as gr |
|
|
import joblib |
|
|
import numpy as np |
|
|
import pandas as pd |
|
|
from sklearn.preprocessing import StandardScaler |
|
|
from fastapi import FastAPI, HTTPException |
|
|
from pydantic import BaseModel |
|
|
import uvicorn |
|
|
import os |
|
|
|
|
|
|
|
|
app = FastAPI(title="Developer Productivity Prediction API", version="1.0.0") |
|
|
|
|
|
|
|
|
model = joblib.load('developer_productivity_model.joblib') |
|
|
scaler = joblib.load('scaler.joblib') |
|
|
|
|
|
|
|
|
class ProductivityRequest(BaseModel): |
|
|
daily_coding_hours: float |
|
|
commits_per_day: int |
|
|
pull_requests_per_week: int |
|
|
issues_closed_per_week: int |
|
|
active_repos: int |
|
|
code_reviews_per_week: int |
|
|
|
|
|
class ProductivityResponse(BaseModel): |
|
|
predicted_score: float |
|
|
status: str |
|
|
|
|
|
def predict_productivity_core(daily_coding_hours, commits_per_day, pull_requests_per_week, |
|
|
issues_closed_per_week, active_repos, code_reviews_per_week): |
|
|
""" |
|
|
Core prediction function used by both API and Gradio interface. |
|
|
""" |
|
|
try: |
|
|
|
|
|
features = np.array([[ |
|
|
daily_coding_hours, |
|
|
commits_per_day, |
|
|
pull_requests_per_week, |
|
|
issues_closed_per_week, |
|
|
active_repos, |
|
|
code_reviews_per_week |
|
|
]]) |
|
|
|
|
|
|
|
|
features_scaled = scaler.transform(features) |
|
|
|
|
|
|
|
|
prediction = model.predict(features_scaled)[0] |
|
|
|
|
|
return round(prediction, 2) |
|
|
|
|
|
except Exception as e: |
|
|
raise HTTPException(status_code=500, detail=f"Prediction error: {str(e)}") |
|
|
|
|
|
|
|
|
@app.get("/") |
|
|
def read_root(): |
|
|
return {"message": "Developer Productivity Prediction API", "status": "active"} |
|
|
|
|
|
@app.post("/predict", response_model=ProductivityResponse) |
|
|
def predict_productivity_api(request: ProductivityRequest): |
|
|
""" |
|
|
API endpoint to predict developer productivity score. |
|
|
""" |
|
|
try: |
|
|
prediction = predict_productivity_core( |
|
|
request.daily_coding_hours, |
|
|
request.commits_per_day, |
|
|
request.pull_requests_per_week, |
|
|
request.issues_closed_per_week, |
|
|
request.active_repos, |
|
|
request.code_reviews_per_week |
|
|
) |
|
|
|
|
|
return ProductivityResponse( |
|
|
predicted_score=prediction, |
|
|
status="success" |
|
|
) |
|
|
|
|
|
except Exception as e: |
|
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|
|
|
|
@app.get("/health") |
|
|
def health_check(): |
|
|
return {"status": "healthy", "model_loaded": True} |
|
|
|
|
|
|
|
|
def predict_productivity_gradio(daily_coding_hours, commits_per_day, pull_requests_per_week, |
|
|
issues_closed_per_week, active_repos, code_reviews_per_week): |
|
|
""" |
|
|
Gradio wrapper for the prediction function. |
|
|
""" |
|
|
try: |
|
|
prediction = predict_productivity_core( |
|
|
daily_coding_hours, |
|
|
commits_per_day, |
|
|
pull_requests_per_week, |
|
|
issues_closed_per_week, |
|
|
active_repos, |
|
|
code_reviews_per_week |
|
|
) |
|
|
|
|
|
return f"Predicted Productivity Score: {prediction}" |
|
|
|
|
|
except Exception as e: |
|
|
return f"Error: {str(e)}" |
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=predict_productivity_gradio, |
|
|
inputs=[ |
|
|
gr.Slider(minimum=1, maximum=12, value=4.0, step=0.1, label="Daily Coding Hours"), |
|
|
gr.Slider(minimum=0, maximum=20, value=5, step=1, label="Commits per Day"), |
|
|
gr.Slider(minimum=0, maximum=15, value=4, step=1, label="Pull Requests per Week"), |
|
|
gr.Slider(minimum=0, maximum=15, value=3, step=1, label="Issues Closed per Week"), |
|
|
gr.Slider(minimum=1, maximum=20, value=5, step=1, label="Active Repositories"), |
|
|
gr.Slider(minimum=0, maximum=15, value=3, step=1, label="Code Reviews per Week") |
|
|
], |
|
|
outputs=gr.Textbox(label="Prediction Result"), |
|
|
title="๐ Developer Productivity Predictor", |
|
|
description=""" |
|
|
### Predict Developer Productivity Score |
|
|
|
|
|
This model predicts developer productivity based on 6 key metrics: |
|
|
- **Daily Coding Hours**: Time spent actively coding |
|
|
- **Commits per Day**: Average daily code commits |
|
|
- **Pull Requests per Week**: Weekly pull requests created |
|
|
- **Issues Closed per Week**: Weekly issues resolved |
|
|
- **Active Repositories**: Number of repositories worked on |
|
|
- **Code Reviews per Week**: Weekly code reviews performed |
|
|
|
|
|
**API Endpoint**: Use `/predict` with POST request for programmatic access. |
|
|
""", |
|
|
examples=[ |
|
|
[4.0, 5, 4, 3, 5, 3], |
|
|
[6.0, 10, 8, 6, 8, 5], |
|
|
[3.0, 2, 2, 1, 2, 1], |
|
|
], |
|
|
theme=gr.themes.Soft() |
|
|
) |
|
|
|
|
|
|
|
|
app = gr.mount_gradio_app(app, iface, path="/") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
port = int(os.environ.get("PORT", 7860)) |
|
|
uvicorn.run(app, host="0.0.0.0", port=port) |