Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
-
import requests
|
4 |
from PIL import Image
|
5 |
import numpy as np
|
6 |
from transformers import AutoProcessor, AutoModelForPreTraining
|
@@ -12,23 +11,23 @@ token = os.getenv("HF_Token")
|
|
12 |
login(token)
|
13 |
|
14 |
# Initialize the Hugging Face model
|
15 |
-
|
16 |
model = AutoModelForPreTraining.from_pretrained("google/paligemma-3b-mix-224")
|
17 |
|
18 |
# Function to transcribe handwritten notes using Hugging Face model
|
19 |
def transcribe_handwriting(image):
|
20 |
-
# Convert image to
|
21 |
-
image =
|
22 |
-
|
23 |
|
24 |
# Prepare input for the model
|
25 |
-
inputs =
|
26 |
-
|
27 |
# Generate output
|
28 |
with torch.no_grad():
|
29 |
outputs = model.generate(**inputs, max_length=512)
|
30 |
-
|
31 |
-
transcription =
|
32 |
return transcription
|
33 |
|
34 |
# Set Streamlit page configuration
|
@@ -45,14 +44,14 @@ st.title("Doctor's Prescription Reader 💊")
|
|
45 |
uploaded_file = st.file_uploader("Upload Prescription Image", type=["jpg", "jpeg", "png"])
|
46 |
|
47 |
if uploaded_file is not None:
|
48 |
-
|
49 |
image = Image.open(uploaded_file)
|
50 |
|
51 |
# Display uploaded image
|
52 |
-
st.image(
|
53 |
|
54 |
with st.spinner("Transcribing handwriting..."):
|
55 |
# Transcribe handwritten notes
|
56 |
-
extracted_text = transcribe_handwriting(
|
57 |
st.subheader("Transcribed Text from Prescription:")
|
58 |
-
st.text(extracted_text)
|
|
|
1 |
import os
|
2 |
import streamlit as st
|
|
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
5 |
from transformers import AutoProcessor, AutoModelForPreTraining
|
|
|
11 |
login(token)
|
12 |
|
13 |
# Initialize the Hugging Face model
|
14 |
+
processor = AutoProcessor.from_pretrained("google/paligemma-3b-mix-224")
|
15 |
model = AutoModelForPreTraining.from_pretrained("google/paligemma-3b-mix-224")
|
16 |
|
17 |
# Function to transcribe handwritten notes using Hugging Face model
|
18 |
def transcribe_handwriting(image):
|
19 |
+
# Convert image to RGB and numpy array
|
20 |
+
image = image.convert("RGB")
|
21 |
+
image_array = np.array(image)
|
22 |
|
23 |
# Prepare input for the model
|
24 |
+
inputs = processor(image_array, return_tensors="pt")
|
25 |
+
|
26 |
# Generate output
|
27 |
with torch.no_grad():
|
28 |
outputs = model.generate(**inputs, max_length=512)
|
29 |
+
|
30 |
+
transcription = processor.decode(outputs[0], skip_special_tokens=True)
|
31 |
return transcription
|
32 |
|
33 |
# Set Streamlit page configuration
|
|
|
44 |
uploaded_file = st.file_uploader("Upload Prescription Image", type=["jpg", "jpeg", "png"])
|
45 |
|
46 |
if uploaded_file is not None:
|
47 |
+
# Open the image using PIL
|
48 |
image = Image.open(uploaded_file)
|
49 |
|
50 |
# Display uploaded image
|
51 |
+
st.image(image, caption="Uploaded Prescription", use_column_width=True)
|
52 |
|
53 |
with st.spinner("Transcribing handwriting..."):
|
54 |
# Transcribe handwritten notes
|
55 |
+
extracted_text = transcribe_handwriting(image)
|
56 |
st.subheader("Transcribed Text from Prescription:")
|
57 |
+
st.text(extracted_text)
|