mkoot007 commited on
Commit
afd4bc8
·
1 Parent(s): ccaa1a2

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -45
app.py DELETED
@@ -1,45 +0,0 @@
1
- import streamlit as st
2
- from PIL import Image
3
- import io
4
- from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModelForSeq2SeqLM
5
- from easyocr import Reader
6
-
7
- ocr_reader = Reader(['en'])
8
- text_generator = AutoModelForCausalLM.from_pretrained("bart")
9
- text_tokenizer = AutoTokenizer.from_pretrained("bart")
10
- explainer = AutoModelForSeq2SeqLM.from_pretrained("bart-explainer")
11
-
12
- def extract_text(image):
13
- return ocr_reader.readtext(image)
14
-
15
- def explain_text(text, explainer, text_tokenizer):
16
- # Extracted text
17
- extracted_text = " ".join([res[1] for res in text])
18
-
19
- # Generate an explanation using the text explanation model
20
- input_ids = text_tokenizer.encode(extracted_text, return_tensors="pt")
21
- explanation_ids = explainer.generate(input_ids, max_length=100, num_return_sequences=1)
22
- explanation = text_tokenizer.decode(explanation_ids[0], skip_special_tokens=True)
23
-
24
- return explanation
25
-
26
- # Create a Streamlit layout
27
- st.title("Text Extraction and Explanation")
28
-
29
- # Allow users to upload an image
30
- uploaded_file = st.file_uploader("Upload an image:")
31
-
32
- # Extract text from the uploaded image and explain it
33
- if uploaded_file is not None:
34
- image = Image.open(uploaded_file)
35
- ocr_results = extract_text(image)
36
- explanation = explain_text(ocr_results, explainer, text_tokenizer)
37
-
38
- st.markdown("**Extracted text:**")
39
- st.markdown(" ".join([res[1] for res in ocr_results]))
40
-
41
- st.markdown("**Explanation:**")
42
- st.markdown(explanation)
43
-
44
- else:
45
- st.markdown("Please upload an image to extract text and get an explanation.")