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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -23
app.py CHANGED
@@ -1,32 +1,43 @@
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
 
 
 
 
 
 
 
 
 
 
 
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 text explanation model (gpt-2 as an example)
7
+ ocr_reader = Reader(['en'])
8
+ explainer = AutoModelForSequenceClassification.from_pretrained("gpt2")
9
 
10
+ # Define a function to extract text from an image
11
+ def extract_text(image):
12
+ return ocr_reader.readtext(image)
13
 
14
+ # Define a function to explain the extracted text
15
+ def explain_text(text):
16
+ tokenizer = AutoTokenizer.from_pretrained("gpt2")
17
+ input_ids = tokenizer.encode(text, return_tensors="pt")
18
+ explanation = explainer(input_ids)
19
+ return explanation
20
 
21
+ # Create a Streamlit layout
22
+ st.title("Text Classification Model")
 
 
 
 
23
 
24
+ # Allow users to upload an image
25
+ uploaded_file = st.file_uploader("Upload an image:")
 
 
26
 
27
+ # Extract text from the uploaded image
28
+ if uploaded_file is not None:
29
+ image = torch.from_numpy(uploaded_file.read()).unsqueeze(0)
30
+ extracted_text = extract_text(image)
31
 
32
+ # Explain the extracted text
33
+ explanation = explain_text(extracted_text)
34
 
35
+ # Display the extracted text and explanation
36
+ st.markdown("**Extracted text:**")
37
+ st.markdown(extracted_text)
38
+
39
+ st.markdown("**Explanation:**")
40
+ st.markdown(explanation)
41
+
42
+ else:
43
+ st.markdown("Please upload an image to extract text and get an explanation.")