mkoot007 commited on
Commit
4560624
·
1 Parent(s): d45b0ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -2,25 +2,24 @@ import streamlit as st
2
  import io
3
  from PIL import Image
4
  import torch
5
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
6
  from easyocr import Reader
7
 
8
  # Load the OCR model and text explanation model (GPT-2 as an example)
9
  ocr_reader = Reader(['en'])
10
- explainer = AutoModelForSequenceClassification.from_pretrained("gpt2")
 
11
 
12
  # Define a function to extract text from an image
13
  def extract_text(image):
14
  return ocr_reader.readtext(image)
15
 
16
- # Define a function to process OCR results and extract actual text
17
- def process_ocr_results(ocr_results):
18
- extracted_text = " ".join([res[1] for res in ocr_results])
19
- return extracted_text
20
-
21
  # Define a function to explain the extracted text
22
  def explain_text(text):
23
- explanation = "The extracted text is: " + text
 
 
 
24
  return explanation
25
 
26
  # Create a Streamlit layout
@@ -36,7 +35,7 @@ if uploaded_file is not None:
36
 
37
  # Extract text from the image
38
  ocr_results = extract_text(image)
39
- extracted_text = process_ocr_results(ocr_results)
40
 
41
  # Explain the extracted text
42
  explanation = explain_text(extracted_text)
 
2
  import io
3
  from PIL import Image
4
  import torch
5
+ from transformers import AutoTokenizer, AutoModelForCausalLM
6
  from easyocr import Reader
7
 
8
  # Load the OCR model and text explanation model (GPT-2 as an example)
9
  ocr_reader = Reader(['en'])
10
+ text_generator = AutoModelForCausalLM.from_pretrained("gpt2")
11
+ text_tokenizer = AutoTokenizer.from_pretrained("gpt2")
12
 
13
  # Define a function to extract text from an image
14
  def extract_text(image):
15
  return ocr_reader.readtext(image)
16
 
 
 
 
 
 
17
  # Define a function to explain the extracted text
18
  def explain_text(text):
19
+ # Generate an explanation using the text generation model (GPT-2)
20
+ input_ids = text_tokenizer.encode(text, return_tensors="pt")
21
+ explanation_ids = text_generator.generate(input_ids, max_length=50, num_return_sequences=1)
22
+ explanation = text_tokenizer.decode(explanation_ids[0], skip_special_tokens=True)
23
  return explanation
24
 
25
  # Create a Streamlit layout
 
35
 
36
  # Extract text from the image
37
  ocr_results = extract_text(image)
38
+ extracted_text = " ".join([res[1] for res in ocr_results])
39
 
40
  # Explain the extracted text
41
  explanation = explain_text(extracted_text)