abuzarAli commited on
Commit
a733905
·
verified ·
1 Parent(s): fa312c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -63
app.py CHANGED
@@ -1,74 +1,36 @@
1
- import os
2
  import streamlit as st
3
- from transformers import pipeline, AutoImageProcessor, AutoModelForImageClassification
4
  from PIL import Image
 
 
5
 
6
- def load_pipeline():
7
- """Load the Hugging Face pipeline for image classification."""
8
- try:
9
- return pipeline("image-classification", model="dima806/pneumonia_chest_xray_image_detection")
10
- except Exception as e:
11
- st.error(f"Error loading pipeline: {e}")
12
- return None
13
-
14
- def classify_image_with_pipeline(pipe, image):
15
- """Classify an image using the pipeline."""
16
- try:
17
- results = pipe(image)
18
- return results
19
- except Exception as e:
20
- st.error(f"Error classifying image: {e}")
21
- return None
22
 
23
- # Streamlit App
24
  st.title("Pneumonia Chest X-ray Image Detection")
25
- st.markdown(
26
- """
27
- This app detects signs of pneumonia in chest X-ray images using a pre-trained Hugging Face model.
28
- """
29
- )
30
-
31
- # File uploader
32
- uploaded_file = st.file_uploader("Upload a chest X-ray image", type=["jpg", "jpeg", "png"])
33
 
34
- if uploaded_file:
35
- image = Image.open(uploaded_file)
36
- st.image(image, caption="Uploaded Chest X-ray", use_column_width=True)
37
 
38
- # Load the model pipeline
39
- pipe = load_pipeline()
40
-
41
- if pipe:
42
- st.write("Classifying the image...")
43
- results = classify_image_with_pipeline(pipe, image)
44
-
45
- if results:
46
- st.write("### Classification Results:")
47
- for result in results:
48
- st.write(f"**Label:** {result['label']} | **Score:** {result['score']:.4f}")
49
-
50
- # Optional: Add Groq API integration if applicable
51
- if os.getenv("GROQ_API_KEY"):
52
- from groq import Groq
53
 
54
- client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
 
55
 
56
- st.sidebar.markdown("### Groq API Integration")
57
- question = st.sidebar.text_input("Ask a question about pneumonia or X-ray diagnosis:")
 
58
 
59
- if question:
60
- try:
61
- chat_completion = client.chat.completions.create(
62
- messages=[
63
- {
64
- "role": "user",
65
- "content": question,
66
- }
67
- ],
68
- model="llama-3.3-70b-versatile",
69
- )
70
 
71
- st.sidebar.write("**Groq API Response:**")
72
- st.sidebar.write(chat_completion.choices[0].message.content)
73
- except Exception as e:
74
- st.sidebar.error(f"Error using Groq API: {e}")
 
 
1
  import streamlit as st
 
2
  from PIL import Image
3
+ from transformers import pipeline
4
+ import os
5
 
6
+ # Check if Groq API key is set and load it if available
7
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
8
+ if GROQ_API_KEY:
9
+ from groq import Groq
10
+ client = Groq(api_key=GROQ_API_KEY)
11
+ # Use Groq API here if you want it for predictions or related tasks
 
 
 
 
 
 
 
 
 
 
12
 
13
+ # Streamlit setup
14
  st.title("Pneumonia Chest X-ray Image Detection")
 
 
 
 
 
 
 
 
15
 
16
+ # Upload image
17
+ uploaded_image = st.file_uploader("Choose a chest X-ray image...", type=["jpg", "jpeg", "png"])
 
18
 
19
+ if uploaded_image is not None:
20
+ # Display the image
21
+ image = Image.open(uploaded_image)
22
+ st.image(image, caption="Uploaded X-ray Image", use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ # Load the Hugging Face model using the pipeline
25
+ pipe = pipeline("image-classification", model="dima806/pneumonia_chest_xray_image_detection")
26
 
27
+ # Run prediction
28
+ with st.spinner("Classifying..."):
29
+ prediction = pipe(image)
30
 
31
+ # Display results
32
+ st.write(f"Prediction: {prediction[0]['label']}")
33
+ st.write(f"Confidence: {prediction[0]['score']:.4f}")
 
 
 
 
 
 
 
 
34
 
35
+ else:
36
+ st.warning("Please upload a chest X-ray image to begin detection.")