Manasa1 commited on
Commit
6125413
·
verified ·
1 Parent(s): 736feaf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -158
app.py CHANGED
@@ -1,170 +1,80 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- from diffusers import StableDiffusionPipeline
4
  import torch
5
  from PIL import Image, ImageDraw, ImageFont
6
- import scipy.io.wavfile
7
- from TTS.api import TTS
8
- from moviepy.editor import CompositeVideoClip, ImageClip, AudioFileClip, concatenate_videoclips, VideoFileClip
9
  import os
10
- from groq import Groq
11
- import asyncio
12
- import aiohttp
13
- from dotenv import load_dotenv
14
- import speech_recognition as sr
15
-
16
- # Load environment variables
17
- load_dotenv()
18
-
19
- # Initialize Clients
20
- groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
21
-
22
- # Use GPT-3.5-turbo for text generation
23
- async def generate_comedy_script(prompt):
24
- chat_completion = await groq_client.chat.completions.create(
25
- messages=[
26
- {
27
- "role": "system",
28
- "content": "You are a comedy writer. Generate a short, funny script based on the given prompt."
29
- },
30
- {
31
- "role": "user",
32
- "content": prompt
33
- }
34
- ],
35
- model="mixtral-8x7b-32768",
36
- max_tokens=200
37
- )
38
- return chat_completion.choices[0].message.content
39
-
40
- # Use Coqui TTS for text-to-speech (unchanged)
41
- tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False, gpu=False)
42
-
43
- # Use MusicGen for music generation (unchanged)
44
- music_generator = pipeline("text-to-audio", model="facebook/musicgen-small", device="cpu")
45
-
46
- # Use Fluently Anime (Stable Diffusion) for anime image generation (unchanged)
47
- model_id = "fluently/Fluently-anime"
48
- anime_image_generator = StableDiffusionPipeline.from_pretrained(model_id).to("cpu")
49
-
50
- # Convert Text to Speech using Coqui TTS (unchanged)
51
- def text_to_speech(script):
52
- output_audio = 'output.wav'
53
- tts.tts_to_file(text=script, file_path=output_audio)
54
- return output_audio
55
-
56
- # Create Anime Images Using Fluently Anime (unchanged)
57
- def create_images_from_script(script):
58
- lines = script.split('. ')
59
- image_paths = []
60
- for i, line in enumerate(lines):
61
- img = anime_image_generator(line).images[0]
62
- img_path = f'/tmp/anime_image_{i}.png'
63
- img.save(img_path)
64
- image_paths.append(img_path)
65
- return image_paths
66
-
67
- # Generate Fun Music Track using MusicGen (unchanged)
68
- def generate_fun_music(prompt, output_music_file="fun_music.wav"):
69
- response = music_generator(prompt)
70
- audio_data = response["audio"]
71
- sampling_rate = response["sampling_rate"]
72
- scipy.io.wavfile.write(output_music_file, rate=sampling_rate, data=audio_data)
73
- return output_music_file
74
-
75
- # Create Video from Generated Anime Images (unchanged)
76
- def generate_text_video(script):
77
- image_paths = create_images_from_script(script)
78
- video_clip = ImageSequenceClip(image_paths, fps=24)
79
- video_path = "/tmp/final_video.mp4"
80
- video_clip.write_videofile(video_path, codec='libx264')
81
- return video_path
82
-
83
- # Combine Audio and Video (unchanged)
84
- def combine_audio_video(video_file, audio_file):
85
- video = VideoFileClip(video_file)
86
- audio = AudioFileClip(audio_file)
87
- final_video = video.set_audio(audio)
88
- return final_video
89
-
90
- # Main Function to Generate Comedy Animation
91
- async def generate_comedy_and_animation(prompt):
92
- script = await generate_comedy_script(prompt)
93
- audio_file = text_to_speech(script)
94
- video_file = generate_text_video(script)
95
- fun_music = generate_fun_music(prompt)
96
- final_video = combine_audio_video(video_file, fun_music)
97
- return script, audio_file, final_video
98
-
99
- # Generate Kids Content (unchanged)
100
- def generate_kids_content(theme):
101
- music_file = generate_fun_music(theme, output_music_file="kids_music.wav")
102
- clips = []
103
- for i in range(5):
104
- img = Image.new('RGB', (800, 400), color=(0, 0, 255))
105
- d = ImageDraw.Draw(img)
106
- fnt = ImageFont.load_default()
107
- d.text((10, 180), f"Kids Music: {theme}", font=fnt, fill=(255, 255, 0))
108
- frame_path = f'/tmp/kids_temp_{i}.png'
109
- img.save(frame_path)
110
- clips.append(ImageClip(frame_path).set_duration(1).set_position(('center', 'center')))
111
- final_video = concatenate_videoclips(clips, method="compose").set_audio(AudioFileClip(music_file))
112
- final_video.write_videofile("/tmp/kids_animation.mp4", fps=24)
113
- return music_file, "/tmp/kids_animation.mp4"
114
-
115
- # New function for speech-to-text using SpeechRecognition
116
- def transcribe_audio(audio_file):
117
- recognizer = sr.Recognizer()
118
- with sr.AudioFile(audio_file) as source:
119
- audio = recognizer.record(source)
120
- try:
121
- return recognizer.recognize_google(audio)
122
- except sr.UnknownValueError:
123
- return "Speech recognition could not understand the audio"
124
- except sr.RequestError as e:
125
- return f"Could not request results from speech recognition service; {e}"
126
 
127
  # Gradio Interface
128
  with gr.Blocks() as app:
129
- gr.Markdown("## AI Comedy and Kids Content Generator")
130
-
131
- # Comedy Animation Tab
132
- with gr.Tab("Generate Comedy Animation"):
133
- prompt_input = gr.Textbox(label="Comedy Prompt")
134
- generate_btn = gr.Button("Generate Comedy Script and Animation")
135
- comedy_script = gr.Textbox(label="Generated Script")
136
- comedy_audio = gr.Audio(label="Generated Audio")
137
- comedy_video = gr.Video(label="Generated Animation")
138
-
139
- generate_btn.click(
140
- generate_comedy_and_animation,
141
- inputs=prompt_input,
142
- outputs=[comedy_script, comedy_audio, comedy_video]
143
  )
144
-
145
- # Kids Music Animation Tab
146
- with gr.Tab("Generate Kids Music Animation"):
147
- theme_input = gr.Textbox(label="Kids Music Theme")
148
- generate_music_btn = gr.Button("Generate Kids Music and Animation")
149
- kids_music_audio = gr.Audio(label="Generated Music")
150
- kids_music_video = gr.Video(label="Generated Kids Animation")
151
-
152
- generate_music_btn.click(
153
- generate_kids_content,
154
- inputs=theme_input,
155
- outputs=[kids_music_audio, kids_music_video]
156
- )
157
-
158
- # Speech-to-Text Tab
159
- with gr.Tab("Speech-to-Text"):
160
- audio_input = gr.Audio(label="Upload Audio")
161
- transcribe_btn = gr.Button("Transcribe Audio")
162
- transcription_output = gr.Textbox(label="Transcription")
163
-
164
- transcribe_btn.click(
165
- transcribe_audio,
166
- inputs=audio_input,
167
- outputs=transcription_output
168
  )
169
 
170
  app.launch()
 
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()