alexdekan030 commited on
Commit
589bdbf
·
verified ·
1 Parent(s): 87a400d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -37
app.py CHANGED
@@ -1,45 +1,75 @@
1
  import gradio as gr
2
  from transformers import pipeline, AutoImageProcessor, AutoModelForImageClassification
3
  import torch
 
 
4
 
5
- # Initialize the model and processor
6
- processor = AutoImageProcessor.from_pretrained("alexdekan030/autotrain-1c9rx-gdx3r")
7
- model = AutoModelForImageClassification.from_pretrained("alexdekan030/autotrain-1c9rx-gdx3r")
8
- pipe = pipeline("image-classification", model=model, image_processor=processor)
9
 
10
- def predict_pneumonia(image):
11
- """
12
- Predict whether an image shows pneumonia or normal chest X-ray
13
- Args:
14
- image: Input image
15
- Returns:
16
- dict: Dictionary containing prediction probabilities
17
- """
18
- # Make prediction
19
- result = pipe(image)
20
-
21
- # Create a formatted output dictionary
22
- probabilities = {pred['label']: float(pred['score']) for pred in result}
23
-
24
- return probabilities
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- # Create the Gradio interface
27
- demo = gr.Interface(
28
- fn=predict_pneumonia,
29
- inputs=gr.Image(type="pil"),
30
- outputs=gr.Label(num_top_classes=2),
31
- title="Pneumonia Detection from Chest X-rays",
32
- description="""Upload a chest X-ray image to detect if it shows signs of pneumonia.
33
- The model will classify the image as either 'NORMAL' or 'PNEUMONIA'
34
- and provide confidence scores for each class.""",
35
- examples=[
36
- # You can add example images here if you have them
37
- # ["path/to/example1.jpg"],
38
- # ["path/to/example2.jpg"]
39
- ],
40
- theme=gr.themes.Base()
41
- )
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  # Launch the app
44
- if __name__ == "__main__":
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()