Spaces:
Build error
Build error
| import warnings | |
| from fastai.vision.all import * | |
| import gradio as gr | |
| import pathlib | |
| # Suppress the pickle warning for demo purposes | |
| warnings.filterwarnings("ignore", category=UserWarning, module="fastai.learner") | |
| # Load the model | |
| learn = load_learner("export.pkl") | |
| labels = learn.dls.vocab | |
| def predict(img): | |
| """ | |
| Predict skin lesion classification for the given image. | |
| Args: | |
| img: PIL Image object from Gradio | |
| Returns: | |
| dict: Classification probabilities for each class | |
| """ | |
| try: | |
| # Convert to PILImage if needed | |
| img = PILImage.create(img) | |
| pred, pred_idx, probs = learn.predict(img) | |
| # Return as dictionary with float probabilities for JSON serialization | |
| return {labels[i]: float(probs[i]) for i in range(len(labels))} | |
| except Exception as e: | |
| raise gr.Error(f"Error processing image: {str(e)}") | |
| # App metadata | |
| title = "Skin Lesion Classifier [RESNET 50]" | |
| description = "A skin lesion classifier trained on the ISIC2019 dataset with fastai. Created as a demo for Gradio and HuggingFace Spaces." | |
| article = "<p style='text-align: center'><a href='https://challenge.isic-archive.com/data/' target='_blank'>Link to ISIC Dataset</a></p>" | |
| # Example images | |
| examples = ['img1.jpg', 'img2.jpg', 'img3.jpg'] if all(pathlib.Path(f).exists() for f in ['img1.jpg', 'img2.jpg', 'img3.jpg']) else None | |
| # Create the modern Gradio interface | |
| def create_interface(): | |
| """Create and return the Gradio interface""" | |
| with gr.Blocks( | |
| title=title, | |
| theme=gr.themes.Soft(), | |
| css=".gradio-container {max-width: 700px; margin: auto;}" | |
| ) as demo: | |
| gr.Markdown(f"# {title}") | |
| gr.Markdown(description) | |
| with gr.Row(): | |
| with gr.Column(): | |
| image_input = gr.Image( | |
| label="Upload Skin Lesion Image", | |
| type="pil", | |
| ) | |
| predict_btn = gr.Button( | |
| "Classify Lesion", | |
| variant="primary", | |
| ) | |
| with gr.Column(): | |
| output_label = gr.Label( | |
| label="Classification Results", | |
| ) | |
| # Add examples if available | |
| if examples: | |
| gr.Examples( | |
| examples=examples, | |
| inputs=image_input, | |
| outputs=output_label, | |
| fn=predict, | |
| cache_examples=True | |
| ) | |
| # Event handlers | |
| predict_btn.click( | |
| fn=predict, | |
| inputs=image_input, | |
| outputs=output_label, | |
| show_progress=True | |
| ) | |
| # Also trigger on image upload | |
| image_input.upload( | |
| fn=predict, | |
| inputs=image_input, | |
| outputs=output_label, | |
| show_progress=True | |
| ) | |
| gr.Markdown(article) | |
| return demo | |
| if __name__ == '__main__': | |
| # Create and launch the interface | |
| demo = create_interface() | |
| demo.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| share=False, | |
| show_error=True | |
| ) | |
| # import gradio as gr | |
| # from fastai.vision.all import * | |
| # import skimage | |
| # #Importing necessary libraries | |
| # import gradio as gr | |
| # #import scikit-learn as sklearn | |
| # from fastai.vision.all import * | |
| # from sklearn.metrics import roc_auc_score | |
| # learn = load_learner('export.pkl') | |
| # labels = learn.dls.vocab | |
| # def predict(img): | |
| # img = PILImage.create(img) | |
| # pred,pred_idx,probs = learn.predict(img) | |
| # return {labels[i]: float(probs[i]) for i in range(len(labels))} | |
| # examples = ['img1.jpg','img2.jpg','img3.jpg'] | |
| # #Launching the gradio application | |
| # gr.Interface(fn=predict,inputs=gr.inputs.Image(shape=(512, 512)), | |
| # outputs=gr.outputs.Label(num_top_classes=1), | |
| # title=title, | |
| # description=description,article=article, | |
| # examples=examples, | |
| # enable_queue=enable_queue).launch(inline=False) | |
| # #gr.Interface(fn=predict,inputs=gr.inputs.Image(shape=(224, 224)),outputs=gr.outputs.Label(num_top_classes=3),title=title,description=description,article=article,examples=examples,interpretation=interpretation,enable_queue=enable_queue).launch() |