Spaces:
Running
Running
Akshayram1
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -11,33 +11,45 @@ def load_model():
|
|
11 |
model = AutoModelForImageTextToText.from_pretrained("HuggingFaceTB/SmolVLM-Instruct")
|
12 |
return processor, model
|
13 |
|
14 |
-
#
|
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 |
-
#
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
33 |
time.sleep(0.5)
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
39 |
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
# Streamlit UI
|
43 |
def main():
|
@@ -51,18 +63,19 @@ def main():
|
|
51 |
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
|
52 |
|
53 |
if uploaded_file is not None:
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
try:
|
61 |
extracted_text = extract_text(image, processor, model)
|
62 |
st.subheader("📝 Extracted Text:")
|
63 |
st.write(extracted_text)
|
64 |
-
|
65 |
-
|
|
|
66 |
|
67 |
if __name__ == "__main__":
|
68 |
main()
|
|
|
11 |
model = AutoModelForImageTextToText.from_pretrained("HuggingFaceTB/SmolVLM-Instruct")
|
12 |
return processor, model
|
13 |
|
14 |
+
# Function to preprocess image and handle model execution
|
15 |
def extract_text(image, processor, model):
|
16 |
# Initialize progress bar
|
17 |
progress_bar = st.progress(0)
|
|
|
|
|
|
|
18 |
time.sleep(0.5)
|
19 |
|
20 |
+
# Resize the image to fixed dimensions
|
21 |
+
try:
|
22 |
+
required_size = (224, 224) # Explicit resizing for model input
|
23 |
+
image_resized = image.resize(required_size)
|
24 |
+
progress_bar.progress(20) # Step 1: Image resized
|
25 |
+
time.sleep(0.5)
|
26 |
+
|
27 |
+
# Preprocess image (extract pixel values)
|
28 |
+
inputs = processor(images=image_resized, return_tensors="pt", do_resize=False).to("cpu")
|
29 |
+
pixel_values = inputs.get("pixel_values")
|
30 |
|
31 |
+
# Check if pixel values are valid
|
32 |
+
if pixel_values is None or pixel_values.shape[0] == 0:
|
33 |
+
raise ValueError("Preprocessing failed: Empty tensor generated for image.")
|
34 |
+
|
35 |
+
progress_bar.progress(50) # Step 2: Image preprocessed
|
36 |
time.sleep(0.5)
|
37 |
|
38 |
+
# Perform inference
|
39 |
+
with torch.no_grad():
|
40 |
+
outputs = model.generate(pixel_values=pixel_values)
|
41 |
+
progress_bar.progress(80) # Step 3: Model processing
|
42 |
+
time.sleep(0.5)
|
43 |
|
44 |
+
# Decode outputs to text
|
45 |
+
result = processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
46 |
+
progress_bar.progress(100) # Step 4: Completed
|
47 |
+
time.sleep(0.5)
|
48 |
+
|
49 |
+
return result
|
50 |
+
|
51 |
+
except Exception as e:
|
52 |
+
raise RuntimeError(f"Error during text extraction: {str(e)}")
|
53 |
|
54 |
# Streamlit UI
|
55 |
def main():
|
|
|
63 |
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
|
64 |
|
65 |
if uploaded_file is not None:
|
66 |
+
try:
|
67 |
+
# Open and display image
|
68 |
+
image = Image.open(uploaded_file).convert("RGB")
|
69 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
70 |
|
71 |
+
# Extract text with progress bar
|
72 |
+
with st.spinner("Extracting text... Please wait!"):
|
|
|
73 |
extracted_text = extract_text(image, processor, model)
|
74 |
st.subheader("📝 Extracted Text:")
|
75 |
st.write(extracted_text)
|
76 |
+
|
77 |
+
except Exception as e:
|
78 |
+
st.error(f"An error occurred: {str(e)}")
|
79 |
|
80 |
if __name__ == "__main__":
|
81 |
main()
|