rahul7star commited on
Commit
dd77776
·
verified ·
1 Parent(s): a56bbaa

Update app_lora_cpu.py

Browse files
Files changed (1) hide show
  1. app_lora_cpu.py +62 -15
app_lora_cpu.py CHANGED
@@ -302,52 +302,99 @@ HF_MODEL = os.environ.get("HF_UPLOAD_REPO", "rahul7star/wan22-aot-image-2026-jan
302
  # --- CPU-only upload function ---
303
  def upload_image_and_prompt_cpu(input_image, prompt_text) -> str:
304
  from datetime import datetime
305
- import tempfile, os, uuid, shutil
 
 
 
 
 
306
  from huggingface_hub import HfApi
307
 
308
- # Instantiate the HfApi class
309
  api = HfApi()
310
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
311
  today_str = datetime.now().strftime("%Y-%m-%d")
312
  unique_subfolder = f"Upload-Image-{uuid.uuid4().hex[:8]}"
313
  hf_folder = f"{today_str}/{unique_subfolder}"
314
 
315
- # Save image temporarily
 
 
 
 
 
316
  with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_img:
317
  if isinstance(input_image, str):
318
  shutil.copy(input_image, tmp_img.name)
319
  else:
320
  input_image.save(tmp_img.name, format="PNG")
 
321
  tmp_img_path = tmp_img.name
322
 
323
- # Upload image using HfApi instance
 
 
324
  api.upload_file(
325
  path_or_fileobj=tmp_img_path,
326
  path_in_repo=f"{hf_folder}/input_image.png",
327
  repo_id=HF_MODEL,
328
  repo_type="model",
329
- token=os.environ.get("HUGGINGFACE_HUB_TOKEN")
330
  )
331
 
332
- # Save prompt as summary.txt
333
- summary_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt").name
334
- with open(summary_file, "w", encoding="utf-8") as f:
335
- f.write(prompt_text)
336
-
 
 
 
 
 
 
 
 
 
 
 
337
  api.upload_file(
338
  path_or_fileobj=summary_file,
339
  path_in_repo=f"{hf_folder}/summary.txt",
340
  repo_id=HF_MODEL,
341
  repo_type="model",
342
- token=os.environ.get("HUGGINGFACE_HUB_TOKEN")
343
  )
344
 
345
- # Cleanup
346
- os.remove(tmp_img_path)
347
- os.remove(summary_file)
 
 
 
 
348
 
349
- return hf_folder
 
 
 
350
 
 
351
  def get_num_frames(duration_seconds: float):
352
  return 1 + int(np.clip(
353
  int(round(duration_seconds * FIXED_FPS)),
 
302
  # --- CPU-only upload function ---
303
  def upload_image_and_prompt_cpu(input_image, prompt_text) -> str:
304
  from datetime import datetime
305
+ import tempfile
306
+ import os
307
+ import uuid
308
+ import shutil
309
+ import unicodedata
310
+
311
  from huggingface_hub import HfApi
312
 
 
313
  api = HfApi()
314
 
315
+ # -----------------------------
316
+ # CLEAN TEXT
317
+ # -----------------------------
318
+ if prompt_text is None:
319
+ prompt_text = ""
320
+
321
+ # Replace problematic unicode chars
322
+ prompt_text = (
323
+ unicodedata.normalize("NFKC", str(prompt_text))
324
+ .replace("\xa0", " ")
325
+ .replace("\u200b", "")
326
+ )
327
+
328
+ # -----------------------------
329
+ # CREATE SAFE HF FOLDER
330
+ # -----------------------------
331
  today_str = datetime.now().strftime("%Y-%m-%d")
332
  unique_subfolder = f"Upload-Image-{uuid.uuid4().hex[:8]}"
333
  hf_folder = f"{today_str}/{unique_subfolder}"
334
 
335
+ # Ensure ASCII-safe repo path
336
+ hf_folder = hf_folder.encode("ascii", "ignore").decode()
337
+
338
+ # -----------------------------
339
+ # SAVE IMAGE TEMP
340
+ # -----------------------------
341
  with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_img:
342
  if isinstance(input_image, str):
343
  shutil.copy(input_image, tmp_img.name)
344
  else:
345
  input_image.save(tmp_img.name, format="PNG")
346
+
347
  tmp_img_path = tmp_img.name
348
 
349
+ # -----------------------------
350
+ # UPLOAD IMAGE
351
+ # -----------------------------
352
  api.upload_file(
353
  path_or_fileobj=tmp_img_path,
354
  path_in_repo=f"{hf_folder}/input_image.png",
355
  repo_id=HF_MODEL,
356
  repo_type="model",
357
+ token=os.environ.get("HUGGINGFACE_HUB_TOKEN"),
358
  )
359
 
360
+ # -----------------------------
361
+ # SAVE SUMMARY TXT
362
+ # -----------------------------
363
+ with tempfile.NamedTemporaryFile(
364
+ mode="w",
365
+ encoding="utf-8",
366
+ suffix=".txt",
367
+ delete=False,
368
+ ) as tmp_txt:
369
+
370
+ tmp_txt.write(prompt_text)
371
+ summary_file = tmp_txt.name
372
+
373
+ # -----------------------------
374
+ # UPLOAD TXT
375
+ # -----------------------------
376
  api.upload_file(
377
  path_or_fileobj=summary_file,
378
  path_in_repo=f"{hf_folder}/summary.txt",
379
  repo_id=HF_MODEL,
380
  repo_type="model",
381
+ token=os.environ.get("HUGGINGFACE_HUB_TOKEN"),
382
  )
383
 
384
+ # -----------------------------
385
+ # CLEANUP
386
+ # -----------------------------
387
+ try:
388
+ os.remove(tmp_img_path)
389
+ except:
390
+ pass
391
 
392
+ try:
393
+ os.remove(summary_file)
394
+ except:
395
+ pass
396
 
397
+ return hf_folder
398
  def get_num_frames(duration_seconds: float):
399
  return 1 + int(np.clip(
400
  int(round(duration_seconds * FIXED_FPS)),