File size: 1,117 Bytes
fc5e76c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import gradio as gr
from transformers import pipeline
from PIL import Image

# Load the pipeline
pipe = pipeline(
    "image-classification",
    model="mariamhsein16/FacialExpressionDetection"
)

# Prediction function
def predict_expression(image):
    if image is None:
        return "Please upload an image."

    results = pipe(image)

    # Format results nicely
    formatted_results = []
    for r in results:
        label = r["label"]
        score = round(r["score"] * 100, 2)
        formatted_results.append(f"{label}: {score}%")

    return "\n".join(formatted_results)

# Gradio UI
with gr.Blocks(title="Facial Expression Detection") as demo:
    gr.Markdown("## 😊 Facial Expression Detection")
    gr.Markdown("Upload a face image to detect the facial expression.")

    with gr.Row():
        image_input = gr.Image(type="pil", label="Upload Image")
        output_text = gr.Textbox(label="Predictions")

    submit_btn = gr.Button("Detect Expression")

    submit_btn.click(
        fn=predict_expression,
        inputs=image_input,
        outputs=output_text
    )

# Launch app
demo.launch()