Skip to content

Commit 98f9176

Browse files
committed
Final API
0 parents  commit 98f9176

4 files changed

Lines changed: 213 additions & 0 deletions

File tree

.gitignore

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
27+
# PyInstaller
28+
# Usually these files are written by a python script from a template
29+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
30+
*.manifest
31+
*.spec
32+
33+
# Installer logs
34+
pip-log.txt
35+
pip-delete-this-directory.txt
36+
37+
# Unit test / coverage reports
38+
htmlcov/
39+
.tox/
40+
.coverage
41+
.coverage.*
42+
.cache
43+
nosetests.xml
44+
coverage.xml
45+
*.cover
46+
.hypothesis/
47+
.pytest_cache/
48+
49+
# Translations
50+
*.mo
51+
*.pot
52+
53+
# Django stuff:
54+
*.log
55+
local_settings.py
56+
db.sqlite3
57+
db.sqlite3-journal
58+
59+
# Flask stuff:
60+
instance/
61+
.webassets-cache
62+
63+
# Scrapy stuff:
64+
.scrapy
65+
66+
# Sphinx documentation
67+
docs/_build/
68+
69+
# PyBuilder
70+
target/
71+
72+
# Jupyter Notebook
73+
.ipynb_checkpoints
74+
75+
# IPython
76+
profile_default/
77+
ipython_config.py
78+
79+
# pyenv
80+
.python-version
81+
82+
# pipenv
83+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
84+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
85+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
86+
# install all needed dependencies.
87+
#Pipfile.lock
88+
89+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
90+
__pypackages__/
91+
92+
# Celery stuff
93+
celerybeat-schedule
94+
celerybeat.pid
95+
96+
# SageMath parsed files
97+
*.sage.py
98+
99+
# Environments
100+
.env
101+
.venv
102+
env/
103+
venv/
104+
ENV/
105+
env.bak/
106+
venv.bak/
107+
108+
# Spyder project settings
109+
.spyderproject
110+
.spyproject
111+
112+
# Rope project settings
113+
.ropeproject
114+
115+
# mkdocs documentation
116+
/site
117+
118+
# mypy
119+
.mypy_cache/
120+
.dmypy.json
121+
dmypy.json
122+
123+
# Pyre type checker
124+
.pyre/
125+
126+
# pytype static type analyzer
127+
.pytype/
128+
129+
# Cython debug symbols
130+
cython_debug/
131+
132+
# Ignore VSCode settings
133+
.vscode/
134+
135+
model.pkl
136+
.DS_Store

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# GreenGridIn Flask API
2+
3+
### API
4+
5+
- `POST /predict` - Predict the power consumption of a server based on the input data.
6+
7+
- Request Body:
8+
9+
```json
10+
{
11+
"air_temperature": 10.926,
12+
"pressure": 0.979103,
13+
"wind_speed": 9.014
14+
}
15+
```
16+
17+
- Response:
18+
19+
```json
20+
{
21+
"power": 33.6881
22+
}
23+
```

app.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/python
2+
3+
import os
4+
import requests
5+
import numpy as np
6+
from flask import Flask, request, jsonify
7+
from flask_cors import CORS
8+
import pickle
9+
10+
app = Flask(__name__)
11+
CORS(app)
12+
13+
MODEL_FILEPATH = "model.pkl"
14+
15+
if not os.path.exists(MODEL_FILEPATH):
16+
url = ""
17+
r = requests.get(url)
18+
with open(MODEL_FILEPATH, "wb") as f:
19+
f.write(r.content)
20+
21+
22+
with open(MODEL_FILEPATH, "rb") as f:
23+
model = pickle.load(f)
24+
25+
26+
@app.route("/", methods=["GET"])
27+
def default():
28+
return jsonify({"message": "Welcome to the API"})
29+
30+
31+
@app.route("/predict", methods=["POST"])
32+
def predict():
33+
req_data = ["air_temperature", "pressure", "wind_speed"]
34+
35+
if not request.json:
36+
return jsonify({"error": "Request data not found"})
37+
38+
if not all(k in request.json for k in req_data):
39+
return jsonify({"error": "Missing required fields"})
40+
41+
data = [request.json[k] for k in req_data]
42+
data = np.array(data).reshape(1, -1)
43+
prediction = model.predict(data)[0]
44+
45+
return jsonify({"power": prediction})
46+
47+
48+
if __name__ == "__main__":
49+
app.run(debug=True, port=5000)

requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Flask
2+
Flask-Cors
3+
Gunicorn
4+
scikit-learn==1.2.2
5+
requests

0 commit comments

Comments
 (0)