Neon-AI commited on
Commit
4395b3c
·
verified ·
1 Parent(s): 23d468b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -19
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import requests
2
  import tempfile
3
  import os
 
4
  import subprocess
5
  from fastapi import FastAPI, BackgroundTasks
6
  from pydantic import BaseModel
@@ -16,8 +17,8 @@ HF_AYANO_BASE = "https://a-y-a-n-o-k-o-j-i-dnd-api.hf.space"
16
  QUALITIES = ["360p", "720p", "1080p"]
17
  queue_lock = Lock()
18
 
19
- def log_error(msg: str):
20
- print(f"[ERROR] {msg}", flush=True)
21
 
22
  class EpisodeExceedsAvailableCount(Exception):
23
  pass
@@ -43,8 +44,7 @@ def download_video(anime_id: str, episode: int, quality: str) -> str | None:
43
  video_url = data["direct_link"]
44
  except EpisodeExceedsAvailableCount:
45
  raise
46
- except Exception as e:
47
- log_error(f"Link failed ep {episode} {quality}: {e}")
48
  return None
49
 
50
  tmp_path = tempfile.mktemp(suffix=".mp4")
@@ -54,8 +54,7 @@ def download_video(anime_id: str, episode: int, quality: str) -> str | None:
54
  with open(tmp_path, "wb") as f:
55
  shutil.copyfileobj(r.raw, f)
56
  return tmp_path
57
- except Exception as e:
58
- log_error(f"Download failed ep {episode} {quality}: {e}")
59
  if os.path.exists(tmp_path):
60
  os.remove(tmp_path)
61
  return None
@@ -71,7 +70,7 @@ def upload_to_0x0(file_path: str, file_name: str) -> str:
71
  r.raise_for_status()
72
  return r.text.strip()
73
  except Exception as e:
74
- log_error(f"Upload failed: {e}")
75
  raise
76
 
77
  def notify_render(anime_id: str, episode: int, quality: str, file_url: str, file_name: str, status: int):
@@ -84,9 +83,9 @@ def notify_render(anime_id: str, episode: int, quality: str, file_url: str, file
84
  "status": status
85
  }
86
  try:
87
- requests.post(RENDER_UPDATE_ENDPOINT, json=payload, timeout=10)
88
  except Exception as e:
89
- log_error(f"Notify failed: {e}")
90
 
91
  def blur_video(input_path: str) -> str:
92
  output = tempfile.mktemp(suffix="_blurred.mp4")
@@ -101,14 +100,11 @@ def blur_video(input_path: str) -> str:
101
  "-c:v", "libx264", "-preset", "fast", "-crf", "23",
102
  "-c:a", "copy", output
103
  ]
104
- try:
105
- subprocess.run(cmd, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
106
- return output
107
- except Exception as e:
108
- log_error(f"Blur failed: {e}")
109
- raise
110
 
111
  def process_anime(anime_id: str, anime_name: str):
 
112
  episode = 1
113
  while True:
114
  processed = False
@@ -127,15 +123,17 @@ def process_anime(anime_id: str, anime_name: str):
127
  except EpisodeExceedsAvailableCount:
128
  notify_render(anime_id, 0, "", "", "", 5)
129
  return
130
- except Exception:
131
  notify_render(anime_id, episode, quality, "", "", 3)
132
  if not processed:
133
  notify_render(anime_id, 0, "", "", "", 5)
134
  break
135
  episode += 1
 
136
 
137
  @app.post("/start")
138
- def start_endpoint(payload: StartPayload, background_tasks: BackgroundTasks):
 
139
  with queue_lock:
140
- background_tasks.add_task(process_anime, payload.anime_id, payload.anime_name)
141
- return {"code": 4, "message": "Job queued and processing in background"}
 
1
  import requests
2
  import tempfile
3
  import os
4
+ import shutil
5
  import subprocess
6
  from fastapi import FastAPI, BackgroundTasks
7
  from pydantic import BaseModel
 
17
  QUALITIES = ["360p", "720p", "1080p"]
18
  queue_lock = Lock()
19
 
20
+ def log(msg: str):
21
+ print(f"[HF] {msg}", flush=True)
22
 
23
  class EpisodeExceedsAvailableCount(Exception):
24
  pass
 
44
  video_url = data["direct_link"]
45
  except EpisodeExceedsAvailableCount:
46
  raise
47
+ except:
 
48
  return None
49
 
50
  tmp_path = tempfile.mktemp(suffix=".mp4")
 
54
  with open(tmp_path, "wb") as f:
55
  shutil.copyfileobj(r.raw, f)
56
  return tmp_path
57
+ except:
 
58
  if os.path.exists(tmp_path):
59
  os.remove(tmp_path)
60
  return None
 
70
  r.raise_for_status()
71
  return r.text.strip()
72
  except Exception as e:
73
+ log(f"[ERROR] Upload failed: {e}")
74
  raise
75
 
76
  def notify_render(anime_id: str, episode: int, quality: str, file_url: str, file_name: str, status: int):
 
83
  "status": status
84
  }
85
  try:
86
+ requests.post(RENDER_UPDATE_ENDPOINT, json=payload, timeout=20)
87
  except Exception as e:
88
+ log(f"[ERROR] Notify failed: {e}")
89
 
90
  def blur_video(input_path: str) -> str:
91
  output = tempfile.mktemp(suffix="_blurred.mp4")
 
100
  "-c:v", "libx264", "-preset", "fast", "-crf", "23",
101
  "-c:a", "copy", output
102
  ]
103
+ subprocess.run(cmd, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
104
+ return output
 
 
 
 
105
 
106
  def process_anime(anime_id: str, anime_name: str):
107
+ log(f"Started {anime_id} - {anime_name}")
108
  episode = 1
109
  while True:
110
  processed = False
 
123
  except EpisodeExceedsAvailableCount:
124
  notify_render(anime_id, 0, "", "", "", 5)
125
  return
126
+ except:
127
  notify_render(anime_id, episode, quality, "", "", 3)
128
  if not processed:
129
  notify_render(anime_id, 0, "", "", "", 5)
130
  break
131
  episode += 1
132
+ log("Complete")
133
 
134
  @app.post("/start")
135
+ def start(payload: StartPayload, bg: BackgroundTasks):
136
+ log(f"Queueing {payload.anime_id} - {payload.anime_name}")
137
  with queue_lock:
138
+ bg.add_task(process_anime, payload.anime_id, payload.anime_name)
139
+ return {"code": 4, "message": "Queued"}