Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
|
|
2 |
import moviepy.video.io.ImageSequenceClip
|
3 |
from PIL import Image
|
4 |
from pydub import AudioSegment
|
5 |
-
# Import everything needed to edit video clips
|
6 |
from moviepy.editor import *
|
7 |
import numpy as np
|
8 |
import mutagen
|
@@ -10,123 +9,58 @@ from mutagen.mp3 import MP3
|
|
10 |
import cv2
|
11 |
|
12 |
def resize(img_list):
|
13 |
-
print("** inside resize **")
|
14 |
-
print('Entity-Images generated by multimodal interface are:',img_list)
|
15 |
resize_img_list = []
|
16 |
for item in img_list:
|
17 |
im = Image.open(item)
|
18 |
imResize = im.resize((256,256), Image.ANTIALIAS)
|
19 |
resize_img_list.append(np.array(imResize))
|
20 |
-
print('Type of elements in the image list:',type(resize_img_list[0]))
|
21 |
return resize_img_list
|
22 |
|
23 |
|
24 |
def merge_audio_video(entities_num, resize_img_list, text_input):
|
25 |
-
print("** inside merge aud vid **")
|
26 |
-
print('Type of image list variable: ',type(resize_img_list))
|
27 |
-
print('Type of elements in the image list: ',type(resize_img_list[0]))
|
28 |
-
|
29 |
-
|
30 |
-
#Convert text to speech using facebook's latest model from HF hub
|
31 |
speech = text2speech(text_input)
|
32 |
-
print('Back in merge_audio_video')
|
33 |
-
print('Type of speech variable : ',type(speech))
|
34 |
-
print('Type of Audio file: ',speech)
|
35 |
wav_audio = AudioSegment.from_file(speech, "flac") #("/content/gdrive/My Drive/AI/audio1.flac", "flac")
|
36 |
-
#convert flac to mp3 audio format
|
37 |
-
print('COnverting flac format to mp3 using AudioSegment object:', type(wav_audio))
|
38 |
wav_audio.export("audio.mp3", format="mp3") #("/content/gdrive/My Drive/AI/audio1.mp3", format="mp3")
|
39 |
-
print('flac audio converted to mp3 audio' )
|
40 |
-
print('now getting duration of this mp3 audio' )
|
41 |
-
#getting audio clip's duration
|
42 |
audio_length = int(MP3("audio.mp3").info.length)
|
43 |
-
print('Audio length is :',audio_length)
|
44 |
-
|
45 |
-
#Calculate the desired frame per second based on given audio length and entities identified
|
46 |
fps= entities_num / audio_length #length of audio file
|
47 |
fps = float(format(fps, '.5f'))
|
48 |
-
print('Based on number of entities/images and audio length, FPS is set as : ',fps)
|
49 |
-
|
50 |
-
#String a list of images into a video and write to memory
|
51 |
clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(resize_img_list, fps=fps)
|
52 |
clip.write_videofile('my_vid_tmp.mp4')
|
53 |
-
print('video clip created successfully from images')
|
54 |
-
|
55 |
-
# loading video file
|
56 |
-
print('Starting video and audio merge')
|
57 |
videoclip = VideoFileClip('my_vid_tmp.mp4') #("/content/gdrive/My Drive/AI/my_video1.mp4")
|
58 |
-
print('loading video-clip')
|
59 |
-
|
60 |
-
# loading audio file
|
61 |
audioclip = AudioFileClip('audio.mp3') #.subclip(0, 15)
|
62 |
-
print('loading mp3-format audio')
|
63 |
-
# adding audio to the video clip
|
64 |
mergedclip = videoclip.set_audio(audioclip)
|
65 |
-
print('video and audio merged successfully')
|
66 |
-
|
67 |
-
#Getting size and frame count of merged video file
|
68 |
-
print('Getting size and frame count of merged video file')
|
69 |
duration = mergedclip.duration
|
70 |
frame_count = mergedclip.fps
|
71 |
-
print('duration is:',duration)
|
72 |
-
print('frame count :', frame_count)
|
73 |
-
|
74 |
return mergedclip
|
75 |
|
76 |
-
|
77 |
fastspeech = gr.Interface.load("huggingface/facebook/fastspeech2-en-ljspeech")
|
78 |
|
79 |
def text2speech(text):
|
80 |
-
print('** inside testtospeech **')
|
81 |
-
print('Loading the model through :',type(fastspeech))
|
82 |
-
print(fastspeech)
|
83 |
speech = fastspeech(text)
|
84 |
-
print('Type of variable in which file is stored:',type(speech))
|
85 |
-
print('Type of Audio file generated :',speech)
|
86 |
return speech
|
87 |
|
88 |
def engine(text_input):
|
89 |
-
print(" ** Inside Enngine **")
|
90 |
-
#Extract entities from text
|
91 |
ner = gr.Interface.load("huggingface/flair/ner-english-ontonotes-large")
|
92 |
entities = ner(text_input)
|
93 |
entities = [tupl for tupl in entities if None not in tupl]
|
94 |
entities_num = len(entities)
|
95 |
-
|
96 |
-
#Generate images using multimodelart's space for each entity identified above
|
97 |
img_list = []
|
98 |
for ent in entities:
|
99 |
img = gr.Interface.load("spaces/multimodalart/latentdiffusion")(ent[0],'50','256','256','1',10)[0]
|
100 |
img_list.append(img)
|
101 |
-
|
102 |
-
print('img_list size:',len(img_list))
|
103 |
-
#Resizing all images produced to same size
|
104 |
resize_img_list = resize(img_list)
|
105 |
-
print('back from resize into engine')
|
106 |
-
|
107 |
-
|
108 |
-
#Merge video and audio created above
|
109 |
mergedclip = merge_audio_video(entities_num, resize_img_list, text_input)
|
110 |
-
print('\n Back in engine')
|
111 |
-
print(' Merged clip type :',type(mergedclip))
|
112 |
-
print('Writing the merged video clip to a video file')
|
113 |
mergedclip.to_videofile('mergedvideo.mp4')
|
114 |
-
print('mergedvideo.mp4 created')
|
115 |
-
|
116 |
-
print('################################ Single Run Completed ##############################')
|
117 |
return 'mergedvideo.mp4'
|
118 |
|
119 |
app = gr.Interface(engine,
|
120 |
gr.inputs.Textbox(lines=5, label="Input Text"),
|
121 |
gr.outputs.Video(type=None, label='Final Merged video'),
|
122 |
-
description="<div>๐ญ๐๏ธ๐ฟ AI
|
123 |
examples=
|
124 |
[
|
125 |
-
["With Christmas only a few days away, Paige Summerland, a Los Angeles magazine writer specializing in relationship advice, boards an eastbound flight to spend the holiday with her fianc, Jack, meet his parents for the first time and plan her upcoming โฌลdream wedding.โฌย Paiges enthusiasm turns to panic when a massive snowstorm detours her flight and she meets - and locks horns with - Dylan, a cynical bar manager who has lost his faith in love. When it looks like all hope of arriving in New York for Christmas is lost, Dylan surprises Paige by renting an SUV and offering her a ride. Reluctantly, Paige braves the snowy road trip with Dylan, joined by Frank and Maxine Harper, an older couple whose 20-year marriage has lost its spark. Along the journey, feelings start to develop between Paige and Dylan. Now, as the young bride-to-be scrambles to make it to New York for Christmas, she begins to wonder if shes marrying the right man and just might learn a surprising lesson about love."],
|
126 |
-
["This is a story about a romantic journey of a culturally opposite couple - Krish Malhotra and Ananya Swaminathan. They meet at the IIM-Ahmedabad College and during the program they fall in love. Complications arise after the program comes to an end and they decide to get married. Krish and Ananya belong to two different states of India. Krish, a North Indian Punjabi boy from Delhi, and Ananya, a Tamilian Brahmin from Chennai. They take a conscious decision; they wont get married until their parents agree. Everything goes downhill when the parents meet. There is a cultural clash and the parents oppose the wedding. To convert their love story into a love marriage, the couple faces a tough battle in front of them. For it is easy to fight and rebel, but much harder to convince. Will Krish and Ananyas love for each other sustain the battles? Will they manage to convince their parents and make it to their wedding? The film thus is a humorous take on inter community marriages in India."],
|
127 |
["Kicking Off starts with the most important game of the season. Loyal fans Wigsy and Cliff watch in trepidation as their football team score the goal that will save them from relegation. Victory is bliss as a chorus of supporters chant and cry with elation. However, this frenzy of happiness quickly turns ugly as the referee disallows the deciding goal. With their hearts and fists pumping, adrenalin running and fury racing through their bloodstream, the fans take matters into their own hands and Cliff makes the fatal mistake of planning while intoxicated. Wigsy, a confirmed idiot, follows through with the said plan and in the darkest hours of the night he commits a crime that will cause chaos and catastrophe for him and his best mate Cliff. Kicking Off is cleverly filmed with split screen shots and slow motion montages. The characters are lovable thugs who will leave you laughing and grimacing at their lack of common sense. The beautiful game just got ugly."],
|
128 |
["Young pilot Branson (Louis Koo) recently takes over Skylette, his fathers aviation empire, only to realise his old flames Cassie (Charmaine Sheh) is a flight attendant there. Several years ago, he was forced to break up with her and move to New York to take care of his fathers business. To this day, the two continue to harbour feelings for each other but decide to keep them bottled up. In an effort to rebrand the airline, Branson invites rock idol TM to star in an upcoming commercial and appoints Sam (Fransic Ng) as her flying consultant. Incongruent in both tastes and experience, this odd couple gets off on the wrong foot. As the shoot progresses, however, they slowly discover each others merits, developing a strong mutual attraction. Jayden (Julien Cheung) has left Skylette Airline to become a pilot for private jets. He meets the young and vivacious Kika (Kuo Tsai Chieh) during a flight and assumes her to be wayward and shallow..."],
|
129 |
-
["Prem Prakash Tiwari (Ayushmann Khurrana) listening to Kumar Sanu is the films opening shot. Set in Haridwar, 1990s, the film captures the nascent feel of the town. Prem owns a cassette shop in the local market. His father is keen to get him hitched and the family goes to a local temple to meet Sandhya (Bhumi Pednekar). B.Ed, waiting for a teaching job, the most visible thing about her is her weight. Coming from a patriarchal cognitive set-up, she doesnt fit the quintessential idea of beautiful. And still, the school drop out Prem must marry her because he is incapable of attaining a girl with Juhi-Chawla-level-of-looks. In an elaborate community-wedding ceremony, Prem and Sandhya get married. Their wedding night is uncomfortable with neither treading towards establishing conjugal relations. Prem has in his own reasons and the girl is naturally shy. Next morning on a call, she announces it to a friend and the whole family finds out"],
|
130 |
["Two groups of classic cartoon characters come together in this fun-filled crossover with the popular action-adventure animated series Jonny Quest. Fans of all ages wont want to miss this heart-stopping adventure as tussling twosome Tom and Jerry join Jonny Quest and his pal Hadji and embark on a dangerous spy mission in order to save the world. Its just another day at the beach for the dueling Tom and Jerry ... until they bump into world-class junior spies Jonny Quest and Hadji and their canine companion, Bandit. When longtime Quest family nemesis Dr. Zin discovers that Jonnys father, Dr. Benton Quest, possesses a device that could solve the worlds energy problems, Zin sends his evil cat army to steal it and capture Benton and his bodyguard, Race Bannon. Thats when Jonny and his new furry friends spring into action! Get set for intrigue, thrills and suspense for the whole family. Tia Carrere and Tim Matheson lend their voices."],
|
131 |
["As Spud Milton continues his awkward stagger through adolescence, he learns one of lifes most important lessons: When dealing with women and cretins, nothing is ever quite as it seems. Im practically a man in most areas, writes Spud confidently on his sixteenth birthday. The year is 1992 and, in South Africa, radical change is in the air. The country may be on the bumpy road to an uncertain future, but Spud Milton is hoping for a smooth ride as he returns to boarding school as a senior. Instead, he discovers that his vindictive arch enemy is back to taunt him and that a garrulous Malawian has taken residence in his dormitory, along with the regular inmates and misfits he calls friends. Spuds world has never seemed less certain; he attempts to master Shakespeare, wrestles constantly with his God, and the power of negative thinking, and develops an aversion to fried fish after a shocking discovery about his grandmother, Wombat."],
|
132 |
["Beatle is a quirky loner who refuses to live by societys rules. Her business cards read Towing/Assassination, and she even has a hit man infomercial to go along with it. Her only human contact is with her psychiatrist and the stripper/hooker she employs once a week. Everything changes the night she meets Athena Klendon, a suicidal blonde with a secret. With a demented killer on their tail, courtesy of Athenas crooked former employer, the two quickly strike a bargain. Athena will change her life insurance policy to reflect Beatle as the beneficiary in exchange for her own execution. And while they wait for the paperwork to come through, Beatle will provide room and board and get some extra help on her infomercial. The two have no idea that Detective Holt has been on their case reviewing their every move. Just when he thinks hes got Beatle pinned beyond a shadow of a doubt, this comedic mystery takes a wild and unexpected turn."],
|
|
|
2 |
import moviepy.video.io.ImageSequenceClip
|
3 |
from PIL import Image
|
4 |
from pydub import AudioSegment
|
|
|
5 |
from moviepy.editor import *
|
6 |
import numpy as np
|
7 |
import mutagen
|
|
|
9 |
import cv2
|
10 |
|
11 |
def resize(img_list):
|
|
|
|
|
12 |
resize_img_list = []
|
13 |
for item in img_list:
|
14 |
im = Image.open(item)
|
15 |
imResize = im.resize((256,256), Image.ANTIALIAS)
|
16 |
resize_img_list.append(np.array(imResize))
|
|
|
17 |
return resize_img_list
|
18 |
|
19 |
|
20 |
def merge_audio_video(entities_num, resize_img_list, text_input):
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
speech = text2speech(text_input)
|
|
|
|
|
|
|
22 |
wav_audio = AudioSegment.from_file(speech, "flac") #("/content/gdrive/My Drive/AI/audio1.flac", "flac")
|
|
|
|
|
23 |
wav_audio.export("audio.mp3", format="mp3") #("/content/gdrive/My Drive/AI/audio1.mp3", format="mp3")
|
|
|
|
|
|
|
24 |
audio_length = int(MP3("audio.mp3").info.length)
|
|
|
|
|
|
|
25 |
fps= entities_num / audio_length #length of audio file
|
26 |
fps = float(format(fps, '.5f'))
|
|
|
|
|
|
|
27 |
clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(resize_img_list, fps=fps)
|
28 |
clip.write_videofile('my_vid_tmp.mp4')
|
|
|
|
|
|
|
|
|
29 |
videoclip = VideoFileClip('my_vid_tmp.mp4') #("/content/gdrive/My Drive/AI/my_video1.mp4")
|
|
|
|
|
|
|
30 |
audioclip = AudioFileClip('audio.mp3') #.subclip(0, 15)
|
|
|
|
|
31 |
mergedclip = videoclip.set_audio(audioclip)
|
|
|
|
|
|
|
|
|
32 |
duration = mergedclip.duration
|
33 |
frame_count = mergedclip.fps
|
|
|
|
|
|
|
34 |
return mergedclip
|
35 |
|
|
|
36 |
fastspeech = gr.Interface.load("huggingface/facebook/fastspeech2-en-ljspeech")
|
37 |
|
38 |
def text2speech(text):
|
|
|
|
|
|
|
39 |
speech = fastspeech(text)
|
|
|
|
|
40 |
return speech
|
41 |
|
42 |
def engine(text_input):
|
|
|
|
|
43 |
ner = gr.Interface.load("huggingface/flair/ner-english-ontonotes-large")
|
44 |
entities = ner(text_input)
|
45 |
entities = [tupl for tupl in entities if None not in tupl]
|
46 |
entities_num = len(entities)
|
|
|
|
|
47 |
img_list = []
|
48 |
for ent in entities:
|
49 |
img = gr.Interface.load("spaces/multimodalart/latentdiffusion")(ent[0],'50','256','256','1',10)[0]
|
50 |
img_list.append(img)
|
|
|
|
|
|
|
51 |
resize_img_list = resize(img_list)
|
|
|
|
|
|
|
|
|
52 |
mergedclip = merge_audio_video(entities_num, resize_img_list, text_input)
|
|
|
|
|
|
|
53 |
mergedclip.to_videofile('mergedvideo.mp4')
|
|
|
|
|
|
|
54 |
return 'mergedvideo.mp4'
|
55 |
|
56 |
app = gr.Interface(engine,
|
57 |
gr.inputs.Textbox(lines=5, label="Input Text"),
|
58 |
gr.outputs.Video(type=None, label='Final Merged video'),
|
59 |
+
description="<div>๐ญ๐๏ธ๐ฟ AI Movie Maker - Comedy ๐ฌ ๐ง ๐จ</div>" ,
|
60 |
examples=
|
61 |
[
|
|
|
|
|
62 |
["Kicking Off starts with the most important game of the season. Loyal fans Wigsy and Cliff watch in trepidation as their football team score the goal that will save them from relegation. Victory is bliss as a chorus of supporters chant and cry with elation. However, this frenzy of happiness quickly turns ugly as the referee disallows the deciding goal. With their hearts and fists pumping, adrenalin running and fury racing through their bloodstream, the fans take matters into their own hands and Cliff makes the fatal mistake of planning while intoxicated. Wigsy, a confirmed idiot, follows through with the said plan and in the darkest hours of the night he commits a crime that will cause chaos and catastrophe for him and his best mate Cliff. Kicking Off is cleverly filmed with split screen shots and slow motion montages. The characters are lovable thugs who will leave you laughing and grimacing at their lack of common sense. The beautiful game just got ugly."],
|
63 |
["Young pilot Branson (Louis Koo) recently takes over Skylette, his fathers aviation empire, only to realise his old flames Cassie (Charmaine Sheh) is a flight attendant there. Several years ago, he was forced to break up with her and move to New York to take care of his fathers business. To this day, the two continue to harbour feelings for each other but decide to keep them bottled up. In an effort to rebrand the airline, Branson invites rock idol TM to star in an upcoming commercial and appoints Sam (Fransic Ng) as her flying consultant. Incongruent in both tastes and experience, this odd couple gets off on the wrong foot. As the shoot progresses, however, they slowly discover each others merits, developing a strong mutual attraction. Jayden (Julien Cheung) has left Skylette Airline to become a pilot for private jets. He meets the young and vivacious Kika (Kuo Tsai Chieh) during a flight and assumes her to be wayward and shallow..."],
|
|
|
64 |
["Two groups of classic cartoon characters come together in this fun-filled crossover with the popular action-adventure animated series Jonny Quest. Fans of all ages wont want to miss this heart-stopping adventure as tussling twosome Tom and Jerry join Jonny Quest and his pal Hadji and embark on a dangerous spy mission in order to save the world. Its just another day at the beach for the dueling Tom and Jerry ... until they bump into world-class junior spies Jonny Quest and Hadji and their canine companion, Bandit. When longtime Quest family nemesis Dr. Zin discovers that Jonnys father, Dr. Benton Quest, possesses a device that could solve the worlds energy problems, Zin sends his evil cat army to steal it and capture Benton and his bodyguard, Race Bannon. Thats when Jonny and his new furry friends spring into action! Get set for intrigue, thrills and suspense for the whole family. Tia Carrere and Tim Matheson lend their voices."],
|
65 |
["As Spud Milton continues his awkward stagger through adolescence, he learns one of lifes most important lessons: When dealing with women and cretins, nothing is ever quite as it seems. Im practically a man in most areas, writes Spud confidently on his sixteenth birthday. The year is 1992 and, in South Africa, radical change is in the air. The country may be on the bumpy road to an uncertain future, but Spud Milton is hoping for a smooth ride as he returns to boarding school as a senior. Instead, he discovers that his vindictive arch enemy is back to taunt him and that a garrulous Malawian has taken residence in his dormitory, along with the regular inmates and misfits he calls friends. Spuds world has never seemed less certain; he attempts to master Shakespeare, wrestles constantly with his God, and the power of negative thinking, and develops an aversion to fried fish after a shocking discovery about his grandmother, Wombat."],
|
66 |
["Beatle is a quirky loner who refuses to live by societys rules. Her business cards read Towing/Assassination, and she even has a hit man infomercial to go along with it. Her only human contact is with her psychiatrist and the stripper/hooker she employs once a week. Everything changes the night she meets Athena Klendon, a suicidal blonde with a secret. With a demented killer on their tail, courtesy of Athenas crooked former employer, the two quickly strike a bargain. Athena will change her life insurance policy to reflect Beatle as the beneficiary in exchange for her own execution. And while they wait for the paperwork to come through, Beatle will provide room and board and get some extra help on her infomercial. The two have no idea that Detective Holt has been on their case reviewing their every move. Just when he thinks hes got Beatle pinned beyond a shadow of a doubt, this comedic mystery takes a wild and unexpected turn."],
|