Manasa1 commited on
Commit
d5bd16f
·
verified ·
1 Parent(s): 3b97054

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -12
app.py CHANGED
@@ -1,16 +1,18 @@
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
  from PIL import Image
7
  from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
8
  from torchvision.io import write_video
9
  import os
 
10
 
11
- # Initialize text generation model (GPT-2)
12
- tokenizer = AutoTokenizer.from_pretrained("gpt2")
13
- model = AutoModelForCausalLM.from_pretrained("gpt2")
 
14
 
15
  # Initialize TTS model
16
  tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC")
@@ -20,11 +22,23 @@ pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4",
20
  pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
21
  pipe = pipe.to("cpu")
22
 
23
- def generate_text(prompt, max_length=200):
24
- input_ids = tokenizer.encode(prompt, return_tensors="pt")
25
- attention_mask = torch.ones_like(input_ids)
26
- output = model.generate(input_ids, attention_mask=attention_mask, max_length=max_length, num_return_sequences=1, no_repeat_ngram_size=2, pad_token_id=tokenizer.eos_token_id)
27
- return tokenizer.decode(output[0], skip_special_tokens=True)
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  def generate_speech(text):
30
  output_path = "generated_speech.wav"
@@ -46,7 +60,12 @@ def create_video_from_frames(frames, output_path="output_video.mp4", fps=5):
46
  return output_path
47
 
48
  def generate_comedy_animation(prompt):
49
- script = generate_text(f"Write a short comedy script with two characters about {prompt}. Use the format 'Character: Action' for each line of dialogue or action.")
 
 
 
 
 
50
  video_prompt = f"A comedic scene with two characters: {prompt}"
51
  frames = generate_video_frames(video_prompt)
52
  video_path = create_video_from_frames(frames)
@@ -54,7 +73,11 @@ def generate_comedy_animation(prompt):
54
  return script, video_path, speech_path
55
 
56
  def generate_kids_music_animation(theme):
57
- lyrics = generate_text(f"Write short and simple lyrics for a children's song about {theme}. Each line should be on a new line. Don't include 'Verse' or 'Chorus' labels.")
 
 
 
 
58
  video_prompt = f"A colorful, animated music video for children about {theme}"
59
  frames = generate_video_frames(video_prompt)
60
  video_path = create_video_from_frames(frames)
@@ -63,7 +86,7 @@ def generate_kids_music_animation(theme):
63
 
64
  # Gradio Interface
65
  with gr.Blocks() as app:
66
- gr.Markdown("## AI-Generated Video and Audio Content (CPU Version)")
67
 
68
  with gr.Tab("Comedy Animation"):
69
  comedy_prompt = gr.Textbox(label="Enter comedy prompt")
 
1
  import gradio as gr
2
  import torch
3
+ from transformers import AutoTokenizer
4
  from TTS.api import TTS
5
  import numpy as np
6
  from PIL import Image
7
  from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
8
  from torchvision.io import write_video
9
  import os
10
+ import groq
11
 
12
+ # Initialize Groq client
13
+ groq_client = groq.Groq()
14
+ API_KEY = "GROQ_API_KEY"
15
+ groq_client.api_key = API_KEY
16
 
17
  # Initialize TTS model
18
  tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC")
 
22
  pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
23
  pipe = pipe.to("cpu")
24
 
25
+ def generate_text_with_groq(prompt, max_tokens=200):
26
+ chat_completion = groq_client.chat.completions.create(
27
+ messages=[
28
+ {
29
+ "role": "system",
30
+ "content": "You are a professional comedy writer skilled in creating short, witty scripts."
31
+ },
32
+ {
33
+ "role": "user",
34
+ "content": prompt
35
+ }
36
+ ],
37
+ model="mixtral-8x7b-32768",
38
+ max_tokens=max_tokens,
39
+ temperature=0.7,
40
+ )
41
+ return chat_completion.choices[0].message.content
42
 
43
  def generate_speech(text):
44
  output_path = "generated_speech.wav"
 
60
  return output_path
61
 
62
  def generate_comedy_animation(prompt):
63
+ script_prompt = f"""Write a short, witty comedy script with two characters about {prompt}.
64
+ Use the format 'Character: Dialogue or Action' for each line.
65
+ Include clever wordplay, unexpected twists, and snappy dialogue.
66
+ Keep it concise, around 5-8 exchanges. Make it genuinely funny!"""
67
+
68
+ script = generate_text_with_groq(script_prompt)
69
  video_prompt = f"A comedic scene with two characters: {prompt}"
70
  frames = generate_video_frames(video_prompt)
71
  video_path = create_video_from_frames(frames)
 
73
  return script, video_path, speech_path
74
 
75
  def generate_kids_music_animation(theme):
76
+ lyrics_prompt = f"""Write short, catchy, and simple lyrics for a children's song about {theme}.
77
+ Each line should be on a new line. Don't include 'Verse' or 'Chorus' labels.
78
+ Make it educational, fun, and easy to remember. Include a repeating chorus."""
79
+
80
+ lyrics = generate_text_with_groq(lyrics_prompt)
81
  video_prompt = f"A colorful, animated music video for children about {theme}"
82
  frames = generate_video_frames(video_prompt)
83
  video_path = create_video_from_frames(frames)
 
86
 
87
  # Gradio Interface
88
  with gr.Blocks() as app:
89
+ gr.Markdown("## AI-Generated Video and Audio Content (Optimized CPU Version with Groq API)")
90
 
91
  with gr.Tab("Comedy Animation"):
92
  comedy_prompt = gr.Textbox(label="Enter comedy prompt")