abinivas8 commited on
Commit
1334733
·
verified ·
1 Parent(s): 9eca786

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +150 -185
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
- # Load the trained model and scaler
8
- model = joblib.load('developer_productivity_model.joblib')
9
- scaler = joblib.load('scaler.joblib')
10
-
11
- def predict_productivity(daily_coding_hours, commits_per_day, pull_requests_per_week,
12
- issues_closed_per_week, active_repos, code_reviews_per_week):
13
- """
14
- Predict developer productivity score based on numeric features.
15
-
16
- Parameters:
17
- - daily_coding_hours: float (hours spent coding per day)
18
- - commits_per_day: int (number of commits per day)
19
- - pull_requests_per_week: int (number of pull requests per week)
20
- - issues_closed_per_week: int (number of issues closed per week)
21
- - active_repos: int (number of active repositories)
22
- - code_reviews_per_week: int (number of code reviews per week)
23
-
24
- Returns:
25
- - predicted_score: float (predicted productivity score)
26
- """
27
-
28
- try:
29
- # Create feature array
30
- features = np.array([[
31
- daily_coding_hours,
32
- commits_per_day,
33
- pull_requests_per_week,
34
- issues_closed_per_week,
35
- active_repos,
36
- code_reviews_per_week
37
- ]])
38
-
39
- # Scale features
40
- features_scaled = scaler.transform(features)
41
-
42
- # Make prediction
43
- prediction = model.predict(features_scaled)[0]
44
-
45
- score = round(prediction, 2)
46
-
47
- # Add interpretation
48
- if score >= 80:
49
- interpretation = "🌟 Excellent - High productivity developer!"
50
- elif score >= 70:
51
- interpretation = "✅ Very Good - Above average productivity!"
52
- elif score >= 60:
53
- interpretation = "👍 Good - Solid productivity level!"
54
- elif score >= 50:
55
- interpretation = "⚖️ Average - Room for improvement!"
56
- else:
57
- interpretation = "📈 Below Average - Consider focusing on key metrics!"
58
-
59
- return f"{score} - {interpretation}"
60
-
61
- except Exception as e:
62
- return f"Error in prediction: {str(e)}"
63
-
64
- # Create Gradio interface
65
- with gr.Blocks(title="GitHub Developer Productivity Predictor", theme=gr.themes.Soft()) as demo:
66
- gr.Markdown(
67
- """
68
- # 🚀 GitHub Developer Productivity Predictor
69
-
70
- This AI model predicts developer productivity scores based on GitHub activity metrics.
71
- Enter your development metrics below to get your predicted productivity score!
72
-
73
- **Features used for prediction:**
74
- - Daily coding hours
75
- - Commits per day
76
- - Pull requests per week
77
- - Issues closed per week
78
- - Active repositories
79
- - Code reviews per week
80
- """
81
- )
82
-
83
- with gr.Row():
84
- with gr.Column():
85
- daily_hours = gr.Number(
86
- label="Daily Coding Hours",
87
- value=4.0,
88
- minimum=0,
89
- maximum=24,
90
- info="Average hours spent coding per day"
91
- )
92
- commits_day = gr.Number(
93
- label="Commits Per Day",
94
- value=5,
95
- minimum=0,
96
- maximum=50,
97
- info="Average number of commits made per day"
98
- )
99
- prs_week = gr.Number(
100
- label="Pull Requests Per Week",
101
- value=4,
102
- minimum=0,
103
- maximum=20,
104
- info="Average number of pull requests created per week"
105
- )
106
-
107
- with gr.Column():
108
- issues_week = gr.Number(
109
- label="Issues Closed Per Week",
110
- value=3,
111
- minimum=0,
112
- maximum=20,
113
- info="Average number of issues resolved per week"
114
- )
115
- repos = gr.Number(
116
- label="Active Repositories",
117
- value=5,
118
- minimum=0,
119
- maximum=50,
120
- info="Number of repositories actively contributed to"
121
- )
122
- reviews_week = gr.Number(
123
- label="Code Reviews Per Week",
124
- value=3,
125
- minimum=0,
126
- maximum=20,
127
- info="Average number of code reviews performed per week"
128
- )
129
-
130
- with gr.Row():
131
- predict_btn = gr.Button("🔮 Predict Productivity Score", variant="primary", size="lg")
132
- clear_btn = gr.Button("🔄 Clear", variant="secondary")
133
-
134
- output = gr.Textbox(
135
- label="Productivity Prediction",
136
- placeholder="Click 'Predict Productivity Score' to see your result...",
137
- interactive=False
138
- )
139
-
140
- # Example buttons
141
- gr.Markdown("### 📊 Try these examples:")
142
- with gr.Row():
143
- high_prod_btn = gr.Button("🌟 High Productivity Developer")
144
- avg_prod_btn = gr.Button("⚖️ Average Developer")
145
- beginner_btn = gr.Button("🌱 Beginner Developer")
146
-
147
- # Event handlers
148
- predict_btn.click(
149
- fn=predict_productivity,
150
- inputs=[daily_hours, commits_day, prs_week, issues_week, repos, reviews_week],
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)