|
import gradio as gr |
|
import os |
|
|
|
|
|
def convert_file(input_file, output_format): |
|
|
|
input_filepath = input_file.name |
|
output_filename = f"{os.path.splitext(os.path.basename(input_filepath))[0]}.{output_format}" |
|
output_filepath = os.path.join(os.getcwd(), output_filename) |
|
|
|
|
|
command = f"soffice --headless --convert-to {output_format} \"{input_filepath}\" --outdir \"{os.getcwd()}\"" |
|
conversion_status = os.system(command) |
|
|
|
|
|
if conversion_status == 0 and os.path.exists(output_filepath): |
|
return output_filepath |
|
else: |
|
return "Error: Conversion failed." |
|
|
|
|
|
supported_formats = [ |
|
"pdf", "doc", "docx", "html", "txt", "odt", "rtf", "ppt", "pptx", "xls", "xlsx" |
|
] |
|
|
|
|
|
def gradio_interface(): |
|
with gr.Blocks() as demo: |
|
file_input = gr.File(label="Upload a file") |
|
format_dropdown = gr.Dropdown(supported_formats, label="Select Output Format") |
|
output_file = gr.File(label="Converted File") |
|
|
|
convert_button = gr.Button("Convert") |
|
convert_button.click( |
|
fn=convert_file, |
|
inputs=[file_input, format_dropdown], |
|
outputs=output_file |
|
) |
|
|
|
return demo |
|
|
|
if __name__ == "__main__": |
|
gradio_interface().launch() |