krishnamishra8848's picture
Update app.py
31fec7c verified
import streamlit as st
import numpy as np
from tensorflow.keras.models import load_model
from PIL import Image
import requests
# Cache the model with st.cache_resource
@st.cache_resource
def load_model_from_hf():
# Download the model from Hugging Face
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)
# Load the model
model = load_model("saved_model.keras")
return model
# Load the model
model = load_model_from_hf()
# Nepali characters mapping
label_mapping = [
"क", "ख", "ग", "घ", "ङ", "च", "छ", "ज", "झ", "ञ",
"ट", "ठ", "ड", "ढ", "ण", "त", "थ", "द", "ध", "न",
"प", "फ", "ब", "भ", "म", "य", "र", "ल", "व", "श",
"ष", "स", "ह", "क्ष", "त्र", "ज्ञ", "०", "१", "२", "३",
"४", "५", "६", "७", "८", "९"
]
# Streamlit App
st.title("Devanagari Character Recognition")
st.write("Upload an image of a Devanagari character or digit, and the model will predict it.")
# File uploader for user to upload images
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
try:
# Preprocess the image
img = Image.open(uploaded_file).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 pixel values
img_input = img_array.reshape(1, 32, 32, 1) # Reshape for the model
# Make prediction
prediction = model.predict(img_input)
predicted_class_index = np.argmax(prediction)
predicted_character = label_mapping[predicted_class_index]
# Display the predicted character
st.success(f"Predicted Character: {predicted_character}")
except Exception as e:
st.error(f"An error occurred: {e}")