Spaces:
Build error
Build error
| import gradio as gr | |
| import numpy as np | |
| from PIL import Image | |
| import tensorflow as tf | |
| # Load TFLite model | |
| interpreter = tf.lite.Interpreter(model_path="model.tflite") | |
| interpreter.allocate_tensors() | |
| # Get input and output details | |
| input_details = interpreter.get_input_details() | |
| output_details = interpreter.get_output_details() | |
| class_names = ['Biceps Brachii', 'Gastrocnemius Medialis', 'Healthy', 'Tibialis Anterior'] | |
| CONFIDENCE_THRESHOLD = 0.7 | |
| def predict_image(image): | |
| try: | |
| image = image.resize((128, 128)).convert("RGB") | |
| img_array = np.array(image, dtype=np.float32) / 255.0 | |
| img_array = np.expand_dims(img_array, axis=0) | |
| interpreter.set_tensor(input_details[0]['index'], img_array) | |
| interpreter.invoke() | |
| output = interpreter.get_tensor(output_details[0]['index']) | |
| class_idx = int(np.argmax(output)) | |
| confidence = float(np.max(output)) | |
| if confidence < CONFIDENCE_THRESHOLD: | |
| return f"⚠️ Low confidence ({confidence:.2f}). The model is unsure. Please try a clearer image." | |
| else: | |
| return f"✅ Prediction: {class_names[class_idx]} (Confidence: {confidence:.2f})" | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| gr.Interface( | |
| fn=predict_image, | |
| inputs=gr.Image(type="pil"), | |
| outputs="text", | |
| title="Muscle Disease Detection", | |
| description="Upload an MRI image to detect muscle conditions." | |
| ).launch() | |