Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,75 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline, AutoImageProcessor, AutoModelForImageClassification
|
3 |
import torch
|
|
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
pipe = pipeline("image-classification", model=model, image_processor=processor)
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
# Create the Gradio interface
|
27 |
-
demo = gr.Interface(
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
# Launch the app
|
44 |
-
|
45 |
-
demo.launch(share=True)
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline, AutoImageProcessor, AutoModelForImageClassification
|
3 |
import torch
|
4 |
+
import os
|
5 |
+
from huggingface_hub import login
|
6 |
|
7 |
+
# Login to Hugging Face Hub
|
8 |
+
if 'HF_TOKEN' in os.environ:
|
9 |
+
login(token=os.environ['HF_TOKEN'])
|
|
|
10 |
|
11 |
+
try:
|
12 |
+
# Initialize the model and processor
|
13 |
+
processor = AutoImageProcessor.from_pretrained(
|
14 |
+
"alexdekan030/autotrain-awcru-nr8j7",
|
15 |
+
use_auth_token=os.environ.get('HF_TOKEN')
|
16 |
+
)
|
17 |
+
model = AutoModelForImageClassification.from_pretrained(
|
18 |
+
"alexdekan030/autotrain-awcru-nr8j7",
|
19 |
+
use_auth_token=os.environ.get('HF_TOKEN')
|
20 |
+
)
|
21 |
+
pipe = pipeline("image-classification", model=model, image_processor=processor)
|
22 |
+
|
23 |
+
def predict_pneumonia(image):
|
24 |
+
"""
|
25 |
+
Predict whether an image shows pneumonia or normal chest X-ray
|
26 |
+
Args:
|
27 |
+
image: Input image
|
28 |
+
Returns:
|
29 |
+
dict: Dictionary containing prediction probabilities
|
30 |
+
"""
|
31 |
+
if image is None:
|
32 |
+
return {"Error": 1.0}
|
33 |
+
|
34 |
+
try:
|
35 |
+
# Make prediction
|
36 |
+
result = pipe(image)
|
37 |
+
|
38 |
+
# Create a formatted output dictionary
|
39 |
+
probabilities = {pred['label']: float(pred['score']) for pred in result}
|
40 |
+
|
41 |
+
return probabilities
|
42 |
+
except Exception as e:
|
43 |
+
return {"Error": 1.0}
|
44 |
|
45 |
+
# Create the Gradio interface
|
46 |
+
demo = gr.Interface(
|
47 |
+
fn=predict_pneumonia,
|
48 |
+
inputs=gr.Image(type="pil"),
|
49 |
+
outputs=gr.Label(num_top_classes=2),
|
50 |
+
title="Pneumonia Detection from Chest X-rays",
|
51 |
+
description="""Upload a chest X-ray image to detect if it shows signs of pneumonia.
|
52 |
+
The model will classify the image as either 'NORMAL' or 'PNEUMONIA'
|
53 |
+
and provide confidence scores for each class.""",
|
54 |
+
examples=[
|
55 |
+
# You can add example images here if you have them
|
56 |
+
# ["path/to/example1.jpg"],
|
57 |
+
# ["path/to/example2.jpg"]
|
58 |
+
]
|
59 |
+
)
|
60 |
+
|
61 |
+
except Exception as e:
|
62 |
+
# Create a simple interface if model loading fails
|
63 |
+
def error_interface(image):
|
64 |
+
return {"Error": "Model failed to load. Please check authentication and model availability."}
|
65 |
+
|
66 |
+
demo = gr.Interface(
|
67 |
+
fn=error_interface,
|
68 |
+
inputs=gr.Image(type="pil"),
|
69 |
+
outputs=gr.Label(num_top_classes=1),
|
70 |
+
title="Error Loading Model",
|
71 |
+
description="There was an error loading the model. Please check if the model is accessible and authentication is correct."
|
72 |
+
)
|
73 |
|
74 |
# Launch the app
|
75 |
+
demo.launch()
|
|