import streamlit as st import torch import torchvision.transforms as transforms from torchvision.models.mobilenetv2 import MobileNetV2 from PIL import Image import numpy as np import cv2 from datetime import datetime from gtts import gTTS import os # ====== Setup ====== os.makedirs("history", exist_ok=True) torch.serialization.add_safe_globals({'MobileNetV2': MobileNetV2}) # Load model model = torch.load("pkr_currency_classifier.pt", map_location='cpu', weights_only=False) model.eval() class_names = ['Fake', 'Not Currency', 'Real'] # Transforms transform = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor() ]) # Text-to-speech def speak_streamlit(text): tts = gTTS(text=text, lang='en') tts.save("temp.mp3") st.audio("temp.mp3", format="audio/mp3") # Prediction def predict(image): img = transform(image).unsqueeze(0) with torch.no_grad(): outputs = model(img) probs = torch.nn.functional.softmax(outputs, dim=1) confidence, predicted = torch.max(probs, 1) print("Probabilities:", probs.numpy()) print("Confidence:", confidence.item()) return class_names[predicted.item()] # Save history def save_history(image, result): timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") image.save(f"history/{timestamp}_{result}.png") # ====== PAGE CONFIG & CUSTOM STYLING ====== st.set_page_config(page_title="Currency Authenticity Detector", page_icon="💵", layout="centered") # Custom CSS st.markdown(""" """, unsafe_allow_html=True) # ====== UI Layout ====== st.markdown("# 💵 Currency Authenticity Detector") st.markdown("### ✅ Instantly check if your currency is **Real or Fake**!") currency_type = st.selectbox("🔄 Select currency type:", ["PKR (Pakistani Rupees)", "USD (US Dollars)", "INR (Indian Rupees)"]) if "PKR" not in currency_type: st.warning("⚠️ Currently only Pakistani Rupees (PKR) is supported. Other currencies coming soon!") st.stop() st.markdown("### 🖼️ Choose how to scan your currency note:") # Choose input method option = st.radio("🔍 Input Method:", ["Upload Image", "Scan via Camera"]) # ===== Upload Image ===== if option == "Upload Image": uploaded_file = st.file_uploader("📤 Upload a currency image", type=["jpg", "jpeg", "png"]) if uploaded_file: image = Image.open(uploaded_file).convert("RGB") st.image(image, caption="🖼️ Uploaded Image", use_column_width=True) prediction = predict(image) if prediction == "Not Currency": st.markdown('