Alex Volkov commited on
Commit
b21ecef
·
1 Parent(s): 352a44b

Many updates to the thing

Browse files
app.py CHANGED
@@ -1,12 +1,12 @@
 
1
  import gradio as gr
2
- from download import check_download, download, download_generator
3
- import argparse
4
  import anvil.media
5
  import os
6
- import pathlib
7
- from pathlib import Path
8
- from shutil import rmtree
9
  import dotenv
 
 
10
  from utils.utils import get_args
11
 
12
  dotenv.load_dotenv()
@@ -20,40 +20,19 @@ model_size: str = args.get("model")
20
  preload_model: str = args.get("preload")
21
 
22
  url_input = gr.Textbox(label="Youtube/Twitter/etc video URL (supports many services)", value='https://twitter.com/starsonxh/status/1552945347194142720', lines=1, elem_id="url_input")
23
- download_status = gr.Textbox(label="Status:", value='', lines=1, elem_id="download_status")
 
24
  init_video = gr.Video(label="Downloaded video", visible=False)
25
  init_audio = gr.Audio(label="Downloaded audio", visible=False)
26
  output_text = gr.Textbox(label="Output text", lines=5, visible=False, max_lines=10, interactive=True)
27
- sub_video = gr.Video(label="Subbed video", visible=False)
28
-
29
-
30
- @anvil.server.callable
31
- def cleanup_output_dir():
32
- #make sure we're in the main directory
33
- os.chdir(pathlib.Path(__file__).parent.absolute())
34
- #delete the output directory contents
35
- for path in Path("output").glob("**/*"):
36
- if path.is_file():
37
- path.unlink()
38
- elif path.is_dir():
39
- rmtree(path)
40
-
41
- @anvil.server.callable
42
- def download_api(url):
43
- print(f'Request from Anvil with URL {url}')
44
- final_response = ''
45
- #TODO: figure out how to push an incoming event to the queue
46
- #THIS DOESN'T WORK queue_placeholder.push_event('download', url)
47
- #TODO: handle errors
48
- for response in download_generator(url):
49
- final_response = response['message']
50
- print(final_response)
51
- return final_response
52
 
53
  def predownload(url):
54
  for response in download_generator(url):
55
  updates_object = {}
56
- updates_object[download_status] = response.get('message', '')
57
  meta = response.get('meta')
58
  if 'video' in response:
