bethecloud commited on
Commit
0b2f980
·
1 Parent(s): e358234

Update app.py

Browse files

Added uplink support

Files changed (1) hide show
  1. app.py +111 -149
app.py CHANGED
@@ -1,153 +1,115 @@
 
1
  import gradio as gr
2
- from gradio import Theme
3
-
4
- import os, cv2, torch, time, random
5
- import numpy as np
6
- from moviepy.editor import *
7
- import boto3
8
- from botocore.client import Config
9
- from botocore.exceptions import NoCredentialsError
10
-
11
- from diffusers import DiffusionPipeline, EulerAncestralDiscreteScheduler
12
- from PIL import Image
13
- from yt_dlp import YoutubeDL
14
-
15
- pipe = DiffusionPipeline.from_pretrained("timbrooks/instruct-pix2pix", torch_dtype=torch.float16, safety_checker=None)
16
- pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
17
- pipe.enable_xformers_memory_efficient_attention()
18
- pipe.unet.to(memory_format=torch.channels_last)
19
- pipe = pipe.to("CPU")
20
-
21
- s3_access_key = "juj22qxqxql7u2pl6nomgxu3ip7a"
22
- s3_secret_key = "j3uwidtozhboy5vczhymhzkkjsaumznnqlzck5zjs5qxgsung4ukk"
23
- s3_endpoint = "https://gateway.storjshare.io"
24
-
25
- s3 = boto3.client("s3", aws_access_key_id=s3_access_key, aws_secret_access_key=s3_secret_key, endpoint_url=s3_endpoint, config=Config(signature_version="s3v4"))
26
-
27
- my_theme = Theme.from_hub("bethecloud/storj_theme")
28
-
29
- def download_video(url):
30
- ydl_opts = {'overwrites':True, 'format':'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4', 'outtmpl':'/content/video.mp4'}
31
- with YoutubeDL(ydl_opts) as ydl:
32
- ydl.download(url)
33
- return f"/content/video.mp4"
34
-
35
- def pix2pix(prompt, text_guidance_scale, image_guidance_scale, image, steps, neg_prompt="", width=512, height=512, seed=0):
36
- if seed == 0:
37
- seed = random.randint(0, 2147483647)
38
- generator = torch.Generator("cuda").manual_seed(seed)
 
 
 
 
 
 
 
 
 
 
 
39
  try:
40
- image = Image.open(image)
41
- ratio = min(height / image.height, width / image.width)
42
- image = image.resize((int(image.width * ratio), int(image.height * ratio)), Image.LANCZOS)
43
- result = pipe(
44
- prompt,
45
- negative_prompt=neg_prompt,
46
- image=image,
47
- num_inference_steps=int(steps),
48
- image_guidance_scale=image_guidance_scale,
49
- guidance_scale=text_guidance_scale,
50
- generator=generator,
51
- )
52
- return result.images, result.nsfw_content_detected, seed
53
  except Exception as e:
54
- return None, None, error_str(e)
55
-
56
- def error_str(error, title="Error"):
57
- return (
58
- f"""#### {title}
59
- {error}"""
60
- if error
61
- else ""
62
- )
63
-
64
- def get_frames(video_in):
65
- frames = []
66
- clip = VideoFileClip(video_in)
67
- if clip.fps > 30:
68
- print("vide rate is over 30, resetting to 30")
69
- clip_resized = clip.resize(height=512)
70
- clip_resized.write_videofile("video_resized.mp4", fps=30, verbose=False)
71
  else:
72
- print("video rate is OK")
73
- clip_resized = clip.resize(height=512)
74
- clip_resized.write_videofile("video_resized.mp4", fps=clip.fps, verbose=False)
75
- print("video resized to 512 height")
76
- cap= cv2.VideoCapture("video_resized.mp4")
77
- fps = cap.get(cv2.CAP_PROP_FPS)
78
- print("video fps: " + str(fps))
79
- i=0
80
- while(cap.isOpened()):
81
- ret, frame = cap.read()
82
- if ret == False:
83
- break
84
- cv2.imwrite('in'+str(i)+'.jpg',frame)
85
- frames.append('in'+str(i)+'.jpg')
86
- i+=1
87
- cap.release()
88
- cv2.destroyAllWindows()
89
- print("broke the video into frames")
90
- return frames, fps
91
-
92
- def create_video(frames, fps):
93
- clips = [ImageClip(m).set_duration(1 / fps) for m in frames]
94
- concat_clip = concatenate_videoclips(clips, method="compose")
95
- concat_clip.write_videofile("/content/output.mp4", fps=fps, verbose=False)
96
- return "/content/output.mp4"
97
-
98
-
99
- def infer(prompt,video_in, seed_in, trim_value):
100
- print(prompt)
101
- break_vid = get_frames(video_in)
102
- frames_list= break_vid[0]
103
- fps = break_vid[1]
104
- n_frame = int(trim_value*fps)
105
- if n_frame >= len(frames_list):
106
- print("video is shorter than the cut value")
107
- n_frame = len(frames_list)
108
- result_frames = []
109
- print("set stop frames to: " + str(n_frame))
110
- for i in frames_list[0:int(n_frame)]:
111
- pix2pix_img = pix2pix(prompt,5.5,1.5,i,15,"",512,512,seed_in)
112
- images = pix2pix_img[0]
113
- rgb_im = images[0].convert("RGB")
114
- rgb_im.save(f"out-{i}.jpg")
115
- result_frames.append(f"out-{i}.jpg")
116
- print("frame " + i + "/" + str(n_frame) + ": done;")
117
- final_vid = create_video(result_frames, fps)
118
- print("Done!")
119
- return final_vid
120
-
121
- def upload_to_storj(bucket_name, file_path):
122
- try:
123
- file_name = os.path.basename(file_path)
124
- s3.upload_file(file_path, bucket_name, file_name)
125
- print(f"{file_name} uploaded to {bucket_name}")
126
- except NoCredentialsError:
127
- print("Credentials not available")
128
-
129
- with gr.Blocks(theme=my_theme) as demo:
130
- with gr.Column(elem_id="col-container"):
131
- with gr.Row():
132
- with gr.Column():
133
- input_text = gr.Textbox(show_label=False, value="https://link.storjshare.io/s/jwn3ljgyj4ynlg6qtqj6yrbo63ha/demo/dancing.mp4?view")
134
- input_download_button = gr.Button(value="Enter a link from Storj Linkshare")
135
- prompt = gr.Textbox(label="Prompt", placeholder="Enter new style of video", show_label=False, elem_id="prompt-in")
136
- video_inp = gr.Video(label="Video source", source="upload", type="filepath", elem_id="input-vid")
137
- input_download_button.click(download_video, inputs=[input_text], outputs=[video_inp])
138
- with gr.Column():
139
- video_out = gr.Video(label="Pix2pix video result", type="filepath", elem_id="video-output")
140
- with gr.Row():
141
- seed_inp = gr.Slider(label="Seed", minimum=0, maximum=2147483647, step=1, value=69)
142
- trim_in = gr.Slider(label="Cut video at (s)", minimun=1, maximum=600, step=1, value=1)
143
- submit_btn = gr.Button("Generate transformed video with Storj")
144
- inputs = [prompt,video_inp,seed_inp, trim_in]
145
- def process_and_upload(prompt, video_inp, seed_inp, trim_in):
146
- final_vid = infer(prompt, video_inp, seed_inp, trim_in)
147
- # Specify the name of the bucket to upload to
148
- storj_bucket_name = "huggingface-demo"
149
- upload_to_storj(storj_bucket_name, final_vid)
150
- return final_vid
151
-
152
- submit_btn.click(process_and_upload, inputs=inputs, outputs=[video_out])
153
- demo.queue().launch(debug=True, share=True, inline=False)
 
1
+ import os
2
  import gradio as gr
