Marek4321 commited on
Commit
fc87be7
·
verified ·
1 Parent(s): aaab59a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -8
app.py CHANGED
@@ -7,6 +7,13 @@ import PyPDF2
7
  from docx import Document
8
  import io
9
  import tempfile
 
 
 
 
 
 
 
10
 
11
  class MultiConverter:
12
  def convert_excel_to_formatted_text(self, excel_file):
@@ -90,38 +97,70 @@ class MultiConverter:
90
 
91
  def convert_file(file):
92
  """Process uploaded file and convert it to text"""
 
 
 
 
93
  try:
 
94
  converter = MultiConverter()
95
  filename = file.name
96
  _, file_ext = os.path.splitext(filename)
97
  file_ext = file_ext.lower()
98
 
 
 
99
  # Create a temporary file
100
  with tempfile.NamedTemporaryFile(delete=False) as temp_file:
101
- temp_file.write(file.read())
 
 
102
  temp_file_path = temp_file.name
 
103
 
104
  try:
 
105
  if file_ext in [".xlsx", ".xls"]:
 
106
  result = converter.convert_excel_to_formatted_text(temp_file_path)
107
  elif file_ext in [".pptx", ".ppt"]:
 
108
  result = converter.convert_pptx_to_text(temp_file_path, filename)
109
  elif file_ext == ".pdf":
 
110
  result = converter.convert_pdf_to_text(temp_file_path, filename)
111
  elif file_ext in [".docx", ".doc"]:
 
112
  result = converter.convert_docx_to_text(temp_file_path, filename)
113
  else:
114
- result = f"Unsupported file format: {file_ext}"
115
-
 
 
 
 
116
  # Create and return file for download
117
  output_filename = os.path.splitext(filename)[0] + ".txt"
118
- return result, output_filename, result
 
 
 
 
 
 
 
 
119
  finally:
120
  # Clean up the temporary file
121
  if os.path.exists(temp_file_path):
 
122
  os.unlink(temp_file_path)
 
 
123
  except Exception as e:
124
- return f"Error converting file: {str(e)}", None, None
 
 
125
 
126
 
127
  # Create Gradio interface
@@ -132,20 +171,42 @@ with gr.Blocks(title="Multi-Format to TXT Converter") as app:
132
  with gr.Row():
133
  with gr.Column():
134
  file_input = gr.File(label="Upload a file (Excel, PowerPoint, PDF, or Word)")
135
- convert_button = gr.Button("Convert to TXT")
136
 
137
  with gr.Row():
138
  with gr.Column():
139
  text_output = gr.Textbox(label="Converted Text", lines=15)
140
  file_output = gr.File(label="Download Text File")
141
 
 
 
 
142
  # Setup the conversion process
143
  convert_button.click(
144
  fn=convert_file,
145
  inputs=[file_input],
146
- outputs=[text_output, file_output, text_output]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  )
148
 
149
  # Launch the app
150
  if __name__ == "__main__":
151
- app.launch()
 
 
 
 
 
 
7
  from docx import Document
8
  import io
9
  import tempfile
10
+ import logging
11
+
12
+ # Konfiguracja logowania
13
+ logging.basicConfig(
14
+ level=logging.INFO,
15
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
16
+ )
17
 
18
  class MultiConverter:
19
  def convert_excel_to_formatted_text(self, excel_file):
 
97
 
98
  def convert_file(file):
99
  """Process uploaded file and convert it to text"""
100
+ if file is None:
101
+ logging.warning("No file provided")
102
+ return "No file uploaded. Please select a file first.", None, None
103
+
104
  try:
105
+ logging.info(f"Converting file: {file.name}")
106
  converter = MultiConverter()
107
  filename = file.name
108
  _, file_ext = os.path.splitext(filename)
109
  file_ext = file_ext.lower()
110
 
111
+ logging.info(f"File extension: {file_ext}")
112
+
113
  # Create a temporary file
