Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,24 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
-
from PIL import Image
|
4 |
-
import torch
|
5 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
6 |
-
from easyocr import Reader
|
7 |
|
8 |
-
|
9 |
-
|
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)
|
33 |
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
# Create a text2text-generation pipeline
|
5 |
+
pipe = pipeline("text2text-generation", model="kaist-ai/prometheus-13b-v1.0")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
st.title("Text Classification Model")
|
8 |
uploaded_file = st.file_uploader("Upload an image:")
|
9 |
|
10 |
if uploaded_file is not None:
|
11 |
+
# Read the uploaded image
|
12 |
image = Image.open(uploaded_file)
|
13 |
+
|
14 |
+
# Extract text from the image using OCR
|
15 |
ocr_results = extract_text(image)
|
16 |
extracted_text = " ".join([res[1] for res in ocr_results])
|
|
|
17 |
st.markdown("**Extracted text:**")
|
18 |
st.markdown(extracted_text)
|
19 |
|
20 |
+
# Generate an explanation for the extracted text using the Hugging Face pipeline
|
21 |
+
explanation = pipe(extracted_text, max_length=100, do_sample=True)[0]["generated_text"]
|
22 |
st.markdown("**Explanation:**")
|
23 |
st.markdown(explanation)
|
24 |
|