59
  updates_object[init_video] = gr.update(visible=True, value=response["video"],
@@ -104,11 +83,11 @@ with gr.Blocks(css=css+"") as demo:
104
  with gr.Group() as group:
105
  url_input.render()
106
  greet_btn = gr.Button("Translate", elem_id='submit', variant='primary')
107
- pause_for_editing = gr.Checkbox(label="Pause for editing")
108
- drowpdown = gr.PlayableVideo
109
  with gr.Row():
110
  with gr.Column():
111
- download_status.render()
112
  init_video.render()
113
  init_audio.render()
114
  with gr.Column():
@@ -123,9 +102,12 @@ with gr.Blocks(css=css+"") as demo:
123
  url_input.submit(fn=predownload, inputs=[url_input], outputs=[download_status, init_video, init_audio, output_text, sub_video])
124
  rebake.click(fn=rebake, inputs=[output_text, sub_video], outputs=[download_status, output_text, sub_video])
125
 
126
- api_button = gr.Button("API", variant='primary', visible=False).click(fn=cleanup_output_dir, inputs=[], outputs=[], api_name='cleanup_output_dir')
 
 
127
 
128
  queue_placeholder = demo.queue()
129
 
130
  if __name__ == "__main__":
131
- demo.launch(show_error=True, debug=True, share=gradio_share)
 
 
1
+ import gradio
2
  import gradio as gr
3
+
4
+ from download import download_generator
5
  import anvil.media
6
  import os
 
 
 
7
  import dotenv
8
+
9
+ from utils.apis import render_api_elements
10
  from utils.utils import get_args
11
 
12
  dotenv.load_dotenv()
 
20
  preload_model: str = args.get("preload")
21
 
22
  url_input = gr.Textbox(label="Youtube/Twitter/etc video URL (supports many services)", value='https://twitter.com/starsonxh/status/1552945347194142720', lines=1, elem_id="url_input")
23
+ # download_status = gr.Textbox(label="Status:", value='', lines=1, elem_id="download_status")
24
+ download_status = gr.Checkbox(label="Status:", elem_id="download_status", interactive=False)
25
  init_video = gr.Video(label="Downloaded video", visible=False)
26
  init_audio = gr.Audio(label="Downloaded audio", visible=False)
27
  output_text = gr.Textbox(label="Output text", lines=5, visible=False, max_lines=10, interactive=True)
28
+ sub_video = gr.Video(label="Subbed video", visible=False, mirror_webcam=False)
29
+
30
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  def predownload(url):
33
  for response in download_generator(url):
34
  updates_object = {}
35
+ updates_object[download_status] = gr.update(label=f"STATUS: {response.get('message')}")
36
  meta = response.get('meta')
37
  if 'video' in response:
38
  updates_object[init_video] = gr.update(visible=True, value=response["video"],
 
83
  with gr.Group() as group:
84
  url_input.render()
85
  greet_btn = gr.Button("Translate", elem_id='submit', variant='primary')
86
+
87
+ download_status.render()
88
  with gr.Row():
89
  with gr.Column():
90
+
91
  init_video.render()
92
  init_audio.render()
93
  with gr.Column():
 
102
  url_input.submit(fn=predownload, inputs=[url_input], outputs=[download_status, init_video, init_audio, output_text, sub_video])
103
  rebake.click(fn=rebake, inputs=[output_text, sub_video], outputs=[download_status, output_text, sub_video])
104
 
105
+
106
+ # Render imported buttons for API bindings
107
+ render_api_elements(url_input)
108
 
109
  queue_placeholder = demo.queue()
110
 
111
  if __name__ == "__main__":
112
+ gradio.close_all()
113
+ demo.launch(show_error=True, debug=True, share=gradio_share, server_port=8111, favicon_path='fonts/icon.png')
download.py CHANGED
@@ -83,6 +83,8 @@ def download_generator(url):
83
  print('stderr:', e.stderr.decode('utf8'))
84
  raise e
85
  except Exception as e:
 
 
86
  os.chdir(original_dir)
87
  print('error', file=sys.stderr)
88
  raise e
 
83
  print('stderr:', e.stderr.decode('utf8'))
84
  raise e
85
  except Exception as e:
86
+ print('stdout:', e.stdout.decode('utf8'))
87
+ print('stderr:', e.stderr.decode('utf8'))
88
  os.chdir(original_dir)
89
  print('error', file=sys.stderr)
90
  raise e
fonts/icon.png ADDED
fonts/logo.png ADDED
fonts/watermark.png ADDED
fonts/watermarksmol.png ADDED
requirements.txt CHANGED
@@ -1,5 +1,7 @@
1
- youtube-dl
2
- whisper @ git+https://github.com/openai/whisper.git@main#egg=whisper
3
- anvil-uplink
4
- gradio
5
- python-dotenv
 
 
 
1
+ youtube-dl==2021.12.17
2
+ whisper @ git+https://github.com/openai/whisper.git@main#egg=whisper==1.1.5
3
+ anvil-uplink==0.4.0
4
+ gradio==3.4.0
5
+ python-dotenv==0.21.0
6
+ aiohttp==3.8.3
7
+ aiohttp-requests==0.1.3
utils/apis.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import pathlib
4
+ import threading
5
+ import time
6
+ from pathlib import Path
7
+ from shutil import rmtree
8
+
9
+ import anvil.server
10
+ import anvil.media
11
+ import gradio as gr
12
+ import requests
13
+ import asyncio
14
+ import httpx
15
+
16
+ from download import download_generator
17
+
18
+
19
+ @anvil.server.callable()
20
+ def call_gradio_self_api(api_name='test_api', data=[]):
21
+ gradio_url = f"http://127.0.0.1:8111/api/{api_name}"
22
+ payload = json.dumps({"data": data})
23
+ headers = {
24
+ 'accept': 'application/json',
25
+ 'Content-Type': 'application/json'
26
+ }
27
+ print(f"Calling {gradio_url} with {payload}")
28
+ resp = requests.request("POST", gradio_url, headers=headers, data=payload)
29
+ print(resp.text)
30
+ return resp.text
31
+
32
+ def remote_download(url):
33
+ print(f"remote_download: Downloading {url}")
34
+ final_response = ''
35
+ subbed_video_media = None
36
+ for response in download_generator(url):
37
+ final_response = response['message']
38
+ print(final_response)
39
+ if 'sub_video' in response:
40
+ subbed_video_media = anvil.media.from_file(response['sub_video'])
41
+ return final_response, subbed_video_media
42
+
43
+ def test_api(url=''):
44
+ print(f'Request from Anvil with URL {url}, faking a long ass request that takes 15 seconds')
45
+ time.sleep(15)
46
+ # fake a long ass request that takes 15 seconds
47
+ # TODO: add a video output here to test how events are done
48
+ # TODO: add an anvil server pingback to show we completed the queue operation
49
+ return f"I've slept for 15 seconds and now I'm done. "
50
+
51
+ def render_api_elements(url_input):
52
+ with gr.Group() as api_buttons:
53
+ # This is a hack to get APIs registered with the blocks interface
54
+ gr.Button("API", visible=False)\
55
+ .click(api_name='cleanup_output_dir',
56
+ fn=cleanup_output_dir, queue=True, inputs=[], outputs=[])
57
+ gr.Button("API", visible=False)\
58
+ .click(api_name='test_api', queue=True, fn=test_api, inputs=[url_input], outputs=[])
59
+
60
+ gr.Button("remote_download", visible=False)\
61
+ .click(api_name='remote_download', queue=True, fn=remote_download, inputs=[url_input], outputs=[])
62
+ return api_buttons
63
+
64
+
65
+ @anvil.server.callable
66
+ def cleanup_output_dir():
67
+ #make sure we're in the main directory
68
+ os.chdir(pathlib.Path(__file__).parent.parent.absolute())
69
+ #delete the output directory contents
70
+ for path in Path("output").glob("**/*"):
71
+ if path.is_file():
72
+ path.unlink()
73
+ elif path.is_dir():
74
+ rmtree(path)
75
+
utils/subs.py CHANGED
@@ -10,17 +10,18 @@ from typing import Iterator, TextIO
10
  def bake_subs(input_file, output_file, subs_file, fontsdir):
11
  print(f"Baking {subs_file} into video... {input_file} -> {output_file}")
12
 
13
-
14
  fontfile = fontsdir / 'arial.ttf'
 
15
  fontstyle = 'Fontsize=18,OutlineColour=&H40000000,BorderStyle=3,FontName=Arial'
16
  video = ffmpeg.input(input_file)
 
17
  audio = video.audio
18
  (
19
- ffmpeg
20
- .concat(
21
- video.filter('subtitles', subs_file, fontsdir=fontfile, force_style=fontstyle),
22
- audio, v=1, a=1
23
- )
24
  .output(filename=output_file)
25
  .run(quiet=True, overwrite_output=True)
26
  )
 
10
  def bake_subs(input_file, output_file, subs_file, fontsdir):
11
  print(f"Baking {subs_file} into video... {input_file} -> {output_file}")
12
 
 
13
  fontfile = fontsdir / 'arial.ttf'
14
+ watermarkfile = fontsdir / 'watermarksmol.png'
15
  fontstyle = 'Fontsize=18,OutlineColour=&H40000000,BorderStyle=3,FontName=Arial'
16
  video = ffmpeg.input(input_file)
17
+ watermark = ffmpeg.input(watermarkfile)
18
  audio = video.audio
19
  (
20
+ ffmpeg.concat(
21
+ video.filter('subtitles', subs_file, fontsdir=fontfile, force_style=fontstyle),
22
+ audio, v=1, a=1
23
+ )
24
+ .overlay(watermark, x='10', y='10')
25
  .output(filename=output_file)
26
  .run(quiet=True, overwrite_output=True)
27
  )