wahab5763 commited on
Commit
b862961
·
verified ·
1 Parent(s): 535100e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -15
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
  from transformers import pipeline
 
3
  import requests
4
 
5
  # Load OCR model for extracting text from images
@@ -41,24 +42,29 @@ def main():
41
  uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
42
 
43
  if uploaded_file is not None:
44
- st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
45
- st.write("Processing...")
 
 
 
46
 
47
- # Extract text from image
48
- ocr_model = load_ocr_model()
49
- extracted_text = ocr_model(uploaded_file)["generated_text"]
50
 
51
- if extracted_text:
52
- st.write("### Extracted Text:")
53
- st.write(extracted_text)
54
 
55
- # Send text to ChatGPT
56
- st.write("### ChatGPT Explanation:")
57
- explanation = chat_with_gpt(extracted_text)
58
- if explanation:
59
- st.write(explanation)
60
- else:
61
- st.error("Could not extract text. Please try again with another image.")
 
 
62
 
63
  if __name__ == "__main__":
64
  main()
 
1
  import streamlit as st
2
  from transformers import pipeline
3
+ from PIL import Image
4
  import requests
5
 
6
  # Load OCR model for extracting text from images
 
42
  uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
43
 
44
  if uploaded_file is not None:
45
+ # Convert uploaded file to a PIL image
46
+ try:
47
+ image = Image.open(uploaded_file)
48
+ st.image(image, caption="Uploaded Image", use_column_width=True)
49
+ st.write("Processing...")
50
 
51
+ # Extract text from image
52
+ ocr_model = load_ocr_model()
53
+ extracted_text = ocr_model(image)[0]["generated_text"]
54
 
55
+ if extracted_text:
56
+ st.write("### Extracted Text:")
57
+ st.write(extracted_text)
58
 
59
+ # Send text to ChatGPT
60
+ st.write("### ChatGPT Explanation:")
61
+ explanation = chat_with_gpt(extracted_text)
62
+ if explanation:
63
+ st.write(explanation)
64
+ else:
65
+ st.error("Could not extract text. Please try again with another image.")
66
+ except Exception as e:
67
+ st.error(f"Error processing the image: {e}")
68
 
69
  if __name__ == "__main__":
70
  main()