Spaces:
Sleeping
Sleeping
requirements.txt
Browse filesgradio
transformers
datasets
Pillow
torch
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForImageClassification, AutoProcessor, pipeline
|
3 |
+
from datasets import load_dataset
|
4 |
+
from PIL import Image
|
5 |
+
import torch
|
6 |
+
|
7 |
+
# Load the model and processor from Hugging Face
|
8 |
+
model_name = "Deepri24/my_awesome_emotion_identifier_model"
|
9 |
+
processor = AutoProcessor.from_pretrained(model_name)
|
10 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
11 |
+
|
12 |
+
# Instantiate a pipeline for image classification
|
13 |
+
classifier = pipeline("image-classification", model=model_name)
|
14 |
+
|
15 |
+
def predict(image):
|
16 |
+
# Use the classifier pipeline to get predictions
|
17 |
+
results = classifier(image)
|
18 |
+
|
19 |
+
# Extract the label from the results
|
20 |
+
predicted_label = results[0]['label'] # Get the top prediction
|
21 |
+
|
22 |
+
return predicted_label
|
23 |
+
|
24 |
+
# Load the validation split of the dataset but only the first 10 samples
|
25 |
+
ds = load_dataset('FastJobs/Visual_Emotional_Analysis', split="train[:10]")
|
26 |
+
|
27 |
+
# Define a function to get sample images
|
28 |
+
def get_samples():
|
29 |
+
# Load two sample images from the dataset
|
30 |
+
sample_images = [ds["image"][i] for i in [0, 1]] # Get the first two images
|
31 |
+
return sample_images
|
32 |
+
|
33 |
+
# Create Gradio interface
|
34 |
+
interface = gr.Interface(
|
35 |
+
fn=predict,
|
36 |
+
inputs=gr.Image(type="pil"), # Accept PIL images
|
37 |
+
outputs="text", # Output will be a text label
|
38 |
+
title="Emotion Identifier",
|
39 |
+
description="Upload an image to identify the emotion.",
|
40 |
+
examples=get_samples() # Use sample images for example inputs
|
41 |
+
)
|
42 |
+
|
43 |
+
# Launch the interface
|
44 |
+
interface.launch()
|