Spaces:
Sleeping
Sleeping
Update app.py
Browse files6 unmodified lines7891011121314151617181977 unmodified lines9798991001011021031041051061071081091101111121131141151161011171181191201211221231241251261271281291301311321331341351141151361371381391401411421431181441451461471481491501511521531541551561571581591601241611621631641651664 unmodified lines1711721731351741751761771781791801811821831841851861871461881891901911921931941951961971981992002012022032042052061512072082092102112126 unmodified linesfrom docx import Documentimport ioimport tempfileimport logging
# Konfiguracja logowanialogging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
class MultiConverter: def convert_excel_to_formatted_text(self, excel_file):77 unmodified lines
def convert_file(file): """Process uploaded file and convert it to text""" if file is None: logging.warning("No file provided") return "No file uploaded. Please select a file first.", None, None try: logging.info(f"Converting file: {file.name}") converter = MultiConverter() filename = file.name _, file_ext = os.path.splitext(filename) file_ext = file_ext.lower() logging.info(f"File extension: {file_ext}") # Create a temporary file with tempfile.NamedTemporaryFile(delete=False) as temp_file: file_content = file.read() logging.info(f"Read {len(file_content)} bytes from input file") temp_file.write(file.read()) temp_file.write(file_content) temp_file_path = temp_file.name logging.info(f"Created temporary file at: {temp_file_path}") try: result = None if file_ext in [".xlsx", ".xls"]: logging.info("Processing Excel file") result = converter.convert_excel_to_formatted_text(temp_file_path) elif file_ext in [".pptx", ".ppt"]: logging.info("Processing PowerPoint file") result = converter.convert_pptx_to_text(temp_file_path, filename) elif file_ext == ".pdf": logging.info("Processing PDF file") result = converter.convert_pdf_to_text(temp_file_path, filename) elif file_ext in [".docx", ".doc"]: logging.info("Processing Word file") result = converter.convert_docx_to_text(temp_file_path, filename) else: result = f"Unsupported file format: {file_ext}" error_msg = f"Unsupported file format: {file_ext}" logging.error(error_msg) result = error_msg logging.info(f"Conversion successful, text length: {len(result) if result else 0}") # Create and return file for download output_filename = os.path.splitext(filename)[0] + ".txt" return result, output_filename, result # Convert result to bytes for file download if result: file_content = result.encode('utf-8') logging.info(f"Created output file: {output_filename}, size: {len(file_content)} bytes") return result, (output_filename, file_content), result else: logging.warning("No result generated") return "No result generated", None, None finally: # Clean up the temporary file if os.path.exists(temp_file_path): logging.info(f"Removing temporary file: {temp_file_path}") os.unlink(temp_file_path) else: logging.warning(f"Temporary file not found for deletion: {temp_file_path}") except Exception as e: return f"Error converting file: {str(e)}", None, None error_msg = f"Error converting file: {str(e)}" logging.exception(error_msg) return error_msg, None, None
# Create Gradio interface4 unmodified lines with gr.Row(): with gr.Column(): file_input = gr.File(label="Upload a file (Excel, PowerPoint, PDF, or Word)") convert_button = gr.Button("Convert to TXT") convert_button = gr.Button("Convert to TXT", variant="primary") with gr.Row(): with gr.Column(): text_output = gr.Textbox(label="Converted Text", lines=15) file_output = gr.File(label="Download Text File") # Add status message status_output = gr.Textbox(label="Status", value="Ready to convert") # Setup the conversion process convert_button.click( fn=convert_file, inputs=[file_input], outputs=[text_output, file_output, text_output] outputs=[text_output, file_output, status_output] ) # Dodaj przykładowe pliki do demonstracji (opcjonalnie) gr.Examples( examples=[ ["example.xlsx"], ["example.pdf"], ["example.docx"], ["example.pptx"], ], inputs=[file_input], outputs=[text_output, file_output, status_output], fn=convert_file, cache_examples=False, )
# Launch the appif __name__ == "__main__": app.launch() try: logging.info("Starting the application") app.launch(debug=True, share=False) logging.info("Application stopped") except Exception as e: logging.exception(f"Error launching application: {str(e)}")app.py+69-8