Spaces:
Configuration error
Configuration error
Dien-Hoa commited on
Commit ·
029ef5b
1
Parent(s): dd0a059
pico
Browse files
main.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
from fasthtml.common import *
|
|
|
|
| 2 |
from translator import languages as VALID_LANGUAGES, process_sbv_file
|
| 3 |
import tempfile
|
| 4 |
from starlette.requests import Request
|
|
@@ -9,47 +10,39 @@ import mimetypes
|
|
| 9 |
import zipfile
|
| 10 |
import io
|
| 11 |
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
# Move TEMP_FILES to a separate module for better organization
|
| 15 |
TEMP_FILES = {}
|
| 16 |
-
|
| 17 |
# Extract form generation to a separate function
|
| 18 |
def generate_language_form():
|
| 19 |
return Form(
|
| 20 |
Input(type='file', name='sbv_file', accept='.sbv'),
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
),
|
|
|
|
| 32 |
action="/upload", method='post', enctype='multipart/form-data'
|
| 33 |
)
|
| 34 |
|
| 35 |
@app.get('/')
|
| 36 |
def home():
|
| 37 |
-
return
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
const selectAllBtn = document.getElementById('select-all-btn');
|
| 44 |
-
const allChecked = Array.from(checkboxes).every(cb => cb.checked);
|
| 45 |
-
|
| 46 |
-
checkboxes.forEach(cb => {
|
| 47 |
-
cb.checked = !allChecked;
|
| 48 |
-
});
|
| 49 |
-
|
| 50 |
-
selectAllBtn.textContent = allChecked ? 'Select All' : 'Deselect All';
|
| 51 |
-
}
|
| 52 |
-
""")
|
| 53 |
)
|
| 54 |
|
| 55 |
# Extract file processing logic to a separate function
|
|
@@ -150,43 +143,34 @@ async def upload_file(request: Request, sbv_file: UploadFile):
|
|
| 150 |
|
| 151 |
result = translate_and_process_sbv(content, sbv_file.filename, languages)
|
| 152 |
|
| 153 |
-
return
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
background-color: #3a8bbb !important;
|
| 157 |
-
box-shadow: 0 4px 8px rgba(0,0,0,0.3) !important;
|
| 158 |
-
}
|
| 159 |
-
"""),
|
| 160 |
-
H1('Translations'),
|
| 161 |
-
Div(
|
| 162 |
A("Download All Translations",
|
| 163 |
href=f"/download/{result['zip_filename']}",
|
| 164 |
download=True,
|
| 165 |
-
class_="
|
| 166 |
-
|
| 167 |
),
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
Div(
|
| 171 |
-
*[Div(
|
| 172 |
-
H2(f'{lang.capitalize()} Translation'),
|
| 173 |
Pre(
|
| 174 |
'\n'.join(result['translations'][lang].split('\n')[:9]) + '\n...',
|
| 175 |
-
style="background-color: #f5f5f5; padding: 10px; border-radius: 5px; max-height: 200px; overflow-y: auto;"
|
| 176 |
-
),
|
| 177 |
-
A(f"Download {lang.capitalize()} Translation",
|
| 178 |
-
href=f"/download/{os.path.basename(result['file_paths'][lang])}",
|
| 179 |
-
download=True,
|
| 180 |
-
class_="download-btn",
|
| 181 |
-
style="display: inline-block; margin-top: 10px; padding: 8px 16px; background-color: #4299e1; color: white; text-decoration: none; border-radius: 3px; transition: all 0.3s;"
|
| 182 |
),
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
) for lang in result['translations']],
|
| 185 |
-
|
| 186 |
)
|
| 187 |
)
|
| 188 |
|
| 189 |
-
@
|
| 190 |
def get(filename: str):
|
| 191 |
file_path = TEMP_FILES.get(filename)
|
| 192 |
if file_path and os.path.exists(file_path):
|
|
|
|
| 1 |
from fasthtml.common import *
|
| 2 |
+
from fasthtml.xtend import CheckboxX
|
| 3 |
from translator import languages as VALID_LANGUAGES, process_sbv_file
|
| 4 |
import tempfile
|
| 5 |
from starlette.requests import Request
|
|
|
|
| 10 |
import zipfile
|
| 11 |
import io
|
| 12 |
|
| 13 |
+
css = Style(':root {--pico-font-size:90%,--pico-font-family: Pacifico, cursive;}')
|
| 14 |
+
app = FastHTML(hdrs=(picolink, css))
|
| 15 |
|
|
|
|
| 16 |
TEMP_FILES = {}
|
|
|
|
| 17 |
# Extract form generation to a separate function
|
| 18 |
def generate_language_form():
|
| 19 |
return Form(
|
| 20 |
Input(type='file', name='sbv_file', accept='.sbv'),
|
| 21 |
+
H3('Select languages for translation'),
|
| 22 |
+
Fieldset(
|
| 23 |
+
Legend("Languages"),
|
| 24 |
+
Div(
|
| 25 |
+
*[CheckboxX(
|
| 26 |
+
label=lang,
|
| 27 |
+
value=lang,
|
| 28 |
+
id=f'lang_{lang}',
|
| 29 |
+
name='languages'
|
| 30 |
+
) for lang in VALID_LANGUAGES],
|
| 31 |
+
class_='grid'
|
| 32 |
+
)
|
| 33 |
),
|
| 34 |
+
Button('Translate', class_="primary"),
|
| 35 |
action="/upload", method='post', enctype='multipart/form-data'
|
| 36 |
)
|
| 37 |
|
| 38 |
@app.get('/')
|
| 39 |
def home():
|
| 40 |
+
return Container(
|
| 41 |
+
Article(
|
| 42 |
+
H1("SBV File Translator"),
|
| 43 |
+
P("Upload an SBV File and select languages for translation:"),
|
| 44 |
+
generate_language_form(),
|
| 45 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
)
|
| 47 |
|
| 48 |
# Extract file processing logic to a separate function
|
|
|
|
| 143 |
|
| 144 |
result = translate_and_process_sbv(content, sbv_file.filename, languages)
|
| 145 |
|
| 146 |
+
return Container(
|
| 147 |
+
Article(
|
| 148 |
+
H1('Translations'),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
A("Download All Translations",
|
| 150 |
href=f"/download/{result['zip_filename']}",
|
| 151 |
download=True,
|
| 152 |
+
class_="primary",
|
| 153 |
+
role="button"
|
| 154 |
),
|
| 155 |
+
*[Card(
|
| 156 |
+
H3(f'{lang.capitalize()} Translation'),
|
|
|
|
|
|
|
|
|
|
| 157 |
Pre(
|
| 158 |
'\n'.join(result['translations'][lang].split('\n')[:9]) + '\n...',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
),
|
| 160 |
+
Footer(
|
| 161 |
+
A(f"Download {lang.capitalize()} Translation",
|
| 162 |
+
href=f"/download/{os.path.basename(result['file_paths'][lang])}",
|
| 163 |
+
download=True,
|
| 164 |
+
class_="secondary outline",
|
| 165 |
+
role="button"
|
| 166 |
+
)
|
| 167 |
+
)
|
| 168 |
) for lang in result['translations']],
|
| 169 |
+
class_="container-fluid" # Add this class for full-width content
|
| 170 |
)
|
| 171 |
)
|
| 172 |
|
| 173 |
+
@app.get("/download/{filename}")
|
| 174 |
def get(filename: str):
|
| 175 |
file_path = TEMP_FILES.get(filename)
|
| 176 |
if file_path and os.path.exists(file_path):
|