wabala69/EATC-Assignment
0
1import streamlit as st2from tensorflow.keras.models import load_model3from tensorflow.keras.preprocessing import image4import numpy as np5from PIL import Image6 7# Load trained model8model = load_model("src/cnn_model.h5")9 10# Map class indices11class_indices = {'FAKE': 0, 'REAL': 1}12labels = {v: k for k, v in class_indices.items()}13 14# Image size (must match your model's input)15IMG_HEIGHT = 25616IMG_WIDTH = 25617 18# Streamlit UI19st.title("๐ต๏ธโโ๏ธ Deepfake Image Detector")20st.write("Upload an image and this app will tell you whether it is likely a **deepfake** or **real**.")21 22uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])23 24if uploaded_file is not None:25 # Display uploaded image26 img = Image.open(uploaded_file)27 st.image(img, caption="Uploaded Image", use_column_width=True)28 29 # Preprocess image30 img = img.convert('RGB') # ensure 3 channels31 img = img.resize((IMG_WIDTH, IMG_HEIGHT))32 img_array = image.img_to_array(img)33 img_array = img_array / 255.034 img_array = np.expand_dims(img_array, axis=0)35 36 # Predict37 prediction = model.predict(img_array)[0][0]38 predicted_class = int(np.round(prediction))39 confidence = prediction if predicted_class == 1 else 1 - prediction40 41 # Output42 st.markdown("---")43 st.subheader("๐ Prediction:")44 st.write(f"**Class:** {labels[predicted_class]}")45 st.write(f"**Confidence:** {confidence * 100:.2f}%")46 st.write(f"Raw prediction: {prediction}")47 st.write(f"**Predicted class:** {predicted_class} | **Raw score:** {prediction:.4f} | **Mapped label:** {labels[predicted_class]}")48 49 