ugolefoo commited on
Commit
aa9b7b4
Β·
verified Β·
1 Parent(s): 4721608

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -13
app.py CHANGED
@@ -4,8 +4,6 @@ import pytesseract
4
  import requests
5
  import pandas as pd
6
  import gradio as gr
7
- import io
8
- from io import BytesIO
9
 
10
  # ──────────────────────────────────────────────────────────────
11
  # 1. Utility: Detect rectangular contours (approximate book covers)
@@ -100,7 +98,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, BytesIO) tuple for CSV.
104
  """
105
  img = np.array(image_file)[:, :, ::-1].copy() # PIL to OpenCV BGR
106
  boxes = detect_book_regions(img)
@@ -128,19 +126,15 @@ def process_image(image_file):
128
  }
129
  )
130
 
 
131
  if not records:
132
  df_empty = pd.DataFrame(columns=["title", "author_name", "publisher", "first_publish_year"])
133
- # Build an empty CSV bytes buffer
134
- empty_csv = df_empty.to_csv(index=False).encode()
135
- buffer = io.BytesIO(empty_csv)
136
- buffer.name = "books.csv"
137
- return df_empty, buffer
138
 
139
  df = pd.DataFrame(records)
140
  csv_bytes = df.to_csv(index=False).encode()
141
- buffer = io.BytesIO(csv_bytes)
142
- buffer.name = "books.csv"
143
- return df, buffer
144
 
145
  # ──────────────────────────────────────────────────────────────
146
  # 5. Build the Gradio Interface
@@ -168,8 +162,9 @@ def build_interface():
168
  download_file = gr.File(label="Download CSV")
169
 
170
  def on_run(image):
171
- df, file_buffer = process_image(image)
172
- return df, file_buffer
 
173
 
174
  run_button.click(
175
  fn=on_run,
@@ -181,4 +176,5 @@ def build_interface():
181
 
182
  if __name__ == "__main__":
183
  demo_app = build_interface()
 
184
  demo_app.launch()
 
4
  import requests
5
  import pandas as pd
6
  import gradio as gr
 
 
7
 
8
  # ──────────────────────────────────────────────────────────────
9
  # 1. Utility: Detect rectangular contours (approximate book covers)
 
98
  def process_image(image_file):
99
  """
100
  Gradio passes a PIL image or numpy array. Convert to OpenCV BGR, detect covers β†’ OCR β†’ OpenLibrary.
101
+ Return a DataFrame and CSV bytes (as raw bytes).
102
  """
103
  img = np.array(image_file)[:, :, ::-1].copy() # PIL to OpenCV BGR
104
  boxes = detect_book_regions(img)
 
126
  }
127
  )
128
 
129
+ # Build DataFrame
130
  if not records:
131
  df_empty = pd.DataFrame(columns=["title", "author_name", "publisher", "first_publish_year"])
132
+ csv_bytes = df_empty.to_csv(index=False).encode()
133
+ return df_empty, csv_bytes
 
 
 
134
 
135
  df = pd.DataFrame(records)
136
  csv_bytes = df.to_csv(index=False).encode()
137
+ return df, csv_bytes
 
 
138
 
139
  # ──────────────────────────────────────────────────────────────
140
  # 5. Build the Gradio Interface
 
162
  download_file = gr.File(label="Download CSV")
163
 
164
  def on_run(image):
165
+ df, csv_bytes = process_image(image)
166
+ # Return DataFrame plus a dict that gr.File understands:
167
+ return df, {"name": "books.csv", "data": csv_bytes}
168
 
169
  run_button.click(
170
  fn=on_run,
 
176
 
177
  if __name__ == "__main__":
178
  demo_app = build_interface()
179
+ # You can add share=True if you want a public link; otherwise this is fine:
180
  demo_app.launch()