Update app.py
Browse files
app.py
CHANGED
|
@@ -1,183 +1,43 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
import subprocess
|
| 4 |
-
import shutil
|
| 5 |
-
from pathlib import Path
|
| 6 |
-
import tempfile
|
| 7 |
-
from typing import Optional, Tuple
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
return False, "No file selected."
|
| 40 |
-
|
| 41 |
-
file_extension = Path(input_file.name).suffix[1:].lower()
|
| 42 |
-
all_formats = [fmt for sublist in self.SUPPORTED_FORMATS.values()
|
| 43 |
-
for fmt in sublist]
|
| 44 |
-
|
| 45 |
-
if file_extension not in all_formats:
|
| 46 |
-
return False, f"Unsupported input format: {file_extension}"
|
| 47 |
-
|
| 48 |
-
if os.path.getsize(input_file.name) > 100 * 1024 * 1024: # 100MB limit
|
| 49 |
-
return False, "File size exceeds 100MB limit."
|
| 50 |
-
|
| 51 |
-
return True, "File is valid."
|
| 52 |
-
|
| 53 |
-
def convert_file(self,
|
| 54 |
-
input_file: gr.File,
|
| 55 |
-
output_format: str,
|
| 56 |
-
progress: gr.Progress) -> Optional[str]:
|
| 57 |
-
"""Convert the input file to the specified format."""
|
| 58 |
-
if not self.check_libreoffice_installation():
|
| 59 |
-
return "Error: LibreOffice is not installed or accessible."
|
| 60 |
-
|
| 61 |
-
# Validate input
|
| 62 |
-
is_valid, message = self.validate_input_file(input_file)
|
| 63 |
-
if not is_valid:
|
| 64 |
-
return message
|
| 65 |
-
|
| 66 |
-
try:
|
| 67 |
-
# Update progress
|
| 68 |
-
progress(0, desc="Starting conversion...")
|
| 69 |
-
|
| 70 |
-
# Get file paths
|
| 71 |
-
input_filepath = input_file.name
|
| 72 |
-
filename = Path(input_filepath).stem
|
| 73 |
-
output_filename = f"{filename}.{output_format}"
|
| 74 |
-
output_filepath = os.path.join(self.temp_dir, output_filename)
|
| 75 |
-
|
| 76 |
-
# Convert file using LibreOffice
|
| 77 |
-
progress(0.3, desc="Converting file...")
|
| 78 |
-
command = [
|
| 79 |
-
'soffice',
|
| 80 |
-
'--headless',
|
| 81 |
-
'--convert-to', output_format,
|
| 82 |
-
input_filepath,
|
| 83 |
-
'--outdir', self.temp_dir
|
| 84 |
-
]
|
| 85 |
-
|
| 86 |
-
process = subprocess.Popen(
|
| 87 |
-
command,
|
| 88 |
-
stdout=subprocess.PIPE,
|
| 89 |
-
stderr=subprocess.PIPE
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
# Wait for conversion to complete
|
| 93 |
-
stdout, stderr = process.communicate()
|
| 94 |
-
progress(0.7, desc="Finalizing conversion...")
|
| 95 |
-
|
| 96 |
-
if process.returncode == 0 and os.path.exists(output_filepath):
|
| 97 |
-
# Copy to a new location to avoid temp directory cleanup issues
|
| 98 |
-
final_output = os.path.join(os.getcwd(), output_filename)
|
| 99 |
-
shutil.copy2(output_filepath, final_output)
|
| 100 |
-
progress(1.0, desc="Conversion complete!")
|
| 101 |
-
return final_output
|
| 102 |
-
else:
|
| 103 |
-
error_msg = stderr.decode() if stderr else "Unknown error"
|
| 104 |
-
return f"Error: Conversion failed. {error_msg}"
|
| 105 |
-
|
| 106 |
-
except Exception as e:
|
| 107 |
-
return f"Error: {str(e)}"
|
| 108 |
-
|
| 109 |
-
def create_interface():
|
| 110 |
-
converter = FileConverter()
|
| 111 |
-
|
| 112 |
-
with gr.Blocks(title="Document Converter",
|
| 113 |
-
theme=gr.themes.Soft()) as demo:
|
| 114 |
-
gr.Markdown("""
|
| 115 |
-
# Document Converter
|
| 116 |
-
Convert your documents between different formats using LibreOffice.
|
| 117 |
-
|
| 118 |
-
**Supported Format Groups:**
|
| 119 |
-
""")
|
| 120 |
-
|
| 121 |
-
# Display supported formats
|
| 122 |
-
for group, formats in FileConverter.SUPPORTED_FORMATS.items():
|
| 123 |
-
gr.Markdown(f"- **{group}**: {', '.join(formats)}")
|
| 124 |
-
|
| 125 |
-
with gr.Row():
|
| 126 |
-
with gr.Column():
|
| 127 |
-
file_input = gr.File(
|
| 128 |
-
label="Upload File (Max 100MB)",
|
| 129 |
-
file_types=list(sum([formats for formats in
|
| 130 |
-
FileConverter.SUPPORTED_FORMATS.values()], []))
|
| 131 |
-
)
|
| 132 |
-
|
| 133 |
-
# Create dropdown with format groups
|
| 134 |
-
format_dropdown = gr.Dropdown(
|
| 135 |
-
choices=[fmt for sublist in FileConverter.SUPPORTED_FORMATS.values()
|
| 136 |
-
for fmt in sublist],
|
| 137 |
-
label="Select Output Format",
|
| 138 |
-
value="pdf"
|
| 139 |
-
)
|
| 140 |
-
|
| 141 |
-
convert_button = gr.Button("Convert", variant="primary")
|
| 142 |
-
|
| 143 |
-
with gr.Column():
|
| 144 |
-
output_file = gr.File(label="Converted File")
|
| 145 |
-
status_text = gr.Textbox(label="Status", interactive=False)
|
| 146 |
-
|
| 147 |
-
# Handle conversion
|
| 148 |
convert_button.click(
|
| 149 |
-
fn=
|
| 150 |
inputs=[file_input, format_dropdown],
|
| 151 |
-
outputs=
|
| 152 |
-
show_progress=True
|
| 153 |
)
|
| 154 |
|
| 155 |
-
# Add example usage
|
| 156 |
-
gr.Examples(
|
| 157 |
-
examples=[
|
| 158 |
-
["example.docx", "pdf"],
|
| 159 |
-
["example.xlsx", "csv"],
|
| 160 |
-
["example.pptx", "pdf"]
|
| 161 |
-
],
|
| 162 |
-
inputs=[file_input, format_dropdown],
|
| 163 |
-
)
|
| 164 |
-
|
| 165 |
-
# Add usage instructions
|
| 166 |
-
gr.Markdown("""
|
| 167 |
-
### Instructions:
|
| 168 |
-
1. Upload your document using the file selector
|
| 169 |
-
2. Choose your desired output format from the dropdown
|
| 170 |
-
3. Click 'Convert' and wait for the process to complete
|
| 171 |
-
4. Download your converted file
|
| 172 |
-
|
| 173 |
-
### Notes:
|
| 174 |
-
- Requires LibreOffice to be installed on your system
|
| 175 |
-
- Maximum file size: 100MB
|
| 176 |
-
- Conversion time depends on file size and complexity
|
| 177 |
-
""")
|
| 178 |
-
|
| 179 |
return demo
|
| 180 |
|
| 181 |
if __name__ == "__main__":
|
| 182 |
-
|
| 183 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# Function to convert files using LibreOffice (soffice)
|
| 5 |
+
def convert_file(input_file, output_format):
|
| 6 |
+
# Get the absolute path to the input file
|
| 7 |
+
input_filepath = input_file.name
|
| 8 |
+
output_filename = f"{os.path.splitext(os.path.basename(input_filepath))[0]}.{output_format}"
|
| 9 |
+
output_filepath = os.path.join(os.getcwd(), output_filename)
|
| 10 |
+
|
| 11 |
+
# Run the LibreOffice command to convert the file using 'soffice'
|
| 12 |
+
command = f"soffice --headless --convert-to {output_format} \"{input_filepath}\" --outdir \"{os.getcwd()}\""
|
| 13 |
+
conversion_status = os.system(command)
|
| 14 |
+
|
| 15 |
+
# Check if the conversion was successful
|
| 16 |
+
if conversion_status == 0 and os.path.exists(output_filepath):
|
| 17 |
+
return output_filepath
|
| 18 |
+
else:
|
| 19 |
+
return "Error: Conversion failed."
|
| 20 |
+
|
| 21 |
+
# Supported formats from LibreOffice filters
|
| 22 |
+
supported_formats = [
|
| 23 |
+
"pdf", "doc", "docx", "html", "txt", "odt", "rtf", "ppt", "pptx", "xls", "xlsx"
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
# Gradio interface
|
| 27 |
+
def gradio_interface():
|
| 28 |
+
with gr.Blocks() as demo:
|
| 29 |
+
file_input = gr.File(label="Upload a file")
|
| 30 |
+
format_dropdown = gr.Dropdown(supported_formats, label="Select Output Format")
|
| 31 |
+
output_file = gr.File(label="Converted File")
|
| 32 |
+
|
| 33 |
+
convert_button = gr.Button("Convert")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
convert_button.click(
|
| 35 |
+
fn=convert_file,
|
| 36 |
inputs=[file_input, format_dropdown],
|
| 37 |
+
outputs=output_file
|
|
|
|
| 38 |
)
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
return demo
|
| 41 |
|
| 42 |
if __name__ == "__main__":
|
| 43 |
+
gradio_interface().launch()
|
|
|