Akshayram1 commited on
Commit
5037d5c
·
verified ·
1 Parent(s): cea10a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -9
app.py CHANGED
@@ -4,26 +4,28 @@ from PIL import Image
4
  import torch
5
 
6
  # Load model and processor
7
- @st.cache_resource # Cache model to avoid reloading
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 using SmolVLM
14
  def extract_text(image, processor, model):
15
  # Preprocess image
16
- inputs = processor(images=image, text="What is the text in this image? extract all data in JSON format", return_tensors="pt")
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
- extracted_text = extract_text(image, processor, model)
43
-
44
- # Display result
45
- st.subheader("📝 Extracted Text:")
46
- st.write(extracted_text)
 
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()