mkaramb commited on
Commit
1346633
·
verified ·
1 Parent(s): 3b55d2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -58,17 +58,20 @@ def unzip_and_find_jpgs(file_path):
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
  else:
65
- results_df = pd.DataFrame(columns=["Filename", "Extracted Text", "Translated Text"]) # Reinitialize the DataFrame
 
66
 
67
  file_path = uploaded_file.name # Gradio provides the file path through the .name attribute
68
  try:
69
  image_files = unzip_and_find_jpgs(file_path)
70
  if not image_files:
71
- return "No JPG files found in the zip."
 
72
  for file_path in image_files:
73
  extracted_text, translated_text = batch_process_documents(file_path, "image/jpeg")
74
  new_row = pd.DataFrame([{
@@ -78,12 +81,16 @@ def process_images(uploaded_file):
78
  }])
79
  results_df = pd.concat([results_df, new_row], ignore_index=True)
80
  except Exception as e:
81
- return f"An error occurred: {str(e)}"
 
82
  html_output = results_df.to_html()
83
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".csv")
84
  results_df.to_csv(temp_file.name, index=False)
85
- temp_file.close()
86
- return html_output, temp_file.name
 
 
 
87
 
88
  with gr.Blocks() as interface:
89
  with gr.Row():
 
58
 
59
  def process_images(uploaded_file):
60
  global results_df
61
+ # Check if a file is actually uploaded
62
  if uploaded_file is None:
63
+ results_df = pd.DataFrame(columns=["Filename", "Extracted Text", "Translated Text"]) # Clear DataFrame
64
  return "", "" # Return empty outputs if no file is uploaded
65
  else:
66
+ # Reinitialize the DataFrame every time a new file is uploaded
67
+ results_df = pd.DataFrame(columns=["Filename", "Extracted Text", "Translated Text"])
68
 
69
  file_path = uploaded_file.name # Gradio provides the file path through the .name attribute
70
  try:
71
  image_files = unzip_and_find_jpgs(file_path)
72
  if not image_files:
73
+ return "No JPG files found in the zip.", ""
74
+
75
  for file_path in image_files:
76
  extracted_text, translated_text = batch_process_documents(file_path, "image/jpeg")
77
  new_row = pd.DataFrame([{
 
81
  }])
82
  results_df = pd.concat([results_df, new_row], ignore_index=True)
83
  except Exception as e:
84
+ return f"An error occurred: {str(e)}", ""
85
+
86
  html_output = results_df.to_html()
87
+ temp_file = tempfile.NamedTemporaryFile(delete=True, suffix=".csv")
88
  results_df.to_csv(temp_file.name, index=False)
89
+ temp_file_path = temp_file.name
90
+ temp_file.close() # Ensure the file is closed after writing
91
+
92
+ return html_output, temp_file_path
93
+
94
 
95
  with gr.Blocks() as interface:
96
  with gr.Row():