| import os | |
| import fn | |
| import gradio as gr | |
| fn.load_model() | |
| last_url = None | |
| last_video = None | |
| last_audio = None | |
| last_chat = None | |
| last_info = None | |
| def run(url, mode): | |
| global last_url, last_video, last_audio, last_chat, last_info | |
| if url != last_url: | |
| last_url = url | |
| if last_video and os.path.exists(last_video): | |
| os.remove(last_video) | |
| last_video = None | |
| if last_audio and os.path.exists(last_audio): | |
| os.remove(last_audio) | |
| last_audio = None | |
| last_chat = None | |
| last_info = None | |
| result = fn.run(url, mode) | |
| if mode == 'video': | |
| ext = os.path.splitext(result)[1] | |
| os.rename(result, 'last_video' + ext) | |
| last_video = 'last_video' + ext | |
| elif mode == 'audio': | |
| ext = os.path.splitext(result)[1] | |
| os.rename(result, 'last_audio' + ext) | |
| last_audio = 'last_audio' + ext | |
| elif mode == 'chat': | |
| last_chat = result | |
| elif mode == 'info': | |
| last_info = result | |
| return last_video, last_audio, last_chat, last_info | |
| with gr.Blocks() as demo: | |
| title = gr.Markdown('# ytdlp') | |
| url = gr.Textbox(label="URL", interactive=True) | |
| mode = gr.Dropdown(value='video', choices=['video', 'audio', 'chat', 'info'], label="mode", allow_custom_value=False, interactive=True) | |
| run_button = gr.Button(value='Run') | |
| video = gr.Video(label="Video", interactive=False) | |
| audio = gr.Audio(label="Audio", interactive=False, type="filepath") | |
| chat = gr.Textbox(label="Chat", interactive=False, show_copy_button=True) | |
| info = gr.Textbox(label="Info", interactive=False, show_copy_button=True) | |
| run_button.click( | |
| fn=run, | |
| inputs=[url, mode], | |
| outputs=[video, audio, chat, info], | |
| ) | |
| if __name__ == '__main__': | |
| demo.launch() | |