Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
st.title("OCR Extraction Client")
|
| 5 |
-
|
| 6 |
st.write(
|
| 7 |
"""
|
| 8 |
This app lets you upload a PDF or image file. The file is sent to a FastAPI endpoint for OCR extraction,
|
|
@@ -10,26 +10,39 @@ st.write(
|
|
| 10 |
"""
|
| 11 |
)
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
if uploaded_file is not None:
|
| 16 |
st.info(f"Processing file: **{uploaded_file.name}**")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
with st.spinner("Sending file to OCR service..."):
|
| 18 |
-
# Prepare the file payload.
|
| 19 |
-
files = {"file": (uploaded_file.name, uploaded_file, uploaded_file.type)}
|
| 20 |
-
|
| 21 |
-
# Replace the URL below with your FastAPI endpoint URL.
|
| 22 |
api_url = "https://hammad712-urdu-ocr-app.hf.space/upload"
|
| 23 |
response = requests.post(api_url, files=files)
|
| 24 |
|
| 25 |
if response.status_code == 200:
|
| 26 |
st.success("OCR extraction complete!")
|
| 27 |
-
|
| 28 |
-
# Get the returned Markdown content.
|
| 29 |
md_content = response.content.decode("utf-8")
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
st.download_button(
|
| 35 |
label="Download Markdown File",
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
+
from PIL import Image
|
| 4 |
|
| 5 |
st.title("OCR Extraction Client")
|
|
|
|
| 6 |
st.write(
|
| 7 |
"""
|
| 8 |
This app lets you upload a PDF or image file. The file is sent to a FastAPI endpoint for OCR extraction,
|
|
|
|
| 10 |
"""
|
| 11 |
)
|
| 12 |
|
| 13 |
+
# Sidebar for uploading the document and processing
|
| 14 |
+
st.sidebar.header("Upload Document")
|
| 15 |
+
uploaded_file = st.sidebar.file_uploader("Upload a PDF or image file", type=["pdf", "png", "jpg", "jpeg", "webp"])
|
| 16 |
+
process_button = st.sidebar.button("Process Document")
|
| 17 |
|
| 18 |
+
if uploaded_file is not None and process_button:
|
| 19 |
st.info(f"Processing file: **{uploaded_file.name}**")
|
| 20 |
+
|
| 21 |
+
# If the file is an image, display it
|
| 22 |
+
if uploaded_file.name.lower().endswith(('.png', '.jpg', '.jpeg', '.webp')):
|
| 23 |
+
try:
|
| 24 |
+
image = Image.open(uploaded_file)
|
| 25 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 26 |
+
except Exception as e:
|
| 27 |
+
st.error(f"Error displaying image: {e}")
|
| 28 |
+
|
| 29 |
with st.spinner("Sending file to OCR service..."):
|
| 30 |
+
# Prepare the file payload using getvalue() to obtain file bytes.
|
| 31 |
+
files = {"file": (uploaded_file.name, uploaded_file.getvalue(), uploaded_file.type)}
|
|
|
|
|
|
|
| 32 |
api_url = "https://hammad712-urdu-ocr-app.hf.space/upload"
|
| 33 |
response = requests.post(api_url, files=files)
|
| 34 |
|
| 35 |
if response.status_code == 200:
|
| 36 |
st.success("OCR extraction complete!")
|
|
|
|
|
|
|
| 37 |
md_content = response.content.decode("utf-8")
|
| 38 |
|
| 39 |
+
# Display output based on file type.
|
| 40 |
+
if uploaded_file.name.lower().endswith(('.png', '.jpg', '.jpeg', '.webp')):
|
| 41 |
+
st.markdown("### Extracted Text from Image")
|
| 42 |
+
st.markdown(md_content)
|
| 43 |
+
else:
|
| 44 |
+
st.markdown("### Extracted Markdown Text")
|
| 45 |
+
st.markdown(md_content)
|
| 46 |
|
| 47 |
st.download_button(
|
| 48 |
label="Download Markdown File",
|