3
+ from modelscope.pipelines import pipeline
4
+ from modelscope.outputs import OutputKeys
5
+ import tempfile
6
+ import uplink_python
7
+ from uplink_python.uplink import Uplink
8
+ from uplink_python.module_classes import Permission, SharePrefix, Config
9
+ import datetime
10
+
11
+ # Define the text to video synthesis pipeline
12
+ p = pipeline("text-to-video-synthesis", "damo/text-to-video-synthesis")
13
+
14
+ # Storj configuration information
15
+ MY_API_KEY = "1H9SGajiNtFMJtjoseMKuhiLpuPHq3Sy7of6eNUjs8yeRE1DkmRF4XJsTm1ExXLbsxqJ4t5fRZobh4tyFmEdocNCFsJhukakve5C4V59TEEzqTaLVdjFc7ovCkmuRGotR9c2aqDdt5RThRTtKcXaVE5uCtuFQ9tZvnsX"
16
+ MY_SATELLITE = "12EayRS2V1kEsWESU9QMRseFhdxYxKicsiFmxrsLZHeLUtdps3S@us1.storj.io:7777"
17
+ MY_BUCKET = "huggingface-demo"
18
+ MY_ENCRYPTION_PASSPHRASE = "."
19
+
20
+ # Initialize Storj Uplink
21
+ uplink = Uplink()
22
+
23
+ # Implement the generate_video function
24
+ def generate_video(text_input: str, duration: int) -> str:
25
+ test_text = {'text': text_input, 'duration': duration}
26
+ output_video_path = p(test_text,)[OutputKeys.OUTPUT_VIDEO]
27
+
28
+ access = uplink.request_access_with_passphrase(MY_SATELLITE, MY_API_KEY, MY_ENCRYPTION_PASSPHRASE)
29
+ project = access.open_project()
30
+ project.ensure_bucket(MY_BUCKET)
31
+ video_key = os.path.basename(output_video_path)
32
+ with open(output_video_path, "rb") as f:
33
+ upload = project.upload_object(MY_BUCKET, video_key)
34
+ upload.write_file(f)
35
+ upload.commit()
36
+
37
+ permissions = Permission(allow_download=True)
38
+ shared_prefix = [SharePrefix(bucket=MY_BUCKET, prefix=video_key)]
39
+ shared_access = access.share(permissions, shared_prefix)
40
+ serialized_shared_access = shared_access.serialize()
41
+ deserialized_shared_access = uplink.parse_access(serialized_shared_access)
42
+ project_with_shared_access = deserialized_shared_access.open_project()
43
+
44
+ expiration = datetime.timedelta(hours=1)
45
+ expires_at = (datetime.datetime.utcnow() + expiration).timestamp()
46
+ storj_video_url = f"{MY_SATELLITE}/{MY_BUCKET}/{video_key}?access={serialized_shared_access}&expiresAt={expires_at}"
47
+ download = project_with_shared_access.download_object(MY_BUCKET, video_key)
48
+
49
+ chunk_size = 1024 * 1024
50
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
51
  try:
52
+ with open(temp_file.name, "wb") as dst:
53
+ while True:
54
+ try:
55
+ chunk = download.read(chunk_size) # Read a chunk from the download
56
+ except Exception as e:
57
+ print(f"Error while reading from download: {e}")
58
+ break
59
+
60
+ if not chunk:
61
+ break
62
+
63
+ dst.write(chunk) # Write the chunk to the destination file
 
64
  except Exception as e:
65
+ print(f"Error while writing to file: {e}")
66
+
67
+ temp_file.seek(0)
68
+ project.close()
69
+
70
+ return temp_file.name, storj_video_url
71
+
72
+ # Define the tweet_video function
73
+ def tweet_video(text_input: str, duration: int, button_click: bool) -> str:
74
+ if button_click:
75
+ tweet_text = f"Check out this amazing video generated by Text-to-Video Synthesis! {text_input}"
76
+ tweet_url = f"https://twitter.com/intent/tweet?text={tweet_text}"
77
+ return tweet_url
 
 
 
 
78
  else:
79
+ return "Hello " + text_input + "!"
80
+
81
+ # Set theme
82
+ my_theme = gr.Theme.from_hub("bethecloud/storj_theme")
83
+
84
+ # Define the input components
85
+ text_input = gr.inputs.Textbox(lines=3, placeholder="Enter a short text to generate video")
86
+ duration = gr.inputs.Slider(minimum=1, maximum=10, default=5, label="Duration (seconds)")
87
+ button = gr.inputs.Button(label="Share on Twitter")
88
+
89
+ # Define the output components
90
+ video_output = gr.outputs.Video(label="Generated Video")
91
+ url_output = gr.outputs.Textbox(label="Storj Video URL")
92
+
93
+ # Create the Gradio interface
94
+ iface = gr.Interface(
95
+ fn=tweet_video,
96
+ inputs=[text_input, duration],
97
+ outputs=[video_output, url_output],
98
+ title="Text-to-Video Synthesis",
99
+ description="Generate a video based on the input text and share it on Twitter.",
100
+ theme=my_theme
101
+ )
102
+
103
+ # Define the action for the "Share on Twitter" button
104
+ def on_button_click():
105
+ storj_video_url = iface.get_component("Storj Video URL").data
106
+ text_input = iface.get_component("Enter a short text to generate video").data
107
+ duration = iface.get_component("Duration (seconds)").data
108
+ tweet_url = tweet_video(text_input, duration, True)
109
+ gr.Interface.open_url(tweet_url)
110
+
111
+ # Add the "Share on Twitter" button and its action to the interface
112
+ iface.set_component("Share on Twitter", gr.Button(on_button_click, label="Share on Twitter"))
113
+
114
+ # Launch the interface
115
+ iface.launch(share=True, debug=True)