Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.")
|