Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,43 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
-
from
|
4 |
-
from
|
5 |
|
6 |
-
#
|
7 |
-
|
|
|
8 |
|
9 |
-
#
|
10 |
-
|
|
|
11 |
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
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 |
-
|
23 |
-
|
24 |
-
tts = gTTS(extracted_text)
|
25 |
-
tts.save("output.mp3")
|
26 |
|
27 |
-
|
28 |
-
|
|
|
|
|
29 |
|
30 |
-
#
|
31 |
-
|
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.")
|