munnae commited on
Commit
d87cfca
·
verified ·
1 Parent(s): e580512
Files changed (1) hide show
  1. app.py +39 -33
app.py CHANGED
@@ -1,45 +1,51 @@
1
- from flask import Flask, request, jsonify
2
  from transformers import pipeline
3
  from PIL import Image
4
  import io
5
 
6
- app = Flask(__name__)
 
7
 
8
- # Load model at startup
9
- print("Loading model...")
10
- classifier = pipeline("image-classification", model="Xenova/mobilenet_v2_1.0_224")
11
- print("Model loaded successfully!")
 
 
 
12
 
13
- @app.route('/')
14
- def home():
15
- return "Food Image Classifier API is running!"
16
 
17
- @app.route('/classify', methods=['POST'])
18
- def classify_image():
19
- if 'file' not in request.files:
20
- return jsonify({"error": "No file provided"}), 400
21
 
22
- file = request.files['file']
23
 
24
- try:
25
- # Convert file to PIL image
26
- image = Image.open(io.BytesIO(file.read()))
27
 
28
- # Run classification
 
 
 
 
29
  results = classifier(image)
30
 
31
- # Get top result
32
- if results:
33
- prediction = {
34
- "label": results[0]['label'],
35
- "confidence": results[0]['score']
36
- }
37
- return jsonify(prediction)
38
- else:
39
- return jsonify({"error": "No results returned"}), 500
40
-
41
- except Exception as e:
42
- return jsonify({"error": str(e)}), 500
43
-
44
- if __name__ == '__main__':
45
- app.run(host="0.0.0.0", port=7860, debug=True)
 
 
1
+ import streamlit as st
2
  from transformers import pipeline
3
  from PIL import Image
4
  import io
5
 
6
+ # Set Streamlit page config
7
+ st.set_page_config(page_title="Food Image Classifier", layout="centered")
8
 
9
+ # Load the model
10
+ @st.cache_resource
11
+ def load_model():
12
+ st.text("Loading model...")
13
+ model = pipeline("image-classification", model="Xenova/mobilenet_v2_1.0_224")
14
+ st.text("Model loaded successfully!")
15
+ return model
16
 
17
+ classifier = load_model()
 
 
18
 
19
+ # Streamlit UI
20
+ st.title("🍕🥖 Food Image Classifier")
21
+ st.write("Upload an image of **roti, pizza, naan, or tofu** to classify.")
 
22
 
23
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
24
 
25
+ if uploaded_file is not None:
26
+ # Convert file to PIL image
27
+ image = Image.open(uploaded_file)
28
 
29
+ # Display the uploaded image
30
+ st.image(image, caption="Uploaded Image", use_column_width=True)
31
+
32
+ # Classify the image
33
+ with st.spinner("Classifying..."):
34
  results = classifier(image)
35
 
36
+ # Display results
37
+ if results:
38
+ label = results[0]['label']
39
+ confidence = results[0]['score'] * 100 # Convert to percentage
40
+
41
+ st.success(f"**Prediction:** {label}")
42
+ st.info(f"**Confidence:** {confidence:.2f}%")
43
+
44
+ # Option to classify another image
45
+ st.button("Classify Another Image", on_click=lambda: st.experimental_rerun())
46
+
47
+ # Footer
48
+ st.markdown("---")
49
+ st.markdown("Made by **Muneeb Sahaf** | Final Year Project 2025")
50
+
51
+