Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
# Load the pipeline
|
| 6 |
+
pipe = pipeline(
|
| 7 |
+
"image-classification",
|
| 8 |
+
model="mariamhsein16/FacialExpressionDetection"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
# Prediction function
|
| 12 |
+
def predict_expression(image):
|
| 13 |
+
if image is None:
|
| 14 |
+
return "Please upload an image."
|
| 15 |
+
|
| 16 |
+
results = pipe(image)
|
| 17 |
+
|
| 18 |
+
# Format results nicely
|
| 19 |
+
formatted_results = []
|
| 20 |
+
for r in results:
|
| 21 |
+
label = r["label"]
|
| 22 |
+
score = round(r["score"] * 100, 2)
|
| 23 |
+
formatted_results.append(f"{label}: {score}%")
|
| 24 |
+
|
| 25 |
+
return "\n".join(formatted_results)
|
| 26 |
+
|
| 27 |
+
# Gradio UI
|
| 28 |
+
with gr.Blocks(title="Facial Expression Detection") as demo:
|
| 29 |
+
gr.Markdown("## 😊 Facial Expression Detection")
|
| 30 |
+
gr.Markdown("Upload a face image to detect the facial expression.")
|
| 31 |
+
|
| 32 |
+
with gr.Row():
|
| 33 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
| 34 |
+
output_text = gr.Textbox(label="Predictions")
|
| 35 |
+
|
| 36 |
+
submit_btn = gr.Button("Detect Expression")
|
| 37 |
+
|
| 38 |
+
submit_btn.click(
|
| 39 |
+
fn=predict_expression,
|
| 40 |
+
inputs=image_input,
|
| 41 |
+
outputs=output_text
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# Launch app
|
| 45 |
+
demo.launch()
|