Manasa1 commited on
Commit
ac8612f
·
verified ·
1 Parent(s): 177dc9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -44
app.py CHANGED
@@ -1,80 +1,115 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
  import torch
4
- from PIL import Image, ImageDraw, ImageFont
 
 
 
 
 
 
 
 
 
5
  import os
6
- import random
7
-
8
- # Initialize text generation model
9
- text_generator = pipeline('text-generation', model='gpt2')
10
-
11
- # Function to generate comedy script
12
- def generate_comedy_script(prompt, max_length=100):
13
- generated = text_generator(f"Write a short comedy script about {prompt}: ", max_length=max_length, num_return_sequences=1)[0]
14
- return generated['generated_text']
15
-
16
- # Function to create a simple animation frame
17
- def create_animation_frame(text, frame_number):
18
- img = Image.new('RGB', (400, 200), color=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
19
- d = ImageDraw.Draw(img)
20
- fnt = ImageFont.load_default()
21
- d.text((10, 10), text, font=fnt, fill=(255, 255, 0))
22
- d.text((10, 180), f"Frame {frame_number}", font=fnt, fill=(255, 255, 255))
23
- return img
24
-
25
- # Function to create a simple animation
26
- def create_animation(text, num_frames=5):
27
- frames = []
28
- for i in range(num_frames):
29
- frame = create_animation_frame(text[:50], i+1) # Use first 50 characters for each frame
30
- frames.append(frame)
31
-
32
- animation_path = 'animation.gif'
33
- frames[0].save(animation_path, save_all=True, append_images=frames[1:], duration=500, loop=0)
34
- return animation_path
35
 
36
- # Function to generate kids music theme
37
- def generate_kids_music_theme(theme):
38
- return f"🎵 {theme} for kids! 🎵\nImagine a catchy tune with lyrics about {theme}."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  # Main function to generate comedy animation
41
  def generate_comedy_animation(prompt):
42
- script = generate_comedy_script(prompt)
43
- animation_path = create_animation(script)
44
- return script, animation_path
 
45
 
46
  # Main function to generate kids music animation
47
  def generate_kids_music_animation(theme):
48
- music_theme = generate_kids_music_theme(theme)
49
- animation_path = create_animation(music_theme)
50
- return music_theme, animation_path
 
51
 
52
  # Gradio Interface
53
  with gr.Blocks() as app:
54
- gr.Markdown("## Simplified Comedy and Kids Animation Generator")
55
 
56
  with gr.Tab("Comedy Animation"):
57
  comedy_prompt = gr.Textbox(label="Enter comedy prompt")
58
  comedy_generate_btn = gr.Button("Generate Comedy Animation")
59
  comedy_script = gr.Textbox(label="Generated Comedy Script")
60
  comedy_animation = gr.Image(label="Comedy Animation")
 
61
 
62
  comedy_generate_btn.click(
63
  generate_comedy_animation,
64
  inputs=comedy_prompt,
65
- outputs=[comedy_script, comedy_animation]
66
  )
67
 
68
  with gr.Tab("Kids Music Animation"):
69
  kids_theme = gr.Textbox(label="Enter kids music theme")
70
  kids_generate_btn = gr.Button("Generate Kids Music Animation")
71
- kids_music_theme = gr.Textbox(label="Generated Music Theme")
72
  kids_animation = gr.Image(label="Kids Music Animation")
 
73
 
74
  kids_generate_btn.click(
75
  generate_kids_music_animation,
76
  inputs=kids_theme,
77
- outputs=[kids_music_theme, kids_animation]
78
  )
79
 
80
  app.launch()
 
1
  import gradio as gr
 
2
  import torch
3
+ from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
4
+ from TTS.api import TTS
5
+ import numpy as np
6
+ import librosa
7
+ import soundfile as sf
8
+ import matplotlib.pyplot as plt
9
+ import matplotlib.animation as animation
10
+ from mpl_toolkits.mplot3d import Axes3D
11
+ import io
12
+ import base64
13
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ # Initialize text generation model (GPT-2)
16
+ tokenizer = AutoTokenizer.from_pretrained("gpt2")
17
+ model = AutoModelForCausalLM.from_pretrained("gpt2")
18
+
19
+ # Initialize TTS model
20
+ tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC")
21
+
22
+ # Function to generate text using GPT-2
23
+ def generate_text(prompt, max_length=100):
24
+ input_ids = tokenizer.encode(prompt, return_tensors="pt")
25
+ output = model.generate(input_ids, max_length=max_length, num_return_sequences=1, no_repeat_ngram_size=2)
26
+ return tokenizer.decode(output[0], skip_special_tokens=True)
27
+
28
+ # Function to generate speech using TTS
29
+ def generate_speech(text):
30
+ output_path = "generated_speech.wav"
31
+ tts.tts_to_file(text=text, file_path=output_path)
32
+ return output_path
33
+
34
+ # Function to create a more advanced character animation
35
+ def create_character_animation(text):
36
+ def update_point(num, data, line):
37
+ line.set_data(data[:2, :num])
38
+ line.set_3d_properties(data[2, :num])
39
+ return line
40
+
41
+ fig = plt.figure()
42
+ ax = fig.add_subplot(111, projection='3d')
43
+
44
+ # Generate a more complex 3D path for the character
45
+ t = np.linspace(0, 20, 100)
46
+ x = np.sin(t)
47
+ y = np.cos(t)
48
+ z = t/10
49
+
50
+ data = np.array([x, y, z])
51
+ line, = ax.plot(data[0, 0:1], data[1, 0:1], data[2, 0:1])
52
+
53
+ # Setting the axes properties
54
+ ax.set_xlim3d([-1.0, 1.0])
55
+ ax.set_xlabel('X')
56
+ ax.set_ylim3d([-1.0, 1.0])
57
+ ax.set_ylabel('Y')
58
+ ax.set_zlim3d([0.0, 2.0])
59
+ ax.set_zlabel('Z')
60
+
61
+ # Adding text
62
+ ax.text2D(0.05, 0.95, text[:50], transform=ax.transAxes)
63
+
64
+ ani = animation.FuncAnimation(fig, update_point, frames=100, fargs=(data, line), interval=100, blit=False)
65
+
66
+ # Save animation as gif
67
+ ani.save('character_animation.gif', writer='pillow')
68
+
69
+ return 'character_animation.gif'
70
 
71
  # Main function to generate comedy animation
72
  def generate_comedy_animation(prompt):
73
+ script = generate_text(f"Write a short comedy script about {prompt}: ")
74
+ animation_path = create_character_animation(script)
75
+ speech_path = generate_speech(script)
76
+ return script, animation_path, speech_path
77
 
78
  # Main function to generate kids music animation
79
  def generate_kids_music_animation(theme):
80
+ lyrics = generate_text(f"Write a short children's song about {theme}: ")
81
+ animation_path = create_character_animation(lyrics)
82
+ speech_path = generate_speech(lyrics)
83
+ return lyrics, animation_path, speech_path
84
 
85
  # Gradio Interface
86
  with gr.Blocks() as app:
87
+ gr.Markdown("## Advanced Character Animation Generator with Open Source Models")
88
 
89
  with gr.Tab("Comedy Animation"):
90
  comedy_prompt = gr.Textbox(label="Enter comedy prompt")
91
  comedy_generate_btn = gr.Button("Generate Comedy Animation")
92
  comedy_script = gr.Textbox(label="Generated Comedy Script")
93
  comedy_animation = gr.Image(label="Comedy Animation")
94
+ comedy_audio = gr.Audio(label="Comedy Speech")
95
 
96
  comedy_generate_btn.click(
97
  generate_comedy_animation,
98
  inputs=comedy_prompt,
99
+ outputs=[comedy_script, comedy_animation, comedy_audio]
100
  )
101
 
102
  with gr.Tab("Kids Music Animation"):
103
  kids_theme = gr.Textbox(label="Enter kids music theme")
104
  kids_generate_btn = gr.Button("Generate Kids Music Animation")
105
+ kids_lyrics = gr.Textbox(label="Generated Lyrics")
106
  kids_animation = gr.Image(label="Kids Music Animation")
107
+ kids_audio = gr.Audio(label="Kids Speech")
108
 
109
  kids_generate_btn.click(
110
  generate_kids_music_animation,
111
  inputs=kids_theme,
112
+ outputs=[kids_lyrics, kids_animation, kids_audio]
113
  )
114
 
115
  app.launch()