Update app.py
Browse files
app.py
CHANGED
|
@@ -1,185 +1,150 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import joblib
|
| 3 |
-
import numpy as np
|
| 4 |
-
import pandas as pd
|
| 5 |
-
from sklearn.preprocessing import StandardScaler
|
| 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 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
outputs=output
|
| 152 |
-
)
|
| 153 |
-
|
| 154 |
-
clear_btn.click(
|
| 155 |
-
fn=lambda: [4.0, 5, 4, 3, 5, 3, ""],
|
| 156 |
-
outputs=[daily_hours, commits_day, prs_week, issues_week, repos, reviews_week, output]
|
| 157 |
-
)
|
| 158 |
-
|
| 159 |
-
high_prod_btn.click(
|
| 160 |
-
fn=lambda: [6.0, 10, 8, 6, 8, 5],
|
| 161 |
-
outputs=[daily_hours, commits_day, prs_week, issues_week, repos, reviews_week]
|
| 162 |
-
)
|
| 163 |
-
|
| 164 |
-
avg_prod_btn.click(
|
| 165 |
-
fn=lambda: [4.0, 5, 4, 3, 5, 3],
|
| 166 |
-
outputs=[daily_hours, commits_day, prs_week, issues_week, repos, reviews_week]
|
| 167 |
-
)
|
| 168 |
-
|
| 169 |
-
beginner_btn.click(
|
| 170 |
-
fn=lambda: [3.0, 2, 2, 1, 2, 1],
|
| 171 |
-
outputs=[daily_hours, commits_day, prs_week, issues_week, repos, reviews_week]
|
| 172 |
-
)
|
| 173 |
-
|
| 174 |
-
gr.Markdown(
|
| 175 |
-
"""
|
| 176 |
-
### 📝 About the Model
|
| 177 |
-
This Random Forest model was trained on GitHub developer activity data and uses 6 key metrics to predict productivity scores (0-100).
|
| 178 |
-
The model achieves good performance in predicting developer productivity based on quantifiable GitHub activities.
|
| 179 |
-
|
| 180 |
-
**Note**: This is a demonstration model. Actual productivity depends on many factors beyond GitHub metrics.
|
| 181 |
-
"""
|
| 182 |
-
)
|
| 183 |
-
|
| 184 |
-
if __name__ == "__main__":
|
| 185 |
-
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from sklearn.preprocessing import StandardScaler
|
| 6 |
+
from fastapi import FastAPI, HTTPException
|
| 7 |
+
from pydantic import BaseModel
|
| 8 |
+
import uvicorn
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
# Create FastAPI app
|
| 12 |
+
app = FastAPI(title="Developer Productivity Prediction API", version="1.0.0")
|
| 13 |
+
|
| 14 |
+
# Load the trained model and scaler
|
| 15 |
+
model = joblib.load('developer_productivity_model.joblib')
|
| 16 |
+
scaler = joblib.load('scaler.joblib')
|
| 17 |
+
|
| 18 |
+
# Pydantic model for API request
|
| 19 |
+
class ProductivityRequest(BaseModel):
|
| 20 |
+
daily_coding_hours: float
|
| 21 |
+
commits_per_day: int
|
| 22 |
+
pull_requests_per_week: int
|
| 23 |
+
issues_closed_per_week: int
|
| 24 |
+
active_repos: int
|
| 25 |
+
code_reviews_per_week: int
|
| 26 |
+
|
| 27 |
+
class ProductivityResponse(BaseModel):
|
| 28 |
+
predicted_score: float
|
| 29 |
+
status: str
|
| 30 |
+
|
| 31 |
+
def predict_productivity_core(daily_coding_hours, commits_per_day, pull_requests_per_week,
|
| 32 |
+
issues_closed_per_week, active_repos, code_reviews_per_week):
|
| 33 |
+
"""
|
| 34 |
+
Core prediction function used by both API and Gradio interface.
|
| 35 |
+
"""
|
| 36 |
+
try:
|
| 37 |
+
# Create feature array
|
| 38 |
+
features = np.array([[
|
| 39 |
+
daily_coding_hours,
|
| 40 |
+
commits_per_day,
|
| 41 |
+
pull_requests_per_week,
|
| 42 |
+
issues_closed_per_week,
|
| 43 |
+
active_repos,
|
| 44 |
+
code_reviews_per_week
|
| 45 |
+
]])
|
| 46 |
+
|
| 47 |
+
# Scale features
|
| 48 |
+
features_scaled = scaler.transform(features)
|
| 49 |
+
|
| 50 |
+
# Make prediction
|
| 51 |
+
prediction = model.predict(features_scaled)[0]
|
| 52 |
+
|
| 53 |
+
return round(prediction, 2)
|
| 54 |
+
|
| 55 |
+
except Exception as e:
|
| 56 |
+
raise HTTPException(status_code=500, detail=f"Prediction error: {str(e)}")
|
| 57 |
+
|
| 58 |
+
# FastAPI endpoints
|
| 59 |
+
@app.get("/")
|
| 60 |
+
def read_root():
|
| 61 |
+
return {"message": "Developer Productivity Prediction API", "status": "active"}
|
| 62 |
+
|
| 63 |
+
@app.post("/predict", response_model=ProductivityResponse)
|
| 64 |
+
def predict_productivity_api(request: ProductivityRequest):
|
| 65 |
+
"""
|
| 66 |
+
API endpoint to predict developer productivity score.
|
| 67 |
+
"""
|
| 68 |
+
try:
|
| 69 |
+
prediction = predict_productivity_core(
|
| 70 |
+
request.daily_coding_hours,
|
| 71 |
+
request.commits_per_day,
|
| 72 |
+
request.pull_requests_per_week,
|
| 73 |
+
request.issues_closed_per_week,
|
| 74 |
+
request.active_repos,
|
| 75 |
+
request.code_reviews_per_week
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
return ProductivityResponse(
|
| 79 |
+
predicted_score=prediction,
|
| 80 |
+
status="success"
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
except Exception as e:
|
| 84 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 85 |
+
|
| 86 |
+
@app.get("/health")
|
| 87 |
+
def health_check():
|
| 88 |
+
return {"status": "healthy", "model_loaded": True}
|
| 89 |
+
|
| 90 |
+
# Gradio interface for web UI
|
| 91 |
+
def predict_productivity_gradio(daily_coding_hours, commits_per_day, pull_requests_per_week,
|
| 92 |
+
issues_closed_per_week, active_repos, code_reviews_per_week):
|
| 93 |
+
"""
|
| 94 |
+
Gradio wrapper for the prediction function.
|
| 95 |
+
"""
|
| 96 |
+
try:
|
| 97 |
+
prediction = predict_productivity_core(
|
| 98 |
+
daily_coding_hours,
|
| 99 |
+
commits_per_day,
|
| 100 |
+
pull_requests_per_week,
|
| 101 |
+
issues_closed_per_week,
|
| 102 |
+
active_repos,
|
| 103 |
+
code_reviews_per_week
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
return f"Predicted Productivity Score: {prediction}"
|
| 107 |
+
|
| 108 |
+
except Exception as e:
|
| 109 |
+
return f"Error: {str(e)}"
|
| 110 |
+
|
| 111 |
+
# Create Gradio interface
|
| 112 |
+
iface = gr.Interface(
|
| 113 |
+
fn=predict_productivity_gradio,
|
| 114 |
+
inputs=[
|
| 115 |
+
gr.Slider(minimum=1, maximum=12, value=4.0, step=0.1, label="Daily Coding Hours"),
|
| 116 |
+
gr.Slider(minimum=0, maximum=20, value=5, step=1, label="Commits per Day"),
|
| 117 |
+
gr.Slider(minimum=0, maximum=15, value=4, step=1, label="Pull Requests per Week"),
|
| 118 |
+
gr.Slider(minimum=0, maximum=15, value=3, step=1, label="Issues Closed per Week"),
|
| 119 |
+
gr.Slider(minimum=1, maximum=20, value=5, step=1, label="Active Repositories"),
|
| 120 |
+
gr.Slider(minimum=0, maximum=15, value=3, step=1, label="Code Reviews per Week")
|
| 121 |
+
],
|
| 122 |
+
outputs=gr.Textbox(label="Prediction Result"),
|
| 123 |
+
title="🚀 Developer Productivity Predictor",
|
| 124 |
+
description="""
|
| 125 |
+
### Predict Developer Productivity Score
|
| 126 |
+
|
| 127 |
+
This model predicts developer productivity based on 6 key metrics:
|
| 128 |
+
- **Daily Coding Hours**: Time spent actively coding
|
| 129 |
+
- **Commits per Day**: Average daily code commits
|
| 130 |
+
- **Pull Requests per Week**: Weekly pull requests created
|
| 131 |
+
- **Issues Closed per Week**: Weekly issues resolved
|
| 132 |
+
- **Active Repositories**: Number of repositories worked on
|
| 133 |
+
- **Code Reviews per Week**: Weekly code reviews performed
|
| 134 |
+
|
| 135 |
+
**API Endpoint**: Use `/predict` with POST request for programmatic access.
|
| 136 |
+
""",
|
| 137 |
+
examples=[
|
| 138 |
+
[4.0, 5, 4, 3, 5, 3], # Your specified values
|
| 139 |
+
[6.0, 10, 8, 6, 8, 5], # High productivity example
|
| 140 |
+
[3.0, 2, 2, 1, 2, 1], # Beginner example
|
| 141 |
+
],
|
| 142 |
+
theme=gr.themes.Soft()
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
# Mount Gradio app with FastAPI
|
| 146 |
+
app = gr.mount_gradio_app(app, iface, path="/")
|
| 147 |
+
|
| 148 |
+
if __name__ == "__main__":
|
| 149 |
+
port = int(os.environ.get("PORT", 7860))
|
| 150 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|