Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,59 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from
|
| 3 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
def load_ocr_model():
|
| 8 |
-
return pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
return pipeline("text-generation", model="EleutherAI/gpt-neo-1.3B") # Use a smaller model for better compatibility
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
try:
|
| 18 |
-
if len(prompt) > 512:
|
| 19 |
-
prompt = prompt[:512] + "..." # Truncate long prompts
|
| 20 |
-
llm_model = load_text_model()
|
| 21 |
-
response = llm_model(prompt, max_length=500, do_sample=True, temperature=0.7)
|
| 22 |
-
if response and len(response) > 0:
|
| 23 |
-
return response[0]["generated_text"]
|
| 24 |
-
else:
|
| 25 |
-
return "No explanation or completion could be generated. Please try again with a different input."
|
| 26 |
-
except Exception as e:
|
| 27 |
-
return f"Error generating explanation: {e}"
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
and get explanations or text completions using a GPT-style open-source model.
|
| 36 |
-
"""
|
| 37 |
-
)
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
try:
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
result = ocr_model(image)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
st.write("### Extracted Text:")
|
| 55 |
-
st.write(f"`{extracted_text}`") # Display the extracted text in a readable format
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
explanation = process_with_llm(extracted_text)
|
| 60 |
-
st.write(explanation)
|
| 61 |
-
else:
|
| 62 |
-
st.error("No text could be extracted. Please try another image.")
|
| 63 |
-
except Exception as e:
|
| 64 |
-
st.error(f"Error processing the image: {e}")
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from paddleocr import PaddleOCR
|
| 3 |
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
import re
|
| 6 |
+
from pyngrok import ngrok
|
| 7 |
+
import subprocess
|
| 8 |
|
| 9 |
+
# Initialize PaddleOCR
|
| 10 |
+
ocr = PaddleOCR(use_angle_cls=True, lang='en') # Enable angle classification for better accuracy
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Streamlit App
|
| 13 |
+
st.title("Real-Time Text Extraction from Images (PaddleOCR)")
|
| 14 |
+
st.markdown("Upload or capture an image to extract text using PaddleOCR.")
|
|
|
|
| 15 |
|
| 16 |
+
# Upload Image
|
| 17 |
+
uploaded_file = st.file_uploader("Upload Image", type=['png', 'jpg', 'jpeg'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
st.markdown("OR")
|
| 20 |
+
|
| 21 |
+
# Capture Image
|
| 22 |
+
captured_image = st.camera_input("Capture Image")
|
| 23 |
+
|
| 24 |
+
image = None # Placeholder for the image
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
if uploaded_file is not None:
|
| 27 |
+
# Open and Display the Uploaded Image
|
| 28 |
+
image = Image.open(uploaded_file)
|
| 29 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 30 |
+
elif captured_image is not None:
|
| 31 |
+
# Open and Display the Captured Image
|
| 32 |
+
image = Image.open(captured_image)
|
| 33 |
+
st.image(image, caption="Captured Image", use_container_width=True)
|
| 34 |
|
| 35 |
+
if image is not None:
|
| 36 |
+
# Convert image to numpy array
|
| 37 |
+
image_np = np.array(image)
|
| 38 |
+
|
| 39 |
+
# Perform OCR with PaddleOCR
|
| 40 |
+
with st.spinner("Extracting text..."):
|
| 41 |
try:
|
| 42 |
+
# Extract text from the image
|
| 43 |
+
results = ocr.ocr(image_np, cls=True)
|
| 44 |
+
extracted_text = " ".join([line[1][0] for line in results[0]]) # Concatenate recognized text
|
| 45 |
|
| 46 |
+
# Clean the extracted text: replace tabs or multiple spaces with a single space
|
| 47 |
+
cleaned_text = re.sub(r'\s+', ' ', extracted_text).strip()
|
|
|
|
| 48 |
|
| 49 |
+
# Add HTML <br> tags for line breaks after numbers
|
| 50 |
+
formatted_text = re.sub(r'(\b\d+\b)', r'\1<br>', cleaned_text)
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
# Add line breaks for table columns or box-separated text
|
| 53 |
+
formatted_text = re.sub(r'[\t|]', r'<br>', formatted_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
# Display the formatted text with HTML rendering
|
| 56 |
+
st.subheader("Extracted Text:")
|
| 57 |
+
st.markdown(formatted_text, unsafe_allow_html=True) # Use unsafe_allow_html=True to render HTML
|
| 58 |
+
except Exception as e:
|
| 59 |
+
st.error(f"Error during text extraction: {e}")
|