bethecloud commited on
Commit
06945c0
1 Parent(s): 8b66d41

Update app.py

Browse files

Updated to new version

Files changed (1) hide show
  1. app.py +50 -47
app.py CHANGED
@@ -1,69 +1,72 @@
1
  import os
2
- import pathlib
3
- import random
4
- import shlex
5
- import subprocess
6
  import gradio as gr
7
- from gradio import inputs, outputs
8
- import torch
9
- from huggingface_hub import snapshot_download
10
  from modelscope.pipelines import pipeline
11
  from modelscope.outputs import OutputKeys
 
 
12
  import boto3
13
- from botocore.client import Config
14
- import gradio as gr
15
-
16
- # Downloading and setting up the model
17
- model_dir = pathlib.Path('weights')
18
- if not model_dir.exists():
19
- model_dir.mkdir()
20
- snapshot_download('damo-vilab/modelscope-damo-text-to-video-synthesis',
21
- repo_type='model',
22
- local_dir=model_dir)
23
 
 
24
  s3_access_key = "juj22qxqxql7u2pl6nomgxu3ip7a"
25
  s3_secret_key = "j3uwidtozhboy5vczhymhzkkjsaumznnqlzck5zjs5qxgsung4ukk"
26
  s3_endpoint = "https://gateway.storjshare.io"
27
 
28
- 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"))
 
 
 
 
 
 
 
 
29
 
30
- # Function to generate video and upload it to Storj
31
- def generate_video(prompt: str, seed: int) -> str:
32
- if seed == -1:
33
- seed = random.randint(0, 1000000)
34
- torch.manual_seed(seed)
35
- result = pipe({'text': prompt})[OutputKeys.OUTPUT_VIDEO]
 
 
36
 
37
- # Upload video to Storj
38
- bucket_name = "huggingface-demo"
39
- # Add the code to upload the video to the Storj bucket
40
- # and return the URL of the uploaded video
 
 
 
 
 
 
41
 
42
- return result
 
 
43
 
44
- # Gradio Interface
45
- examples = [
46
- ['An astronaut riding a horse.', 0],
47
- ['A panda eating bamboo on a rock.', 0],
48
- ['Spiderman is surfing.', 0],
49
- ]
50
 
51
- # Import Storj Theme from the hub
52
- storj_theme = gr.Theme.from_hub("bethecloud/storj_theme")
53
 
54
- inputs = [
55
- gr.inputs.Textbox(lines=1, placeholder="Enter your text here..."),
56
- gr.inputs.Slider(minimum=-1, maximum=1000000, step=1, default=-1, label="Seed")
57
- ]
58
 
59
  iface = gr.Interface(
60
  fn=generate_video,
61
- inputs=inputs,
62
- outputs=gr.outputs.Video(),
63
- theme=storj_theme,
64
- allow_flagging=False,
65
- examples=examples
 
 
 
 
 
66
  )
67
 
68
- ## Run app
69
  iface.launch(share=True, debug=True)
 
1
  import os
 
 
 
 
2
  import gradio as gr
 
 
 
3
  from modelscope.pipelines import pipeline
4
  from modelscope.outputs import OutputKeys
5
+ import requests
6
+ import tempfile
7
  import boto3
8
+ from botocore.exceptions import NoCredentialsError
 
 
 
 
 
 
 
 
 
9
 
10
+ # Hardcoded S3 keys
11
  s3_access_key = "juj22qxqxql7u2pl6nomgxu3ip7a"
12
  s3_secret_key = "j3uwidtozhboy5vczhymhzkkjsaumznnqlzck5zjs5qxgsung4ukk"
13
  s3_endpoint = "https://gateway.storjshare.io"
14
 
15
+ # Initialize Storj with boto3
16
+ s3 = boto3.client(
17
+ "s3",
18
+ endpoint_url=s3_endpoint,
19
+ aws_access_key_id=s3_access_key,
20
+ aws_secret_access_key=s3_secret_key,
21
+ )
22
+
23
+ p = pipeline("text-to-video-synthesis", "damo/text-to-video-synthesis")
24
 
25
+ async def generate_video(text_input: str, duration: int) -> (str, str):
26
+ test_text = {'text': text_input, 'duration': duration}
27
+ output_video_path = p(test_text,)[OutputKeys.OUTPUT_VIDEO]
28
+
29
+ # Upload to Storj
30
+ with open(output_video_path, "rb") as f:
31
+ video_key = os.path.basename(output_video_path)
32
+ s3.upload_fileobj(f, "huggingface-demo", video_key)
33
 
34
+ # Generate signed URL for uploaded video
35
+ try:
36
+ storj_video_url = s3.generate_presigned_url(
37
+ "get_object",
38
+ Params={"Bucket": "huggingface-demo", "Key": video_key},
39
+ ExpiresIn=3600,
40
+ )
41
+ except NoCredentialsError:
42
+ print("Credentials not available")
43
+ return None
44
 
45
+ # Download the video from Storj to a temporary file
46
+ response = requests.get(storj_video_url, stream=True)
47
+ response.raise_for_status()
48
 
49
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_file:
50
+ for chunk in response.iter_content(chunk_size=8192):
51
+ temp_file.write(chunk)
52
+ local_video_path = temp_file.name
 
 
53
 
54
+ return local_video_path, storj_video_url
 
55
 
56
+ storj_theme = gr.Theme.from_hub("bethecloud/storj_theme")
 
 
 
57
 
58
  iface = gr.Interface(
59
  fn=generate_video,
60
+ inputs=[
61
+ gr.inputs.Textbox(lines=3, label="Input text"),
62
+ gr.inputs.Slider(minimum=1, maximum=60, step=1, default=5, label="Duration (seconds)")
63
+ ],
64
+ outputs=[
65
+ gr.outputs.Video(label="Generated Video"),
66
+ gr.outputs.Textbox(label="Storj Video URL")
67
+ ],
68
+ title="Text to Video to Storj",
69
+ theme=storj_theme
70
  )
71
 
 
72
  iface.launch(share=True, debug=True)