Spaces:
Sleeping
Sleeping
SakibRumu
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from fastai.vision.all import *
|
3 |
+
|
4 |
+
# Load your trained model (replace with your model path)
|
5 |
+
model_path = "path_to_your_trained_model.pkl"
|
6 |
+
model = load_learner(model_path)
|
7 |
+
|
8 |
+
# Emotion prediction function with percentage
|
9 |
+
def predict_emotion(image):
|
10 |
+
# Perform prediction using your trained model
|
11 |
+
pred_class, pred_idx, outputs = model.predict(image)
|
12 |
+
|
13 |
+
# Get the probability (confidence) of the prediction
|
14 |
+
confidence = outputs[pred_idx] * 100 # Convert to percentage
|
15 |
+
|
16 |
+
return f"Predicted Emotion: {pred_class}", f"Confidence: {confidence:.2f}%"
|
17 |
+
|
18 |
+
# Create the Gradio interface
|
19 |
+
with gr.Blocks(theme='gstaff/xkcd') as demo:
|
20 |
+
gr.Markdown("# Emotion Recognition Classifier")
|
21 |
+
|
22 |
+
with gr.Row():
|
23 |
+
with gr.Column():
|
24 |
+
# Image input widget
|
25 |
+
img_input = gr.Image(type="pil", label="Upload an image")
|
26 |
+
|
27 |
+
# Text output widgets for emotion prediction and confidence percentage
|
28 |
+
label_output = gr.Textbox(label="Predicted Emotion")
|
29 |
+
confidence_output = gr.Textbox(label="Confidence Percentage")
|
30 |
+
|
31 |
+
# Button to trigger emotion classification
|
32 |
+
img_input.upload(predict_emotion, img_input, [label_output, confidence_output])
|
33 |
+
|
34 |
+
# Run the app
|
35 |
+
demo.launch(share=True)
|