File size: 1,872 Bytes
9e97c11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21ef3d4
 
 
9e97c11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- coding: utf-8 -*-
"""threadcheckerv1_gui.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/145qYaJaBGKmbGoSNYSsFlMASN1PUKEgF
"""

import numpy as np
from PIL import Image
import gradio as gr
import huggingface_hub
from tensorflow.keras.models import load_model

# Load model from Hugging Face Hub
repo_id = "ddecosmo/thread_checker_v1"
model_filename = "thread_checker_model.keras"
model_path = huggingface_hub.hf_hub_download(repo_id=repo_id, filename=model_filename)
model = load_model(model_path)

# Example images (replace with actual files in your repo if desired)
example_images = [
    "0.125_ex.jpg",
    "0.25_ex.jpg",
    "0.375_ex.jpg"
]

def predict_image(image):
    """
    Predicts the class of an image using the loaded Keras model and returns
    confidence scores for all classes and the final determination.
    """
    img_width, img_height = model.input_shape[1:3]
    image = image.resize((img_width, img_height))
    image = np.array(image).astype("float32") / 255.0
    image = np.expand_dims(image, axis=0)

    predictions = model.predict(image)
    confidence_scores = predictions[0]

    predicted_class_index = np.argmax(confidence_scores)
    class_labels = ["0.125", "0.25", "0.375"]
    final_determination = class_labels[predicted_class_index]

    return (
        float(confidence_scores[0]),
        float(confidence_scores[1]),
        float(confidence_scores[2]),
        final_determination,
    )

iface = gr.Interface(
    fn=predict_image,
    inputs=gr.Image(type="pil"),
    outputs=[
        gr.Number(label="Confidence (0.125)"),
        gr.Number(label="Confidence (0.25)"),
        gr.Number(label="Confidence (0.375)"),
        gr.Textbox(label="Final Determination"),
    ],
    examples=example_images,
)

if __name__ == "__main__":
    iface.launch()