Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
# Load the TensorFlow Lite model
|
| 8 |
+
interpreter = tf.lite.Interpreter(model_path="quote_model.tflite") # Use your model file name
|
| 9 |
+
interpreter.allocate_tensors()
|
| 10 |
+
|
| 11 |
+
# Get input & output details
|
| 12 |
+
input_details = interpreter.get_input_details()
|
| 13 |
+
output_details = interpreter.get_output_details()
|
| 14 |
+
|
| 15 |
+
@app.route("/predict", methods=["POST"])
|
| 16 |
+
def predict():
|
| 17 |
+
try:
|
| 18 |
+
data = request.json # Get input JSON
|
| 19 |
+
embedding = np.array([data["embedding"]], dtype=np.float32) # Convert to tensor
|
| 20 |
+
|
| 21 |
+
# Run inference
|
| 22 |
+
interpreter.set_tensor(input_details[0]['index'], embedding)
|
| 23 |
+
interpreter.invoke()
|
| 24 |
+
output_data = interpreter.get_tensor(output_details[0]['index'])
|
| 25 |
+
|
| 26 |
+
return jsonify({"predictions": output_data.tolist()})
|
| 27 |
+
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return jsonify({"error": str(e)})
|
| 30 |
+
|
| 31 |
+
# Run the Flask server
|
| 32 |
+
if __name__ == "__main__":
|
| 33 |
+
app.run(host="0.0.0.0", port=7860)
|