Spaces:
Sleeping
Sleeping
File size: 1,791 Bytes
632ca8b 7f52c00 60c15f5 7f52c00 60c15f5 34d51d0 60c15f5 b1f8173 6b8a2fa 7f52c00 60c15f5 34d51d0 60c15f5 7aa5308 60c15f5 7f52c00 7aa5308 7f52c00 60c15f5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import os
import streamlit as st
import requests
from PIL import Image
import numpy as np
from transformers import AutoProcessor, AutoModelForPreTraining
import torch
from huggingface_hub import login
# Get the token from environment variables (set in Hugging Face Space)
token = os.getenv("HF_Token")
login(token)
# Initialize the Hugging Face model
tokenizer = AutoProcessor.from_pretrained("google/paligemma-3b-mix-224")
model = AutoModelForPreTraining.from_pretrained("google/paligemma-3b-mix-224")
# Function to transcribe handwritten notes using Hugging Face model
def transcribe_handwriting(image):
# Convert image to array and preprocess
image = Image.convert("RGB")
image = np.array(image)
# Prepare input for the model
inputs = tokenizer(image, return_tensors="pt")
# Generate output
with torch.no_grad():
outputs = model.generate(**inputs, max_length=512)
transcription = tokenizer.decode(outputs[0], skip_special_tokens=True)
return transcription
# Set Streamlit page configuration
st.set_page_config(
page_title="Prescription Reader",
page_icon="π",
layout="centered",
)
# Header
st.title("Doctor's Prescription Reader π")
# Upload prescription image
uploaded_file = st.file_uploader("Upload Prescription Image", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Open the image using PIL
image = Image.open(uploaded_file)
# Display uploaded image
st.image(uploaded_file, caption="Uploaded Prescription", use_column_width=True)
with st.spinner("Transcribing handwriting..."):
# Transcribe handwritten notes
extracted_text = transcribe_handwriting(uploaded_file)
st.subheader("Transcribed Text from Prescription:")
st.text(extracted_text) |