114
  with tempfile.NamedTemporaryFile(delete=False) as temp_file:
115
+ file_content = file.read()
116
+ logging.info(f"Read {len(file_content)} bytes from input file")
117
+ temp_file.write(file_content)
118
  temp_file_path = temp_file.name
119
+ logging.info(f"Created temporary file at: {temp_file_path}")
120
 
121
  try:
122
+ result = None
123
  if file_ext in [".xlsx", ".xls"]:
124
+ logging.info("Processing Excel file")
125
  result = converter.convert_excel_to_formatted_text(temp_file_path)
126
  elif file_ext in [".pptx", ".ppt"]:
127
+ logging.info("Processing PowerPoint file")
128
  result = converter.convert_pptx_to_text(temp_file_path, filename)
129
  elif file_ext == ".pdf":
130
+ logging.info("Processing PDF file")
131
  result = converter.convert_pdf_to_text(temp_file_path, filename)
132
  elif file_ext in [".docx", ".doc"]:
133
+ logging.info("Processing Word file")
134
  result = converter.convert_docx_to_text(temp_file_path, filename)
135
  else:
136
+ error_msg = f"Unsupported file format: {file_ext}"
137
+ logging.error(error_msg)
138
+ result = error_msg
139
+
140
+ logging.info(f"Conversion successful, text length: {len(result) if result else 0}")
141
+
142
  # Create and return file for download
143
  output_filename = os.path.splitext(filename)[0] + ".txt"
144
+
145
+ # Convert result to bytes for file download
146
+ if result:
147
+ file_content = result.encode('utf-8')
148
+ logging.info(f"Created output file: {output_filename}, size: {len(file_content)} bytes")
149
+ return result, (output_filename, file_content), result
150
+ else:
151
+ logging.warning("No result generated")
152
+ return "No result generated", None, None
153
  finally:
154
  # Clean up the temporary file
155
  if os.path.exists(temp_file_path):
156
+ logging.info(f"Removing temporary file: {temp_file_path}")
157
  os.unlink(temp_file_path)
158
+ else:
159
+ logging.warning(f"Temporary file not found for deletion: {temp_file_path}")
160
  except Exception as e:
161
+ error_msg = f"Error converting file: {str(e)}"
162
+ logging.exception(error_msg)
163
+ return error_msg, None, None
164
 
165
 
166
  # Create Gradio interface
 
171
  with gr.Row():
172
  with gr.Column():
173
  file_input = gr.File(label="Upload a file (Excel, PowerPoint, PDF, or Word)")
174
+ convert_button = gr.Button("Convert to TXT", variant="primary")
175
 
176
  with gr.Row():
177
  with gr.Column():
178
  text_output = gr.Textbox(label="Converted Text", lines=15)
179
  file_output = gr.File(label="Download Text File")
180
 
181
+ # Add status message
182
+ status_output = gr.Textbox(label="Status", value="Ready to convert")
183
+
184
  # Setup the conversion process
185
  convert_button.click(
186
  fn=convert_file,
187
  inputs=[file_input],
188
+ outputs=[text_output, file_output, status_output]
189
+ )
190
+
191
+ # Dodaj przykładowe pliki do demonstracji (opcjonalnie)
192
+ gr.Examples(
193
+ examples=[
194
+ ["example.xlsx"],
195
+ ["example.pdf"],
196
+ ["example.docx"],
197
+ ["example.pptx"],
198
+ ],
199
+ inputs=[file_input],
200
+ outputs=[text_output, file_output, status_output],
201
+ fn=convert_file,
202
+ cache_examples=False,
203
  )
204
 
205
  # Launch the app
206
  if __name__ == "__main__":
207
+ try:
208
+ logging.info("Starting the application")
209
+ app.launch(debug=True, share=False)
210
+ logging.info("Application stopped")
211
+ except Exception as e:
212
+ logging.exception(f"Error launching application: {str(e)}")