Spaces:
Runtime error
Runtime error
Ivan Shelonik
commited on
Commit
·
be16e3e
1
Parent(s):
543f7b1
add: html,css,js
Browse files- api_server.py +25 -6
- static/script.js +30 -0
- static/style.css +45 -0
- templates/index.html +77 -0
api_server.py
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
import numpy as np
|
|
|
|
|
|
|
| 4 |
|
| 5 |
from pathlib import Path
|
| 6 |
|
|
@@ -11,7 +18,7 @@ os.environ['TRANSFORMERS_CACHE'] = str(Path('./artifacts/').absolute())
|
|
| 11 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
| 12 |
|
| 13 |
from tensorflow import keras
|
| 14 |
-
from flask import Flask, jsonify, request
|
| 15 |
|
| 16 |
load_type = 'remote_hub_from_pretrained'
|
| 17 |
"""
|
|
@@ -37,12 +44,13 @@ elif load_type == 'remote_hub_from_pretrained':
|
|
| 37 |
model = from_pretrained_keras(REPO_ID, cache_dir='./artifacts/')
|
| 38 |
elif load_type == 'remote_hub_pipeline':
|
| 39 |
from transformers import pipeline
|
| 40 |
-
|
| 41 |
else:
|
| 42 |
-
|
| 43 |
|
| 44 |
# Initialize the Flask application
|
| 45 |
app = Flask(__name__)
|
|
|
|
| 46 |
|
| 47 |
|
| 48 |
# API route for prediction
|
|
@@ -63,10 +71,20 @@ def predict():
|
|
| 63 |
(Measures time only for ML operations preprocessing with predict)
|
| 64 |
}
|
| 65 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
start_time = time.time()
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# Get the image data from the request
|
| 69 |
-
image_data = request.get_json()['image']
|
| 70 |
|
| 71 |
# Preprocess the image
|
| 72 |
processed_image = preprocess_image(image_data)
|
|
@@ -129,12 +147,13 @@ def version():
|
|
| 129 |
|
| 130 |
@app.route("/")
|
| 131 |
def hello_world():
|
| 132 |
-
return "
|
|
|
|
| 133 |
|
| 134 |
|
| 135 |
# Start the Flask application
|
| 136 |
if __name__ == '__main__':
|
| 137 |
-
app.run()
|
| 138 |
|
| 139 |
|
| 140 |
##################
|
|
|
|
| 1 |
+
"""
|
| 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 |
|
|
|
|
| 18 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
| 19 |
|
| 20 |
from tensorflow import keras
|
| 21 |
+
from flask import Flask, jsonify, request, render_template
|
| 22 |
|
| 23 |
load_type = 'remote_hub_from_pretrained'
|
| 24 |
"""
|
|
|
|
| 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
|
|
|
|
| 71 |
(Measures time only for ML operations preprocessing with predict)
|
| 72 |
}
|
| 73 |
"""
|
| 74 |
+
if 'image' not in request.files:
|
| 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']
|
| 82 |
+
|
| 83 |
+
# Get pixels out of file
|
| 84 |
+
image_data = Image.open(file)
|
| 85 |
+
|
| 86 |
# Get the image data from the request
|
| 87 |
+
# image_data = request.get_json()['image']
|
| 88 |
|
| 89 |
# Preprocess the image
|
| 90 |
processed_image = preprocess_image(image_data)
|
|
|
|
| 147 |
|
| 148 |
@app.route("/")
|
| 149 |
def hello_world():
|
| 150 |
+
return render_template("index.html")
|
| 151 |
+
# return "<p>Hello, Team!</p>"
|
| 152 |
|
| 153 |
|
| 154 |
# Start the Flask application
|
| 155 |
if __name__ == '__main__':
|
| 156 |
+
app.run(debug=True)
|
| 157 |
|
| 158 |
|
| 159 |
##################
|
static/script.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//const textGenForm = document.querySelector('.text-gen-form');
|
| 2 |
+
//
|
| 3 |
+
//const translateText = async (text) => {
|
| 4 |
+
// const inferResponse = await fetch(`infer_t5?input=${text}`);
|
| 5 |
+
// const inferJson = await inferResponse.json();
|
| 6 |
+
//
|
| 7 |
+
// return inferJson.output;
|
| 8 |
+
//};
|
| 9 |
+
//
|
| 10 |
+
//textGenForm.addEventListener('submit', async (event) => {
|
| 11 |
+
// event.preventDefault();
|
| 12 |
+
//
|
| 13 |
+
// const textGenInput = document.getElementById('text-gen-input');
|
| 14 |
+
// const textGenParagraph = document.querySelector('.text-gen-output');
|
| 15 |
+
//
|
| 16 |
+
// try {
|
| 17 |
+
// textGenParagraph.textContent = await translateText(textGenInput.value);
|
| 18 |
+
// } catch (err) {
|
| 19 |
+
// console.error(err);
|
| 20 |
+
// }
|
| 21 |
+
//});
|
| 22 |
+
|
| 23 |
+
document.addEventListener("DOMContentLoaded", () => {
|
| 24 |
+
const uploadForm = document.querySelector(".image-upload-form");
|
| 25 |
+
const uploadButton = document.querySelector("#image-upload-submit");
|
| 26 |
+
|
| 27 |
+
uploadButton.addEventListener("click", () => {
|
| 28 |
+
uploadForm.submit();
|
| 29 |
+
});
|
| 30 |
+
});
|
static/style.css
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
body {
|
| 2 |
+
--text: hsl(0 0% 15%);
|
| 3 |
+
padding: 2.5rem;
|
| 4 |
+
font-family: sans-serif;
|
| 5 |
+
color: var(--text);
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
body.dark-theme {
|
| 9 |
+
--text: hsl(0 0% 90%);
|
| 10 |
+
background-color: hsl(223 39% 7%);
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
main {
|
| 14 |
+
max-width: 80rem;
|
| 15 |
+
text-align: center;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
section {
|
| 19 |
+
display: flex;
|
| 20 |
+
flex-direction: column;
|
| 21 |
+
align-items: center;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
a {
|
| 25 |
+
color: var(--text);
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
form {
|
| 29 |
+
width: 30rem;
|
| 30 |
+
margin: 0 auto;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
input {
|
| 34 |
+
width: 100%;
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
button {
|
| 38 |
+
cursor: pointer;
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
.text-gen-output {
|
| 42 |
+
min-height: 1.2rem;
|
| 43 |
+
margin: 1rem;
|
| 44 |
+
border: 0.5px solid grey;
|
| 45 |
+
}
|
templates/index.html
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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>
|
| 46 |
+
<meta charset="UTF-8" />
|
| 47 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 48 |
+
<title>Flask API</title>
|
| 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>Text generation using Flan T5</h1>
|
| 60 |
+
<p>
|
| 61 |
+
Model:
|
| 62 |
+
<a
|
| 63 |
+
href="https://huggingface.co/1vash/mnist_demo_model"
|
| 64 |
+
rel="noreferrer"
|
| 65 |
+
target="_blank"
|
| 66 |
+
>1vash/mnist_demo_model</a
|
| 67 |
+
>
|
| 68 |
+
</p>
|
| 69 |
+
<form class="image-upload-form" action="/predict" method="POST" enctype="multipart/form-data">
|
| 70 |
+
<label for="image-upload-input">Upload an image:</label>
|
| 71 |
+
<input id="image-upload-input" type="file" accept="image/*" name="image" />
|
| 72 |
+
<button id="image-upload-submit">Upload</button>
|
| 73 |
+
</form>
|
| 74 |
+
</section>
|
| 75 |
+
</main>
|
| 76 |
+
</body>
|
| 77 |
+
</html>
|