Spaces:
Runtime error
Runtime error
Ivan Shelonik
commited on
Commit
·
696f1ca
1
Parent(s):
be16e3e
upd: refactored
Browse files- api_server.py +3 -9
- templates/index.html +1 -46
api_server.py
CHANGED
@@ -2,18 +2,13 @@
|
|
2 |
official fastapi HF example https://huggingface.co/docs/hub/spaces-sdks-docker-examples#docker-spaces-examples
|
3 |
"""
|
4 |
|
5 |
-
|
6 |
import os
|
7 |
import time
|
8 |
import numpy as np
|
9 |
from PIL import Image
|
10 |
|
11 |
-
|
12 |
from pathlib import Path
|
13 |
|
14 |
-
os.environ['TRANSFORMERS_CACHE'] = str(Path('./artifacts/').absolute())
|
15 |
-
|
16 |
-
|
17 |
# Disable tensorflow warnings
|
18 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
19 |
|
@@ -29,7 +24,6 @@ remote_hub_pipeline; - needs config.json and this is not easy to grasp how to do
|
|
29 |
https://discuss.huggingface.co/t/how-to-create-a-config-json-after-saving-a-model/10459/4
|
30 |
"""
|
31 |
|
32 |
-
|
33 |
REPO_ID = "1vash/mnist_demo_model"
|
34 |
|
35 |
# Load the saved model into memory
|
@@ -37,20 +31,22 @@ if load_type == 'local':
|
|
37 |
model = keras.models.load_model('artifacts/models/mnist_model.h5')
|
38 |
elif load_type == 'remote_hub_download':
|
39 |
from huggingface_hub import hf_hub_download
|
|
|
40 |
model = keras.models.load_model(hf_hub_download(repo_id=REPO_ID, filename="saved_model.pb"))
|
41 |
elif load_type == 'remote_hub_from_pretrained':
|
42 |
# https://huggingface.co/docs/hub/keras
|
|
|
43 |
from huggingface_hub import from_pretrained_keras
|
44 |
model = from_pretrained_keras(REPO_ID, cache_dir='./artifacts/')
|
45 |
elif load_type == 'remote_hub_pipeline':
|
46 |
from transformers import pipeline
|
|
|
47 |
model = pipeline("image-classification", model=REPO_ID)
|
48 |
else:
|
49 |
raise AssertionError('No load type is specified!')
|
50 |
|
51 |
# Initialize the Flask application
|
52 |
app = Flask(__name__)
|
53 |
-
# app = FastAPI()
|
54 |
|
55 |
|
56 |
# API route for prediction
|
@@ -75,7 +71,6 @@ def predict():
|
|
75 |
# Handle if no file is selected
|
76 |
return 'No file selected'
|
77 |
|
78 |
-
print('PRINT ME HERE', request)
|
79 |
start_time = time.time()
|
80 |
|
81 |
file = request.files['image']
|
@@ -155,7 +150,6 @@ def hello_world():
|
|
155 |
if __name__ == '__main__':
|
156 |
app.run(debug=True)
|
157 |
|
158 |
-
|
159 |
##################
|
160 |
# Flask API usages:
|
161 |
# 1. Just a wrapper over OpenAI API
|
|
|
2 |
official fastapi HF example https://huggingface.co/docs/hub/spaces-sdks-docker-examples#docker-spaces-examples
|
3 |
"""
|
4 |
|
|
|
5 |
import os
|
6 |
import time
|
7 |
import numpy as np
|
8 |
from PIL import Image
|
9 |
|
|
|
10 |
from pathlib import Path
|
11 |
|
|
|
|
|
|
|
12 |
# Disable tensorflow warnings
|
13 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
14 |
|
|
|
24 |
https://discuss.huggingface.co/t/how-to-create-a-config-json-after-saving-a-model/10459/4
|
25 |
"""
|
26 |
|
|
|
27 |
REPO_ID = "1vash/mnist_demo_model"
|
28 |
|
29 |
# Load the saved model into memory
|
|
|
31 |
model = keras.models.load_model('artifacts/models/mnist_model.h5')
|
32 |
elif load_type == 'remote_hub_download':
|
33 |
from huggingface_hub import hf_hub_download
|
34 |
+
|
35 |
model = keras.models.load_model(hf_hub_download(repo_id=REPO_ID, filename="saved_model.pb"))
|
36 |
elif load_type == 'remote_hub_from_pretrained':
|
37 |
# https://huggingface.co/docs/hub/keras
|
38 |
+
os.environ['TRANSFORMERS_CACHE'] = str(Path('./artifacts/').absolute())
|
39 |
from huggingface_hub import from_pretrained_keras
|
40 |
model = from_pretrained_keras(REPO_ID, cache_dir='./artifacts/')
|
41 |
elif load_type == 'remote_hub_pipeline':
|
42 |
from transformers import pipeline
|
43 |
+
|
44 |
model = pipeline("image-classification", model=REPO_ID)
|
45 |
else:
|
46 |
raise AssertionError('No load type is specified!')
|
47 |
|
48 |
# Initialize the Flask application
|
49 |
app = Flask(__name__)
|
|
|
50 |
|
51 |
|
52 |
# API route for prediction
|
|
|
71 |
# Handle if no file is selected
|
72 |
return 'No file selected'
|
73 |
|
|
|
74 |
start_time = time.time()
|
75 |
|
76 |
file = request.files['image']
|
|
|
150 |
if __name__ == '__main__':
|
151 |
app.run(debug=True)
|
152 |
|
|
|
153 |
##################
|
154 |
# Flask API usages:
|
155 |
# 1. Just a wrapper over OpenAI API
|
templates/index.html
CHANGED
@@ -1,45 +1,3 @@
|
|
1 |
-
<!--<link rel="stylesheet" href="{{ url_for('static', filename='style.css', _external=True) }}" />-->
|
2 |
-
<!--<script type="module" src="{{ url_for('static', filename='script.js', _external=True) }}"></script>-->
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
<!--<!DOCTYPE html>-->
|
7 |
-
<!--<html lang="en">-->
|
8 |
-
<!-- <head>-->
|
9 |
-
<!-- <meta charset="UTF-8" />-->
|
10 |
-
<!-- <meta name="viewport" content="width=device-width, initial-scale=1.0" />-->
|
11 |
-
<!-- <title>Flask API</title>-->
|
12 |
-
<!-- <link rel="stylesheet" href="style.css" />-->
|
13 |
-
<!-- <script type="module" src="script.js"></script>-->
|
14 |
-
<!-- </head>-->
|
15 |
-
<!-- <body>-->
|
16 |
-
<!-- <main>-->
|
17 |
-
<!-- <section id="text-gen">-->
|
18 |
-
<!-- <h1>Text generation using Flan T5</h1>-->
|
19 |
-
<!-- <p>-->
|
20 |
-
<!-- Model:-->
|
21 |
-
<!-- <a-->
|
22 |
-
<!-- href="https://huggingface.co/1vash/mnist_demo_model"-->
|
23 |
-
<!-- rel="noreferrer"-->
|
24 |
-
<!-- target="_blank"-->
|
25 |
-
<!-- >1vash/mnist_demo_model</a-->
|
26 |
-
<!-- >-->
|
27 |
-
<!-- </p>-->
|
28 |
-
<!-- <form class="text-gen-form">-->
|
29 |
-
<!-- <label for="text-gen-input">Text prompt</label>-->
|
30 |
-
<!-- <input-->
|
31 |
-
<!-- id="text-gen-input"-->
|
32 |
-
<!-- type="text"-->
|
33 |
-
<!-- value="English: Translate There are many ducks. German:"-->
|
34 |
-
<!-- />-->
|
35 |
-
<!-- <button id="text-gen-submit">Submit</button>-->
|
36 |
-
<!-- <p class="text-gen-output"></p>-->
|
37 |
-
<!-- </form>-->
|
38 |
-
<!-- </section>-->
|
39 |
-
<!-- </main>-->
|
40 |
-
<!-- </body>-->
|
41 |
-
<!--</html>-->
|
42 |
-
|
43 |
<!DOCTYPE html>
|
44 |
<html lang="en">
|
45 |
<head>
|
@@ -49,14 +7,11 @@
|
|
49 |
<!-- 304 status codes indicate that the files are being cached by the browser. There is no error or issue to be concerned about in this case.-->
|
50 |
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" />
|
51 |
<script type="module" src="{{ url_for('static', filename='script.js') }}"></script>
|
52 |
-
|
53 |
-
<!-- <link rel="stylesheet" href="style.css" />-->
|
54 |
-
<!-- <script type="module" src="script.js"></script>-->
|
55 |
</head>
|
56 |
<body>
|
57 |
<main>
|
58 |
<section id="text-gen">
|
59 |
-
<h1
|
60 |
<p>
|
61 |
Model:
|
62 |
<a
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
<!DOCTYPE html>
|
2 |
<html lang="en">
|
3 |
<head>
|
|
|
7 |
<!-- 304 status codes indicate that the files are being cached by the browser. There is no error or issue to be concerned about in this case.-->
|
8 |
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" />
|
9 |
<script type="module" src="{{ url_for('static', filename='script.js') }}"></script>
|
|
|
|
|
|
|
10 |
</head>
|
11 |
<body>
|
12 |
<main>
|
13 |
<section id="text-gen">
|
14 |
+
<h1>🤗 Image Classification of MNIST digits 🤗</h1>
|
15 |
<p>
|
16 |
Model:
|
17 |
<a
|