mkoot007 commited on
Commit
51bd59d
·
1 Parent(s): ffc468b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -33
app.py CHANGED
@@ -1,42 +1,32 @@
1
  import streamlit as st
2
- import torch
3
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
- from easyocr import Reader
5
 
6
- # Load the OCR model and explanation model
7
- ocr_reader = Reader(['en'])
8
- explainer = AutoModelForSequenceClassification.from_pretrained("bart-explainer")
9
 
10
- def extract_text(image):
11
- return ocr_reader.readtext(image)
12
 
13
- # Define a function to explain the extracted text
14
- def explain_text(text):
15
- tokenizer = AutoTokenizer.from_pretrained("bart-large")
16
- encoded_text = tokenizer(text, return_tensors="pt")
17
- explanation = explainer.generate(encoded_text)
18
- return explanation[0]
19
 
20
- # Create a Streamlit layout
21
- st.title("Text Classification Model")
 
 
 
 
22
 
23
- # Allow users to upload an image
24
- uploaded_file = st.file_uploader("Upload an image:")
 
 
25
 
26
- # Extract text from the uploaded image
27
- if uploaded_file is not None:
28
- image = torch.from_numpy(uploaded_file.read()).unsqueeze(0)
29
- extracted_text = extract_text(image)
30
 
31
- # Explain the extracted text
32
- explanation = explain_text(extracted_text)
33
 
34
- # Display the extracted text and explanation
35
- st.markdown("**Extracted text:**")
36
- st.markdown(extracted_text)
37
-
38
- st.markdown("**Explanation:**")
39
- st.markdown(explanation)
40
-
41
- else:
42
- st.markdown("Please upload an image to extract text and get an explanation.")
 
1
  import streamlit as st
2
+ import easyocr
3
+ from gtts import gTTS
4
+ from IPython.display import Audio
5
 
6
+ # Create Streamlit app title
7
+ st.title("Image Text-to-Speech App")
 
8
 
9
+ # Upload image
10
+ uploaded_image = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
11
 
12
+ if uploaded_image is not None:
13
+ image = uploaded_image.read()
 
 
 
 
14
 
15
+ # Perform OCR on the uploaded image
16
+ st.header("Text Extracted from Image:")
17
+ reader = easyocr.Reader(['en'])
18
+ result = reader.readtext(image)
19
+ extracted_text = " ".join([res[1] for res in result])
20
+ st.write(extracted_text)
21
 
22
+ # Perform text-to-speech conversion
23
+ st.header("Text-to-Speech:")
24
+ tts = gTTS(extracted_text)
25
+ tts.save("output.mp3")
26
 
27
+ # Display the audio player
28
+ st.audio("output.mp3")
 
 
29
 
30
+ # Information for the user
31
+ st.info("Upload an image, and this app will extract the text and convert it to speech.")
32