YAML Metadata Warning:empty or missing yaml metadata in repo card
Check out the documentation for more information.
import gradio as gr import numpy as np import tensorflow as tf from tensorflow.keras.models import load_model from tensorflow.keras.preprocessing import image import requests from io import BytesIO
Download the model from Hugging Face
model_url = "https://huggingface.co/almohab/mobilenet_tb_model/resolve/main/mobilenet_tb_model.h5" model_path = "mobilenet_tb_model.h5"
Download the model if not already present
try: open(model_path) except: print("Downloading model...") response = requests.get(model_url) with open(model_path, "wb") as f: f.write(response.content)
Load the model
model = load_model(model_path) labels = ["Normal", "Tuberculosis"] # Adjust based on your classes
Define prediction function
def predict_tb(img): img = img.resize((224, 224)) # Resize image to match MobileNet input size img = image.img_to_array(img) img = np.expand_dims(img, axis=0) img = img / 255.0 # Normalize
prediction = model.predict(img)
result = labels[int(prediction[0] > 0.5)] # Assume binary classification
return f"Prediction: {result}"
Create Gradio Interface
interface = gr.Interface( fn=predict_tb, inputs=gr.Image(type="pil"), outputs="text", title="Tuberculosis Detection API", description="Upload a chest X-ray image to check if it is normal or tuberculosis." )
Launch the app
interface.launch()