Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,52 +1,32 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
from PIL import Image
|
3 |
-
|
|
|
4 |
from easyocr import Reader
|
5 |
|
6 |
-
# Initialize the image-to-text model
|
7 |
-
image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large")
|
8 |
|
9 |
-
# Load the OCR model and text explanation model
|
10 |
ocr_reader = Reader(['en'])
|
11 |
-
|
12 |
-
|
13 |
-
text_generator = AutoModelForCausalLM.from_pretrained("HuggingFaceH4/zephyr-7b-alpha")
|
14 |
-
text_tokenizer = AutoTokenizer.from_pretrained("HuggingFaceH4/zephyr-7b-alpha")
|
15 |
-
|
16 |
-
# Define a function to extract text from an image using OCR
|
17 |
def extract_text(image):
|
18 |
return ocr_reader.readtext(image)
|
19 |
-
|
20 |
-
|
21 |
-
def explain_text(text, text_generator, text_tokenizer):
|
22 |
-
# Extracted text
|
23 |
-
extracted_text = " ".join([res[1] for res in text])
|
24 |
-
|
25 |
-
# Generate an explanation using the text explanation model
|
26 |
-
input_ids = text_tokenizer.encode(extracted_text, return_tensors="pt")
|
27 |
explanation_ids = text_generator.generate(input_ids, max_length=100, num_return_sequences=1)
|
28 |
explanation = text_tokenizer.decode(explanation_ids[0], skip_special_tokens=True)
|
29 |
-
|
30 |
return explanation
|
31 |
|
32 |
-
|
33 |
-
st.title("Text Extraction and Explanation")
|
34 |
-
|
35 |
-
# Allow users to upload an image
|
36 |
uploaded_file = st.file_uploader("Upload an image:")
|
37 |
|
38 |
-
# Extract text from the uploaded image and explain it
|
39 |
if uploaded_file is not None:
|
40 |
image = Image.open(uploaded_file)
|
41 |
ocr_results = extract_text(image)
|
42 |
-
|
43 |
-
explanation = explain_text(
|
44 |
-
|
45 |
st.markdown("**Extracted text:**")
|
46 |
-
st.markdown(
|
47 |
-
|
48 |
-
st.markdown("**Image Caption:**")
|
49 |
-
st.markdown(image_caption[0]['caption']) # Display the image caption
|
50 |
|
51 |
st.markdown("**Explanation:**")
|
52 |
st.markdown(explanation)
|
|
|
1 |
import streamlit as st
|
2 |
+
import io
|
3 |
from PIL import Image
|
4 |
+
import torch
|
5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
6 |
from easyocr import Reader
|
7 |
|
|
|
|
|
8 |
|
|
|
9 |
ocr_reader = Reader(['en'])
|
10 |
+
text_generator = AutoModelForCausalLM.from_pretrained("gpt2")
|
11 |
+
text_tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
|
|
|
|
|
|
|
|
12 |
def extract_text(image):
|
13 |
return ocr_reader.readtext(image)
|
14 |
+
def explain_text(text):
|
15 |
+
input_ids = text_tokenizer.encode(text, return_tensors="pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
explanation_ids = text_generator.generate(input_ids, max_length=100, num_return_sequences=1)
|
17 |
explanation = text_tokenizer.decode(explanation_ids[0], skip_special_tokens=True)
|
|
|
18 |
return explanation
|
19 |
|
20 |
+
st.title("Text Classification Model")
|
|
|
|
|
|
|
21 |
uploaded_file = st.file_uploader("Upload an image:")
|
22 |
|
|
|
23 |
if uploaded_file is not None:
|
24 |
image = Image.open(uploaded_file)
|
25 |
ocr_results = extract_text(image)
|
26 |
+
extracted_text = " ".join([res[1] for res in ocr_results])
|
27 |
+
explanation = explain_text(extracted_text)
|
|
|
28 |
st.markdown("**Extracted text:**")
|
29 |
+
st.markdown(extracted_text)
|
|
|
|
|
|
|
30 |
|
31 |
st.markdown("**Explanation:**")
|
32 |
st.markdown(explanation)
|