File size: 1,482 Bytes
95f1720
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
import requests
import os

# ==========================
# 1. Load your trained model
# ==========================
MODEL_PATH = "model1.h5"
if not os.path.exists(MODEL_PATH):
    print("Downloading model from GitHub...")
    url = "https://media.githubusercontent.com/media/Sedtha-019/face-recognition-model/main/model1.h5"
    r = requests.get(url)
    r.raise_for_status()
    with open(MODEL_PATH, "wb") as f:
        f.write(r.content)
    print("✅ Model downloaded successfully!")

model = tf.keras.models.load_model(MODEL_PATH)
print("✅ Model loaded successfully!")

class_names = ["Sed", "Rak"]

def preprocess_image(image):
    image = image.resize((300, 300))
    img_array = np.array(image) / 255.0
    img_array = np.expand_dims(img_array, axis=0)
    return img_array

def predict(image):
    if image is None:
        return {"Error": 1.0}
    
    img_array = preprocess_image(image)
    prediction = model.predict(img_array)
    
    return {class_names[i]: float(prediction[0][i]) for i in range(len(class_names))}

iface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil", label="Upload a Face Image"),
    outputs=gr.Label(num_top_classes=2, label="Prediction"),
    title="🧠 Face Recognition (Sedtha Model)",
    description="Upload a face image — the model predicts who it is.",
    theme="default"
)

# For Hugging Face, don't use share=True
iface.launch()