Spaces:
Runtime error
Runtime error
Update app.py
Browse files7 unmodified lines89101112131427 unmodified lines424344454647484950515253545 unmodified lines606162636465666 unmodified lines737475767778798081828384858687888990919293941 unmodified line969798991001011021031047 unmodified linesfrom huggingface_hub import hf_hub_downloadimport streamlit as st# Constants and environment variablesTOKEN = os.getenv('hf_read_token')repo_id = "Nischay103/captcha_recognition"model_files = {27 unmodified lines"11": "0123456789$"}# Load modelsmodels = {}for key, model_file in model_files.items():model_path = hf_hub_download(repo_id=repo_id, filename=model_file, token=TOKEN)models[key] = torch.jit.load(model_path)# Function to transform imagedef transform_image(image_path):transform = T.Compose([T.ToTensor()])device = 'cuda' if torch.cuda.is_available() else 'cpu'5 unmodified linesimage = image.unsqueeze(1)return image# Function to get label from model predictiondef get_label(model_prediction, model_version):max_captcha_len, cls_dim_encoded = len_dim_pair[model_version]_cls = char_sets[cls_dim_encoded]6 unmodified lineslab += get_charreturn lab# Streamlit appst.title("Character Sequence Recognition from Scene-Image (Captcha)")st.write("Recognize captchas using different models")uploaded_file = st.file_uploader("Choose a captcha image...", type=["jpg", "png"])model_version = st.selectbox("Model Version", list(model_files.keys()), index=0)if uploaded_file is not None:# Save uploaded file to a temporary locationwith open("temp_captcha_image.png", "wb") as f:f.write(uploaded_file.getbuffer())input_image_path = "temp_captcha_image.png"st.image(input_image_path, caption='Uploaded Captcha Image', use_column_width=True)if st.button('Recognize'):input = transform_image(input_image_path)model = models[model_version]with torch.no_grad():1 unmodified lineoutput = get_label(model_prediction, model_version)st.write(f"Recognized Character Sequence: {output}")st.write("## Examples")cols = st.columns(3)for idx,(model_variant,captcha_path) in enumerate(example_captchas.items()):col = cols[idx % 3]col.image(captcha_path,caption=f'Model {model_variant}: {captcha_path.split("/")[-1]}', width=100)col.write(f"Model Version: {model_variant}")7 unmodified lines891011121327 unmodified lines41424344454647484950515 unmodified lines5758596061626 unmodified lines69707172737475767778798081828384858687881 unmodified line9091929394959697987 unmodified linesfrom huggingface_hub import hf_hub_downloadimport streamlit as stTOKEN = os.getenv('hf_read_token')repo_id = "Nischay103/captcha_recognition"model_files = {27 unmodified lines"11": "0123456789$"}models = {}for key, model_file in model_files.items():model_path = hf_hub_download(repo_id=repo_id, filename=model_file, token=TOKEN)models[key] = torch.jit.load(model_path)def transform_image(image_path):transform = T.Compose([T.ToTensor()])device = 'cuda' if torch.cuda.is_available() else 'cpu'5 unmodified linesimage = image.unsqueeze(1)return imagedef get_label(model_prediction, model_version):max_captcha_len, cls_dim_encoded = len_dim_pair[model_version]_cls = char_sets[cls_dim_encoded]6 unmodified lineslab += get_charreturn labst.title("char-seq recognition from scene-image (captcha)")st.write("recognize captchas using different models")uploaded_file = st.file_uploader("choose a captcha image...", type=["jpg", "png"])model_version = st.selectbox("model variant", list(model_files.keys()), index=0)if uploaded_file is not None:with open("temp_captcha_image.png", "wb") as f:f.write(uploaded_file.getbuffer())input_image_path = "temp_captcha_image.png"st.image(input_image_path, caption='uploaded captcha image', use_column_width=True)if st.button('recognize'):input = transform_image(input_image_path)model = models[model_version]with torch.no_grad():1 unmodified lineoutput = get_label(model_prediction, model_version)st.write(f"Recognized Character Sequence: {output}")st.write("## examples")cols = st.columns(4)for idx,(model_variant,captcha_path) in enumerate(example_captchas.items()):col = cols[idx % 4]col.image(captcha_path,caption=f'{captcha_path.split("/")[-1]}', width=100)col.write(f"Model Version: {model_variant}")
app.py+10-16