Update app.py
Browse files
app.py
CHANGED
|
@@ -64,14 +64,11 @@ def unzip_and_find_jpgs(file_path):
|
|
| 64 |
def process_images(uploaded_file):
|
| 65 |
# Reinitialize the DataFrame each time this function is called
|
| 66 |
results_df = pd.DataFrame(columns=["Filename", "Extracted Text", "Translated Text"])
|
| 67 |
-
print("DataFrame reinitialized:", results_df) # Debugging statement
|
| 68 |
|
| 69 |
file_path = uploaded_file.name # Gradio provides the file path through the .name attribute
|
| 70 |
-
print("Processing file:", file_path) # Debugging statement
|
| 71 |
|
| 72 |
try:
|
| 73 |
image_files = unzip_and_find_jpgs(file_path)
|
| 74 |
-
print("Found image files:", image_files) # Debugging statement
|
| 75 |
|
| 76 |
if not image_files:
|
| 77 |
return "No JPG files found in the zip."
|
|
@@ -84,20 +81,36 @@ def process_images(uploaded_file):
|
|
| 84 |
"Translated Text": translated_text
|
| 85 |
}])
|
| 86 |
results_df = pd.concat([results_df, new_row], ignore_index=True)
|
| 87 |
-
print("Current DataFrame state:", results_df) # Debugging statement
|
| 88 |
except Exception as e:
|
| 89 |
return f"An error occurred: {str(e)}"
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
if __name__ == "__main__":
|
| 103 |
interface.launch(debug=True)
|
|
|
|
| 64 |
def process_images(uploaded_file):
|
| 65 |
# Reinitialize the DataFrame each time this function is called
|
| 66 |
results_df = pd.DataFrame(columns=["Filename", "Extracted Text", "Translated Text"])
|
|
|
|
| 67 |
|
| 68 |
file_path = uploaded_file.name # Gradio provides the file path through the .name attribute
|
|
|
|
| 69 |
|
| 70 |
try:
|
| 71 |
image_files = unzip_and_find_jpgs(file_path)
|
|
|
|
| 72 |
|
| 73 |
if not image_files:
|
| 74 |
return "No JPG files found in the zip."
|
|
|
|
| 81 |
"Translated Text": translated_text
|
| 82 |
}])
|
| 83 |
results_df = pd.concat([results_df, new_row], ignore_index=True)
|
|
|
|
| 84 |
except Exception as e:
|
| 85 |
return f"An error occurred: {str(e)}"
|
| 86 |
|
| 87 |
+
html_output = results_df.to_html()
|
| 88 |
+
|
| 89 |
+
# Save DataFrame to a temporary CSV file for download
|
| 90 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".csv") # Create a temp file
|
| 91 |
+
results_df.to_csv(temp_file.name, index=False) # Save DataFrame to CSV
|
| 92 |
+
|
| 93 |
+
# Read the contents back to verify
|
| 94 |
+
temp_file.seek(0) # Move file pointer to the beginning of the file
|
| 95 |
+
print(pd.read_csv(temp_file.name)) # Read and print the CSV file to verify its contents
|
| 96 |
+
|
| 97 |
+
temp_file.close() # Close the file
|
| 98 |
+
|
| 99 |
+
# Return HTML and the path to the CSV file
|
| 100 |
+
return html_output, temp_file.name
|
| 101 |
+
|
| 102 |
+
with gr.Blocks() as interface:
|
| 103 |
+
with gr.Row():
|
| 104 |
+
gr.Markdown("# Document AI Translation")
|
| 105 |
+
gr.Markdown("Upload a ZIP file containing JPEG/JPG images, and the system will extract and translate text from each image.")
|
| 106 |
+
with gr.Row():
|
| 107 |
+
file_input = gr.File(label="Upload ZIP File")
|
| 108 |
+
with gr.Row():
|
| 109 |
+
html_output = gr.HTML()
|
| 110 |
+
with gr.Row():
|
| 111 |
+
file_output = gr.File()
|
| 112 |
+
|
| 113 |
+
file_input.change(process_images, inputs=file_input, outputs=[html_output, file_output])
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
| 116 |
interface.launch(debug=True)
|