Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
|
|
2 |
from transformers import AutoProcessor, AutoModelForImageTextToText
|
3 |
from PIL import Image
|
4 |
import torch
|
|
|
5 |
|
6 |
# Load model and processor
|
7 |
@st.cache_resource
|
@@ -12,16 +13,30 @@ def load_model():
|
|
12 |
|
13 |
# Extract text from image
|
14 |
def extract_text(image, processor, model):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
# Preprocess image (extract pixel values only)
|
16 |
inputs = processor(images=image, return_tensors="pt").to("cpu")
|
17 |
-
pixel_values = inputs.get("pixel_values")
|
|
|
|
|
18 |
|
19 |
# Perform generation
|
20 |
with torch.no_grad():
|
21 |
-
outputs = model.generate(pixel_values=pixel_values)
|
|
|
|
|
22 |
|
23 |
# Decode outputs
|
24 |
result = processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
|
|
|
|
|
|
25 |
return result
|
26 |
|
27 |
# Streamlit UI
|
@@ -38,10 +53,10 @@ def main():
|
|
38 |
if uploaded_file is not None:
|
39 |
# Open image
|
40 |
image = Image.open(uploaded_file).convert("RGB")
|
41 |
-
st.image(image, caption="Uploaded Image",
|
42 |
|
43 |
-
# Extract text
|
44 |
-
with st.spinner("Extracting text..."):
|
45 |
try:
|
46 |
extracted_text = extract_text(image, processor, model)
|
47 |
st.subheader("📝 Extracted Text:")
|
|
|
2 |
from transformers import AutoProcessor, AutoModelForImageTextToText
|
3 |
from PIL import Image
|
4 |
import torch
|
5 |
+
import time # To simulate progress bar updates
|
6 |
|
7 |
# Load model and processor
|
8 |
@st.cache_resource
|
|
|
13 |
|
14 |
# Extract text from image
|
15 |
def extract_text(image, processor, model):
|
16 |
+
# Initialize progress bar
|
17 |
+
progress_bar = st.progress(0)
|
18 |
+
|
19 |
+
# Simulate steps for progress
|
20 |
+
progress_bar.progress(20) # Step 1: Starting processing
|
21 |
+
time.sleep(0.5)
|
22 |
+
|
23 |
# Preprocess image (extract pixel values only)
|
24 |
inputs = processor(images=image, return_tensors="pt").to("cpu")
|
25 |
+
pixel_values = inputs.get("pixel_values")
|
26 |
+
progress_bar.progress(50) # Step 2: Preprocessed image
|
27 |
+
time.sleep(0.5)
|
28 |
|
29 |
# Perform generation
|
30 |
with torch.no_grad():
|
31 |
+
outputs = model.generate(pixel_values=pixel_values)
|
32 |
+
progress_bar.progress(80) # Step 3: Model processing
|
33 |
+
time.sleep(0.5)
|
34 |
|
35 |
# Decode outputs
|
36 |
result = processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
37 |
+
progress_bar.progress(100) # Step 4: Completed
|
38 |
+
time.sleep(0.5)
|
39 |
+
|
40 |
return result
|
41 |
|
42 |
# Streamlit UI
|
|
|
53 |
if uploaded_file is not None:
|
54 |
# Open image
|
55 |
image = Image.open(uploaded_file).convert("RGB")
|
56 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
57 |
|
58 |
+
# Extract text with progress bar
|
59 |
+
with st.spinner("Extracting text... Please wait!"):
|
60 |
try:
|
61 |
extracted_text = extract_text(image, processor, model)
|
62 |
st.subheader("📝 Extracted Text:")
|