ugolefoo commited on
Commit
bbc77e0
Β·
verified Β·
1 Parent(s): 0475645

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -4
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
- Return a DataFrame and a (filename, bytes) tuple for CSV.
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
- return df, ("books.csv", csv_bytes)
 
 
 
 
 
 
 
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, file_tuple = process_image(image)
163
- return df, file_tuple
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,