mkaramb commited on
Commit
88a3c99
·
verified ·
1 Parent(s): 60d2516

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -13
app.py CHANGED
@@ -58,16 +58,14 @@ def unzip_and_find_jpgs(file_path):
58
 
59
  def process_images(uploaded_file):
60
  global results_df
61
- results_df = results_df.iloc[0:0] # Clear the DataFrame if re-running this cell
62
-
 
63
  file_path = uploaded_file.name # Gradio provides the file path through the .name attribute
64
-
65
  try:
66
  image_files = unzip_and_find_jpgs(file_path)
67
-
68
  if not image_files:
69
  return "No JPG files found in the zip."
70
-
71
  for file_path in image_files:
72
  extracted_text, translated_text = batch_process_documents(file_path, "image/jpeg")
73
  new_row = pd.DataFrame([{
@@ -78,15 +76,10 @@ def process_images(uploaded_file):
78
  results_df = pd.concat([results_df, new_row], ignore_index=True)
79
  except Exception as e:
80
  return f"An error occurred: {str(e)}"
81
-
82
  html_output = results_df.to_html()
83
-
84
- # Save DataFrame to a temporary CSV file for download
85
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".csv") # Create a temp file
86
- results_df.to_csv(temp_file.name, index=False) # Save DataFrame to CSV
87
- temp_file.close() # Close the file
88
-
89
- # Return HTML and the path to the CSV file
90
  return html_output, temp_file.name
91
 
92
  with gr.Blocks() as interface:
 
58
 
59
  def process_images(uploaded_file):
60
  global results_df
61
+ if uploaded_file is None:
62
+ results_df = results_df.iloc[0:0] # Clear the DataFrame
63
+ return "", "" # Return empty outputs if no file is uploaded
64
  file_path = uploaded_file.name # Gradio provides the file path through the .name attribute
 
65
  try:
66
  image_files = unzip_and_find_jpgs(file_path)
 
67
  if not image_files:
68
  return "No JPG files found in the zip."
 
69
  for file_path in image_files:
70
  extracted_text, translated_text = batch_process_documents(file_path, "image/jpeg")
71
  new_row = pd.DataFrame([{
 
76
  results_df = pd.concat([results_df, new_row], ignore_index=True)
77
  except Exception as e:
78
  return f"An error occurred: {str(e)}"
 
79
  html_output = results_df.to_html()
80
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".csv")
81
+ results_df.to_csv(temp_file.name, index=False)
82
+ temp_file.close()
 
 
 
 
83
  return html_output, temp_file.name
84
 
85
  with gr.Blocks() as interface: