Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files
app.py
CHANGED
|
@@ -172,10 +172,6 @@ def parse_file(file_obj):
|
|
| 172 |
with zipfile.ZipFile(file_obj.name, 'r') as z:
|
| 173 |
z.extractall(extract_root)
|
| 174 |
|
| 175 |
-
# Check for existing media (rough check)
|
| 176 |
-
media_dir = os.path.join(extract_root, "media")
|
| 177 |
-
has_media = os.path.exists(media_dir) or any(f.isdigit() for f in os.listdir(extract_root))
|
| 178 |
-
|
| 179 |
col_path = os.path.join(extract_root, "collection.anki2")
|
| 180 |
if not os.path.exists(col_path):
|
| 181 |
shutil.rmtree(extract_root)
|
|
@@ -187,16 +183,23 @@ def parse_file(file_obj):
|
|
| 187 |
rows = cur.fetchall()
|
| 188 |
|
| 189 |
data = []
|
|
|
|
| 190 |
for r in rows:
|
| 191 |
flds = r[0].split('\x1f')
|
| 192 |
tags = r[1].strip() if len(r) > 1 else ""
|
| 193 |
q = flds[0] if len(flds) > 0 else ""
|
| 194 |
a = flds[1] if len(flds) > 1 else ""
|
|
|
|
|
|
|
|
|
|
| 195 |
data.append([q, a, tags])
|
| 196 |
|
| 197 |
df = pd.DataFrame(data, columns=["Question", "Answer", "Tags"])
|
| 198 |
conn.close()
|
| 199 |
|
|
|
|
|
|
|
|
|
|
| 200 |
else:
|
| 201 |
return None, None, None, "Unsupported file type", "", None
|
| 202 |
|
|
@@ -204,7 +207,7 @@ def parse_file(file_obj):
|
|
| 204 |
|
| 205 |
msg = f"✅ Loaded {len(df)} cards."
|
| 206 |
if has_media:
|
| 207 |
-
msg += " 🎵
|
| 208 |
|
| 209 |
return df, has_media, df.head(PREVIEW_LIMIT), msg, estimate_time(len(df), has_media), extract_root
|
| 210 |
|
|
@@ -473,6 +476,24 @@ with gr.Blocks(title="Pocket TTS Anki") as app:
|
|
| 473 |
file_input.upload(on_upload, inputs=file_input,
|
| 474 |
outputs=[full_df_state, has_media_state, preview_table, status, eta_box, extract_root_state, tag_dropdown])
|
| 475 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 476 |
def on_search(term, df, has_media, mode, search_in, selected_tag):
|
| 477 |
if df is None: return None, "No data"
|
| 478 |
|
|
|
|
| 172 |
with zipfile.ZipFile(file_obj.name, 'r') as z:
|
| 173 |
z.extractall(extract_root)
|
| 174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
col_path = os.path.join(extract_root, "collection.anki2")
|
| 176 |
if not os.path.exists(col_path):
|
| 177 |
shutil.rmtree(extract_root)
|
|
|
|
| 183 |
rows = cur.fetchall()
|
| 184 |
|
| 185 |
data = []
|
| 186 |
+
audio_count = 0 # Count cards with existing audio
|
| 187 |
for r in rows:
|
| 188 |
flds = r[0].split('\x1f')
|
| 189 |
tags = r[1].strip() if len(r) > 1 else ""
|
| 190 |
q = flds[0] if len(flds) > 0 else ""
|
| 191 |
a = flds[1] if len(flds) > 1 else ""
|
| 192 |
+
# Check if either field has audio tags
|
| 193 |
+
if re.search(r'\[sound:.*?\]', q) or re.search(r'\[sound:.*?\]', a):
|
| 194 |
+
audio_count += 1
|
| 195 |
data.append([q, a, tags])
|
| 196 |
|
| 197 |
df = pd.DataFrame(data, columns=["Question", "Answer", "Tags"])
|
| 198 |
conn.close()
|
| 199 |
|
| 200 |
+
# has_media means existing AUDIO, not images
|
| 201 |
+
has_media = audio_count > 0
|
| 202 |
+
|
| 203 |
else:
|
| 204 |
return None, None, None, "Unsupported file type", "", None
|
| 205 |
|
|
|
|
| 207 |
|
| 208 |
msg = f"✅ Loaded {len(df)} cards."
|
| 209 |
if has_media:
|
| 210 |
+
msg += f" 🎵 {audio_count} cards have existing audio."
|
| 211 |
|
| 212 |
return df, has_media, df.head(PREVIEW_LIMIT), msg, estimate_time(len(df), has_media), extract_root
|
| 213 |
|
|
|
|
| 476 |
file_input.upload(on_upload, inputs=file_input,
|
| 477 |
outputs=[full_df_state, has_media_state, preview_table, status, eta_box, extract_root_state, tag_dropdown])
|
| 478 |
|
| 479 |
+
def on_clear():
|
| 480 |
+
"""Reset all fields when file is cleared."""
|
| 481 |
+
return (
|
| 482 |
+
None, # full_df_state
|
| 483 |
+
False, # has_media_state
|
| 484 |
+
None, # preview_table
|
| 485 |
+
"", # status
|
| 486 |
+
"", # eta_box
|
| 487 |
+
None, # extract_root_state
|
| 488 |
+
gr.Dropdown(choices=["All"], value="All"), # tag_dropdown
|
| 489 |
+
"", # search_box
|
| 490 |
+
None, # dl (download file)
|
| 491 |
+
"" # result_lbl
|
| 492 |
+
)
|
| 493 |
+
|
| 494 |
+
file_input.clear(on_clear, inputs=[],
|
| 495 |
+
outputs=[full_df_state, has_media_state, preview_table, status, eta_box, extract_root_state, tag_dropdown, search_box, dl, result_lbl])
|
| 496 |
+
|
| 497 |
def on_search(term, df, has_media, mode, search_in, selected_tag):
|
| 498 |
if df is None: return None, "No data"
|
| 499 |
|