Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -15,14 +15,15 @@ import yt_dlp
|
|
15 |
ROOTPATH = os.path.dirname(os.path.abspath(__file__))
|
16 |
MODEL_PATH = os.path.join(ROOTPATH, "weights_v3")
|
17 |
FINISH_PATH = os.path.join(ROOTPATH, "finish")
|
18 |
-
|
|
|
19 |
VIDEO_OUTPUT_PATH = os.path.join(FINISH_PATH, "upscaled_video_temp.mp4")
|
20 |
FINAL_OUTPUT_PATH = os.path.join(FINISH_PATH, "upscaled_video.mp4")
|
21 |
|
22 |
# Create directories
|
23 |
try:
|
24 |
os.makedirs(FINISH_PATH, exist_ok=True)
|
25 |
-
os.makedirs(
|
26 |
except PermissionError as e:
|
27 |
raise PermissionError(f"Failed to create directories for anime upscaling: {e}")
|
28 |
|
@@ -34,6 +35,16 @@ try:
|
|
34 |
except FileNotFoundError:
|
35 |
raise FileNotFoundError(f"Anime model directory not found at {MODEL_PATH}.")
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
# Check if video has audio
|
38 |
def has_audio(video_path):
|
39 |
try:
|
@@ -70,7 +81,7 @@ def add_audio_to_video(video_path, audio_path, output_path):
|
|
70 |
|
71 |
# Download media using yt-dlp
|
72 |
def download_media(url, media_type):
|
73 |
-
output_path = os.path.join(
|
74 |
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
75 |
|
76 |
ydl_opts = {
|
@@ -207,6 +218,8 @@ def upscale_video(video_path, scale_factor, selected_model, progress=gr.Progress
|
|
207 |
|
208 |
# Process anime video or image
|
209 |
def process_media(drive_link, uploaded_file, scale_factor, selected_model, media_type, progress=gr.Progress()):
|
|
|
|
|
210 |
file_path = None
|
211 |
if uploaded_file:
|
212 |
file_path = uploaded_file
|
@@ -216,7 +229,7 @@ def process_media(drive_link, uploaded_file, scale_factor, selected_model, media
|
|
216 |
try:
|
217 |
file_id = drive_link.split('/d/')[1].split('/')[0]
|
218 |
extension = ".mp4" if media_type == "Video" else ".png"
|
219 |
-
file_path = os.path.join(
|
220 |
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
221 |
gdown.download(f"https://drive.google.com/uc?id={file_id}", file_path, quiet=False)
|
222 |
except Exception as e:
|
@@ -364,6 +377,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
|
364 |
- Use low-quality anime sources for optimal upscaling.
|
365 |
- 2x is great for subtle enhancements, 4x maximizes detail.
|
366 |
- Ensure links from Google Drive, YouTube, Instagram, or TikTok are accessible.
|
|
|
367 |
- Best suited for 2D anime styles with vibrant colors and sharp lines!
|
368 |
"""
|
369 |
)
|
|
|
15 |
ROOTPATH = os.path.dirname(os.path.abspath(__file__))
|
16 |
MODEL_PATH = os.path.join(ROOTPATH, "weights_v3")
|
17 |
FINISH_PATH = os.path.join(ROOTPATH, "finish")
|
18 |
+
TEMP_PATH = os.path.join(ROOTPATH, "tmp")
|
19 |
+
TEMP_AUDIO_PATH = os.path.join(TEMP_PATH, "audio_temp.wav")
|
20 |
VIDEO_OUTPUT_PATH = os.path.join(FINISH_PATH, "upscaled_video_temp.mp4")
|
21 |
FINAL_OUTPUT_PATH = os.path.join(FINISH_PATH, "upscaled_video.mp4")
|
22 |
|
23 |
# Create directories
|
24 |
try:
|
25 |
os.makedirs(FINISH_PATH, exist_ok=True)
|
26 |
+
os.makedirs(TEMP_PATH, exist_ok=True)
|
27 |
except PermissionError as e:
|
28 |
raise PermissionError(f"Failed to create directories for anime upscaling: {e}")
|
29 |
|
|
|
35 |
except FileNotFoundError:
|
36 |
raise FileNotFoundError(f"Anime model directory not found at {MODEL_PATH}.")
|
37 |
|
38 |
+
# Clean previous media files
|
39 |
+
def clean_previous_media():
|
40 |
+
for folder in [TEMP_PATH, FINISH_PATH]:
|
41 |
+
for file in os.listdir(folder):
|
42 |
+
if file.endswith(('.mp4', '.png', '.wav')):
|
43 |
+
try:
|
44 |
+
os.remove(os.path.join(folder, file))
|
45 |
+
except Exception as e:
|
46 |
+
print(f"Error deleting previous file {file}: {str(e)}")
|
47 |
+
|
48 |
# Check if video has audio
|
49 |
def has_audio(video_path):
|
50 |
try:
|
|
|
81 |
|
82 |
# Download media using yt-dlp
|
83 |
def download_media(url, media_type):
|
84 |
+
output_path = os.path.join(TEMP_PATH, f"anime_media_to_upscale.{'mp4' if media_type == 'Video' else 'png'}")
|
85 |
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
86 |
|
87 |
ydl_opts = {
|
|
|
218 |
|
219 |
# Process anime video or image
|
220 |
def process_media(drive_link, uploaded_file, scale_factor, selected_model, media_type, progress=gr.Progress()):
|
221 |
+
if drive_link:
|
222 |
+
clean_previous_media() # Clean previous media when a new link is provided
|
223 |
file_path = None
|
224 |
if uploaded_file:
|
225 |
file_path = uploaded_file
|
|
|
229 |
try:
|
230 |
file_id = drive_link.split('/d/')[1].split('/')[0]
|
231 |
extension = ".mp4" if media_type == "Video" else ".png"
|
232 |
+
file_path = os.path.join(TEMP_PATH, f"anime_media_to_upscale{extension}")
|
233 |
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
234 |
gdown.download(f"https://drive.google.com/uc?id={file_id}", file_path, quiet=False)
|
235 |
except Exception as e:
|
|
|
377 |
- Use low-quality anime sources for optimal upscaling.
|
378 |
- 2x is great for subtle enhancements, 4x maximizes detail.
|
379 |
- Ensure links from Google Drive, YouTube, Instagram, or TikTok are accessible.
|
380 |
+
- Previous media files are deleted when a new link is provided.
|
381 |
- Best suited for 2D anime styles with vibrant colors and sharp lines!
|
382 |
"""
|
383 |
)
|