Download app.py from Rudrockz/Moodmate: direct link, hf CLI and curl.
- Browser
- Download file 1.26 kB
-
https://huggingface.co/Rudrockz/Moodmate/resolve/main/app.py
- Command line
-
hf download hf://Rudrockz/Moodmate/app.py
-
curl -L -o app.py https://huggingface.co/Rudrockz/Moodmate/resolve/main/app.py
1.26 kB
| import gradio as gr | |
| import tensorflow as tf | |
| import numpy as np | |
| from PIL import Image | |
| # Load model | |
| interpreter = tf.lite.Interpreter(model_path="model.tflite") | |
| interpreter.allocate_tensors() | |
| # Get input and output details | |
| input_details = interpreter.get_input_details() | |
| output_details = interpreter.get_output_details() | |
| # Load labels | |
| with open("labels.txt", "r") as f: | |
| labels = [line.strip() for line in f.readlines()] | |
| def predict(image): | |
| # Preprocess image | |
| image = image.resize((224, 224)) # adjust size if needed | |
| image_array = np.array(image, dtype=np.float32) / 255.0 | |
| image_array = np.expand_dims(image_array, axis=0) | |
| # Run inference | |
| interpreter.set_tensor(input_details[0]['index'], image_array) | |
| interpreter.invoke() | |
| predictions = interpreter.get_tensor(output_details[0]['index'])[0] | |
| # Map predictions to labels | |
| results = {labels[i]: float(predictions[i]) for i in range(len(labels))} | |
| return results | |
| # Gradio interface | |
| 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() | |