Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,8 @@ import pytesseract
|
|
| 4 |
import requests
|
| 5 |
import pandas as pd
|
| 6 |
import gradio as gr
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 9 |
# 1. Utility: Detect rectangular contours (approximate book covers)
|
|
@@ -100,7 +102,7 @@ def query_openlibrary(title_text: str, author_text: str = None):
|
|
| 100 |
def process_image(image_file):
|
| 101 |
"""
|
| 102 |
Gradio passes a PIL image or numpy array. Convert to OpenCV BGR, detect covers β OCR β OpenLibrary.
|
| 103 |
-
|
| 104 |
"""
|
| 105 |
img = np.array(image_file)[:, :, ::-1].copy() # PIL to OpenCV BGR
|
| 106 |
boxes = detect_book_regions(img)
|
|
@@ -131,7 +133,14 @@ def process_image(image_file):
|
|
| 131 |
# Build DataFrame (even if empty)
|
| 132 |
df = pd.DataFrame(records, columns=["title", "author_name", "publisher", "first_publish_year"])
|
| 133 |
csv_bytes = df.to_csv(index=False).encode()
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 137 |
# 5. Build the Gradio Interface
|
|
@@ -159,8 +168,8 @@ def build_interface():
|
|
| 159 |
download_file = gr.File(label="Download CSV")
|
| 160 |
|
| 161 |
def on_run(image):
|
| 162 |
-
df,
|
| 163 |
-
return df,
|
| 164 |
|
| 165 |
run_button.click(
|
| 166 |
fn=on_run,
|
|
|
|
| 4 |
import requests
|
| 5 |
import pandas as pd
|
| 6 |
import gradio as gr
|
| 7 |
+
import uuid
|
| 8 |
+
import os
|
| 9 |
|
| 10 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 11 |
# 1. Utility: Detect rectangular contours (approximate book covers)
|
|
|
|
| 102 |
def process_image(image_file):
|
| 103 |
"""
|
| 104 |
Gradio passes a PIL image or numpy array. Convert to OpenCV BGR, detect covers β OCR β OpenLibrary.
|
| 105 |
+
Write CSV to a temp file and return (DataFrame, filepath).
|
| 106 |
"""
|
| 107 |
img = np.array(image_file)[:, :, ::-1].copy() # PIL to OpenCV BGR
|
| 108 |
boxes = detect_book_regions(img)
|
|
|
|
| 133 |
# Build DataFrame (even if empty)
|
| 134 |
df = pd.DataFrame(records, columns=["title", "author_name", "publisher", "first_publish_year"])
|
| 135 |
csv_bytes = df.to_csv(index=False).encode()
|
| 136 |
+
|
| 137 |
+
# Write to a unique temporary file
|
| 138 |
+
unique_name = f"books_{uuid.uuid4().hex}.csv"
|
| 139 |
+
temp_path = os.path.join("/tmp", unique_name)
|
| 140 |
+
with open(temp_path, "wb") as f:
|
| 141 |
+
f.write(csv_bytes)
|
| 142 |
+
|
| 143 |
+
return df, temp_path
|
| 144 |
|
| 145 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 146 |
# 5. Build the Gradio Interface
|
|
|
|
| 168 |
download_file = gr.File(label="Download CSV")
|
| 169 |
|
| 170 |
def on_run(image):
|
| 171 |
+
df, filepath = process_image(image)
|
| 172 |
+
return df, filepath
|
| 173 |
|
| 174 |
run_button.click(
|
| 175 |
fn=on_run,
|