Spaces:
Runtime error
Runtime error
Commit
·
0b2f980
1
Parent(s):
e358234
Update app.py
Browse filesAdded uplink support
app.py
CHANGED
@@ -1,153 +1,115 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
4 |
-
import
|
5 |
-
import
|
6 |
-
from
|
7 |
-
import
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
with
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
try:
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
return result.images, result.nsfw_content_detected, seed
|
53 |
except Exception as e:
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
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 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|