|
from flask import Flask, request, jsonify |
|
import numpy as np |
|
import tensorflow as tf |
|
import torch |
|
from transformers import pipeline |
|
import cv2 |
|
import os |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
|
|
tf_model = tf.keras.models.load_model('path_to_your_tf_model') |
|
|
|
|
|
torch_model = torch.load('path_to_your_torch_model') |
|
torch_model.eval() |
|
|
|
|
|
text_gen_pipeline = pipeline("text-generation", model="gpt2") |
|
|
|
@app.route('/') |
|
def index(): |
|
return "Welcome to the AI app! Endpoints are ready." |
|
|
|
|
|
@app.route('/predict_tf', methods=['POST']) |
|
def predict_tf(): |
|
data = request.json |
|
input_data = np.array(data['input']) |
|
prediction = tf_model.predict(input_data) |
|
return jsonify({"prediction": prediction.tolist()}) |
|
|
|
|
|
@app.route('/predict_torch', methods=['POST']) |
|
def predict_torch(): |
|
data = request.json |
|
input_data = torch.tensor(data['input']) |
|
prediction = torch_model(input_data) |
|
return jsonify({"prediction": prediction.detach().numpy().tolist()}) |
|
|
|
|
|
@app.route('/generate_text', methods=['POST']) |
|
def generate_text(): |
|
data = request.json |
|
prompt = data['prompt'] |
|
result = text_gen_pipeline(prompt, max_length=100, num_return_sequences=1) |
|
return jsonify({"generated_text": result[0]['generated_text']}) |
|
|
|
|
|
@app.route('/process_image', methods=['POST']) |
|
def process_image(): |
|
if 'file' not in request.files: |
|
return "No file found", 400 |
|
file = request.files['file'] |
|
img = cv2.imdecode(np.frombuffer(file.read(), np.uint8), cv2.IMREAD_COLOR) |
|
|
|
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) |
|
|
|
processed_path = 'processed_image.jpg' |
|
cv2.imwrite(processed_path, gray_img) |
|
return jsonify({"message": "Image processed", "file_path": processed_path}) |
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
app.run(host='0.0.0.0', port=5000, debug=True) |
|
import os |
|
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0" |
|
import tensorflow as tf |
|
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU'))) |
|
import os |
|
import tensorflow as tf |
|
|
|
|
|
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0" |
|
|
|
|
|
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU'))) |
|
|
|
|
|
def main(): |
|
|
|
pass |
|
|
|
if __name__ == "__main__": |
|
main() |