import streamlit as st import numpy as np import cv2 from tensorflow.keras.models import load_model from PIL import Image import requests # Load the model from Hugging Face @st.cache(allow_output_mutation=True) def load_model_from_hf(): url = "https://huggingface.co/krishnamishra8848/Devanagari_Character_Recognition/resolve/main/saved_model.keras" response = requests.get(url) with open("saved_model.keras", "wb") as f: f.write(response.content) model = load_model("saved_model.keras") return model model = load_model_from_hf() # Label mapping label_mapping = [ "क", "ख", "ग", "घ", "ङ", "च", "छ", "ज", "झ", "ञ", "ट", "ठ", "ड", "ढ", "ण", "त", "थ", "द", "ध", "न", "प", "फ", "ब", "भ", "म", "य", "र", "ल", "व", "श", "ष", "स", "ह", "क्ष", "त्र", "ज्ञ", "०", "१", "२", "३", "४", "५", "६", "७", "८", "९" ] # Streamlit app interface st.title("Devanagari Character Recognition") st.write("Upload an image of a Devanagari character or digit, and the model will predict it.") # File uploader uploaded_file = st.file_uploader("Choose a file", type=["jpg", "png", "jpeg"]) if uploaded_file is not None: # Load and preprocess the image img = Image.open(uploaded_file) img = img.convert("L") # Convert to grayscale img_resized = img.resize((32, 32)) # Resize to 32x32 img_array = np.array(img_resized).astype("float32") / 255.0 # Normalize img_input = img_array.reshape(1, 32, 32, 1) # Reshape for the model # Display uploaded image st.image(img, caption="Uploaded Image", use_column_width=True) # Make prediction prediction = model.predict(img_input) predicted_class_index = np.argmax(prediction) predicted_character = label_mapping[predicted_class_index] # Display result st.success(f"Predicted Character: {predicted_character}")