Manasa1 commited on
Commit
0d07348
·
verified ·
1 Parent(s): 2de0b32

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ from pydub import AudioSegment
3
+
4
+ def generate_kids_music(theme):
5
+ # Create a simple melody based on themes
6
+ notes = {
7
+ "birthday": ["C4", "E4", "G4", "C5"],
8
+ "ocean": ["C4", "D4", "E4", "F4"],
9
+ "adventure": ["E4", "G4", "A4", "C5"]
10
+ }
11
+
12
+ selected_notes = notes.get(theme, ["C4", "E4", "G4"]) # Default notes if theme not found
13
+ music = AudioSegment.silent(duration=1000) # Start with 1 second of silence
14
+
15
+ for note in selected_notes:
16
+ # Generate a simple sine wave for each note
17
+ frequency = 261.63 if note == "C4" else 329.63 if note == "E4" else 392.00 if note == "G4" else 523.25
18
+ sine_wave = AudioSegment.sine(frequency=frequency, duration=500) # 0.5 second for each note
19
+ music += sine_wave
20
+
21
+ # Export the generated music
22
+ output_file = f"{theme}_kids_music.wav"
23
+ music.export(output_file, format="wav")
24
+ return output_file
25
+
26
+ from transformers import pipeline
27
+
28
+ # Load the GPT-2 model for text generation
29
+ comedy_generator = pipeline('text-generation', model='gpt2')
30
+
31
+ def generate_comedy_script(prompt):
32
+ result = comedy_generator(prompt, max_length=150, num_return_sequences=1)
33
+ return result[0]['generated_text']
34
+
35
+ from gtts import gTTS
36
+
37
+ def script_to_audio(script):
38
+ tts = gTTS(script, lang='en')
39
+ audio_file = "comedy_script.mp3"
40
+ tts.save(audio_file)
41
+ return audio_file
42
+
43
+ import gradio as gr
44
+
45
+ def generate_music_and_comedy(theme, prompt):
46
+ # Generate music
47
+ music_file = generate_kids_music(theme)
48
+
49
+ # Generate comedy script
50
+ comedy_script = generate_comedy_script(prompt)
51
+
52
+ # Convert the comedy script to audio
53
+ audio_file = script_to_audio(comedy_script)
54
+
55
+ return music_file, comedy_script, audio_file
56
+
57
+ # Gradio Interface
58
+ iface = gr.Interface(
59
+ fn=generate_music_and_comedy,
60
+ inputs=[
61
+ gr.Dropdown(choices=["birthday", "ocean", "adventure"], label="Select Theme for Kids' Music"),
62
+ gr.Textbox(lines=2, placeholder="Enter a prompt for comedy...", label="Comedy Prompt")
63
+ ],
64
+ outputs=[
65
+ gr.Audio(label="Generated Kids' Music"),
66
+ gr.Textbox(label="Generated Comedy Script"),
67
+ gr.Audio(label="Comedy Audio")
68
+ ],
69
+ title="AI Music and Comedy Generator",
70
+ description="Generate unique music for kids based on themes and comedic scripts from your prompts."
71
+ )
72
+
73
+ # Launch the app
74
+ iface.launch()
75
+