Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,26 +4,28 @@ from PIL import Image
|
|
4 |
import torch
|
5 |
|
6 |
# Load model and processor
|
7 |
-
@st.cache_resource
|
8 |
def load_model():
|
9 |
processor = AutoProcessor.from_pretrained("HuggingFaceTB/SmolVLM-Instruct")
|
10 |
model = AutoModelForImageTextToText.from_pretrained("HuggingFaceTB/SmolVLM-Instruct")
|
11 |
return processor, model
|
12 |
|
13 |
-
# Extract text from image
|
14 |
def extract_text(image, processor, model):
|
15 |
# Preprocess image
|
16 |
-
inputs = processor(images=image,
|
17 |
|
|
|
18 |
with torch.no_grad():
|
19 |
outputs = model.generate(**inputs)
|
20 |
|
|
|
21 |
result = processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
22 |
return result
|
23 |
|
24 |
# Streamlit UI
|
25 |
def main():
|
26 |
-
st.title("🖼️ OCR App using SmolVLM")
|
27 |
st.write("Upload an image, and I will extract the text for you!")
|
28 |
|
29 |
# Load the model and processor
|
@@ -39,11 +41,12 @@ def main():
|
|
39 |
|
40 |
# Extract text
|
41 |
with st.spinner("Extracting text..."):
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
47 |
|
48 |
if __name__ == "__main__":
|
49 |
main()
|
|
|
4 |
import torch
|
5 |
|
6 |
# Load model and processor
|
7 |
+
@st.cache_resource
|
8 |
def load_model():
|
9 |
processor = AutoProcessor.from_pretrained("HuggingFaceTB/SmolVLM-Instruct")
|
10 |
model = AutoModelForImageTextToText.from_pretrained("HuggingFaceTB/SmolVLM-Instruct")
|
11 |
return processor, model
|
12 |
|
13 |
+
# Extract text from image
|
14 |
def extract_text(image, processor, model):
|
15 |
# Preprocess image
|
16 |
+
inputs = processor(images=image, return_tensors="pt")
|
17 |
|
18 |
+
# Perform generation
|
19 |
with torch.no_grad():
|
20 |
outputs = model.generate(**inputs)
|
21 |
|
22 |
+
# Decode outputs
|
23 |
result = processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
24 |
return result
|
25 |
|
26 |
# Streamlit UI
|
27 |
def main():
|
28 |
+
st.title("🖼️ OCR App using SmolVLM-Instruct")
|
29 |
st.write("Upload an image, and I will extract the text for you!")
|
30 |
|
31 |
# Load the model and processor
|
|
|
41 |
|
42 |
# Extract text
|
43 |
with st.spinner("Extracting text..."):
|
44 |
+
try:
|
45 |
+
extracted_text = extract_text(image, processor, model)
|
46 |
+
st.subheader("📝 Extracted Text:")
|
47 |
+
st.write(extracted_text)
|
48 |
+
except Exception as e:
|
49 |
+
st.error(f"An error occurred: {str(e)}")
|
50 |
|
51 |
if __name__ == "__main__":
|
52 |
main()
|