Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import os
|
|
| 4 |
import subprocess
|
| 5 |
import tempfile
|
| 6 |
from pathlib import Path
|
| 7 |
-
from typing import
|
| 8 |
|
| 9 |
# Document Converter Class
|
| 10 |
class DocumentConverter:
|
|
@@ -51,38 +51,37 @@ def convert_exe_to_doc(input_path: str, output_name: str = "output.doc") -> str:
|
|
| 51 |
except Exception as e:
|
| 52 |
raise RuntimeError(f"EXE conversion failed: {str(e)}")
|
| 53 |
|
| 54 |
-
|
| 55 |
-
def update_ui(file_info: Dict) -> Dict:
|
| 56 |
"""Update UI components based on file type"""
|
| 57 |
if not file_info:
|
| 58 |
-
return
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
filename = file_info.name.lower()
|
| 65 |
if filename.endswith('.exe'):
|
| 66 |
-
return
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
else:
|
| 72 |
ext = Path(filename).suffix.lower()
|
| 73 |
doc_converter = DocumentConverter()
|
| 74 |
formats = doc_converter.supported_conversions.get(ext, [])
|
| 75 |
-
return
|
| 76 |
-
|
| 77 |
visible=bool(formats),
|
| 78 |
-
choices=[f.upper() for f in formats],
|
| 79 |
value=formats[0][1:].upper() if formats else None
|
| 80 |
),
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
|
| 85 |
-
def convert_file(file_info:
|
| 86 |
"""Handle file conversion"""
|
| 87 |
if not file_info:
|
| 88 |
raise gr.Error("Please upload a file first")
|
|
@@ -93,7 +92,6 @@ def convert_file(file_info: Dict, output_format: str, exe_output_name: str) -> T
|
|
| 93 |
output_path = convert_exe_to_doc(file_info.name, exe_output_name)
|
| 94 |
return output_path, "EXE to DOC conversion successful!"
|
| 95 |
else:
|
| 96 |
-
ext = Path(filename).suffix.lower()
|
| 97 |
if not output_format:
|
| 98 |
raise gr.Error("Please select an output format")
|
| 99 |
output_path = DocumentConverter().convert_document(file_info.name, f".{output_format.lower()}")
|
|
@@ -128,11 +126,7 @@ with gr.Blocks(title="Universal Converter") as app:
|
|
| 128 |
file_input.change(
|
| 129 |
update_ui,
|
| 130 |
inputs=file_input,
|
| 131 |
-
outputs=
|
| 132 |
-
"format_dropdown": format_dropdown,
|
| 133 |
-
"exe_name_input": exe_name_input,
|
| 134 |
-
"convert_btn": convert_btn
|
| 135 |
-
}
|
| 136 |
)
|
| 137 |
|
| 138 |
# Handle conversion
|
|
|
|
| 4 |
import subprocess
|
| 5 |
import tempfile
|
| 6 |
from pathlib import Path
|
| 7 |
+
from typing import List
|
| 8 |
|
| 9 |
# Document Converter Class
|
| 10 |
class DocumentConverter:
|
|
|
|
| 51 |
except Exception as e:
|
| 52 |
raise RuntimeError(f"EXE conversion failed: {str(e)}")
|
| 53 |
|
| 54 |
+
def update_ui(file_info: gr.File) -> List:
|
|
|
|
| 55 |
"""Update UI components based on file type"""
|
| 56 |
if not file_info:
|
| 57 |
+
return [
|
| 58 |
+
gr.Dropdown(visible=False), # format_dropdown
|
| 59 |
+
gr.Textbox(visible=False), # exe_name_input
|
| 60 |
+
gr.Button(visible=False) # convert_btn
|
| 61 |
+
]
|
| 62 |
|
| 63 |
filename = file_info.name.lower()
|
| 64 |
if filename.endswith('.exe'):
|
| 65 |
+
return [
|
| 66 |
+
gr.Dropdown(visible=False),
|
| 67 |
+
gr.Textbox(visible=True, value="converted.doc"),
|
| 68 |
+
gr.Button(visible=True)
|
| 69 |
+
]
|
| 70 |
else:
|
| 71 |
ext = Path(filename).suffix.lower()
|
| 72 |
doc_converter = DocumentConverter()
|
| 73 |
formats = doc_converter.supported_conversions.get(ext, [])
|
| 74 |
+
return [
|
| 75 |
+
gr.Dropdown(
|
| 76 |
visible=bool(formats),
|
| 77 |
+
choices=[f[1:].upper() for f in formats], # Remove dot and uppercase
|
| 78 |
value=formats[0][1:].upper() if formats else None
|
| 79 |
),
|
| 80 |
+
gr.Textbox(visible=False),
|
| 81 |
+
gr.Button(visible=bool(formats))
|
| 82 |
+
]
|
| 83 |
|
| 84 |
+
def convert_file(file_info: gr.File, output_format: str, exe_output_name: str) -> Tuple[str, str]:
|
| 85 |
"""Handle file conversion"""
|
| 86 |
if not file_info:
|
| 87 |
raise gr.Error("Please upload a file first")
|
|
|
|
| 92 |
output_path = convert_exe_to_doc(file_info.name, exe_output_name)
|
| 93 |
return output_path, "EXE to DOC conversion successful!"
|
| 94 |
else:
|
|
|
|
| 95 |
if not output_format:
|
| 96 |
raise gr.Error("Please select an output format")
|
| 97 |
output_path = DocumentConverter().convert_document(file_info.name, f".{output_format.lower()}")
|
|
|
|
| 126 |
file_input.change(
|
| 127 |
update_ui,
|
| 128 |
inputs=file_input,
|
| 129 |
+
outputs=[format_dropdown, exe_name_input, convert_btn]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
)
|
| 131 |
|
| 132 |
# Handle conversion
|