|
|
import gradio as gr
|
|
|
import tensorflow as tf
|
|
|
import numpy as np
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
|
interpreter = tf.lite.Interpreter(model_path="model.tflite")
|
|
|
interpreter.allocate_tensors()
|
|
|
|
|
|
|
|
|
input_details = interpreter.get_input_details()
|
|
|
output_details = interpreter.get_output_details()
|
|
|
|
|
|
|
|
|
with open("labels.txt", "r") as f:
|
|
|
labels = [line.strip() for line in f.readlines()]
|
|
|
|
|
|
def predict(image):
|
|
|
|
|
|
image = image.resize((224, 224))
|
|
|
image_array = np.array(image, dtype=np.float32) / 255.0
|
|
|
image_array = np.expand_dims(image_array, axis=0)
|
|
|
|
|
|
|
|
|
interpreter.set_tensor(input_details[0]['index'], image_array)
|
|
|
interpreter.invoke()
|
|
|
predictions = interpreter.get_tensor(output_details[0]['index'])[0]
|
|
|
|
|
|
|
|
|
results = {labels[i]: float(predictions[i]) for i in range(len(labels))}
|
|
|
return results
|
|
|
|
|
|
|
|
|
demo = gr.Interface(
|
|
|
fn=predict,
|
|
|
inputs=gr.Image(type="pil"),
|
|
|
outputs=gr.Label(num_top_classes=3),
|
|
|
title="Moodmate",
|
|
|
description="Upload an image to detect the mood."
|
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
demo.launch()
|
|
|
|