Bhavibond commited on
Commit
d450bfe
·
verified ·
1 Parent(s): 075eec8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -65
app.py CHANGED
@@ -1,76 +1,47 @@
1
  import gradio as gr
2
  import torch
3
  from diffusers import StableDiffusionPipeline
4
- from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech
5
- import openai
6
- import cv2
7
- import numpy as np
8
-
9
- # Load Stable Diffusion pipeline
10
- sd_pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
11
- if torch.cuda.is_available():
12
- sd_pipe.to("cuda")
13
-
14
- # Load AI voiceover model
15
- processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
16
- tts_model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")
17
-
18
- # OpenAI API key for voice and physics interactions
19
- openai.api_key = "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"
20
-
21
- def generate_image(prompt, emotion):
22
- emotion_map = {"Happy": "smiling", "Angry": "frowning", "Sad": "crying"}
23
- prompt = f"{prompt}, {emotion_map.get(emotion, '')}"
24
- image = sd_pipe(prompt).images[0]
25
- return image
26
-
27
- def generate_voiceover(text):
28
- inputs = processor(text, return_tensors="pt")
29
- speech = tts_model.generate(**inputs)
30
- return speech
31
-
32
- def apply_physics(image, effect):
33
- img_array = np.array(image)
34
- if effect == "Wind":
35
- kernel = np.array([[1, 1, 1], [0, 0, 0], [-1, -1, -1]])
36
- img_array = cv2.filter2D(img_array, -1, kernel)
37
- elif effect == "Falling":
38
- img_array = np.roll(img_array, 10, axis=0)
39
- return img_array
40
-
41
- def track_facial_expressions():
42
- cap = cv2.VideoCapture(0)
43
- face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
44
- while True:
45
- ret, frame = cap.read()
46
- gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
47
- faces = face_cascade.detectMultiScale(gray, 1.3, 5)
48
- for (x, y, w, h) in faces:
49
- cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
50
- cv2.imshow('Face Tracking', frame)
51
- if cv2.waitKey(1) & 0xFF == ord('q'):
52
- break
53
- cap.release()
54
- cv2.destroyAllWindows()
55
-
56
- def animate_scene(prompt, emotion, physics, text):
57
- image = generate_image(prompt, emotion)
58
- image = apply_physics(image, physics)
59
- speech = generate_voiceover(text)
60
- return image, speech
61
-
62
- iface = gr.Interface(
63
- fn=animate_scene,
64
  inputs=[
65
  gr.Textbox(label="Character Description"),
66
  gr.Radio(["Happy", "Angry", "Sad"], label="Emotion"),
67
  gr.Radio(["None", "Wind", "Falling"], label="Physics Effect"),
68
  gr.Textbox(label="Dialogue")
69
  ],
70
- outputs=[
71
- gr.Image(label="Generated Scene"),
72
- gr.Audio(label="Voiceover")
73
- ]
74
  )
75
 
76
- iface.launch()
 
1
  import gradio as gr
2
  import torch
3
  from diffusers import StableDiffusionPipeline
4
+ from transformers import pipeline
5
+ from gtts import gTTS
6
+ import os
7
+
8
+ def generate_character(description, emotion, physics, dialogue):
9
+ # Load Stable Diffusion Model (Optimized for Speed)
10
+ pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
11
+ pipe.to("cuda" if torch.cuda.is_available() else "cpu")
12
+
13
+ # Modify description based on emotion
14
+ emotion_map = {"Happy": "smiling", "Angry": "furious", "Sad": "teary-eyed"}
15
+ description = f"{description}, {emotion_map.get(emotion, '')}"
16
+
17
+ # Generate Image
18
+ image = pipe(description).images[0]
19
+ image.save("output.png")
20
+
21
+ # Generate Voiceover
22
+ try:
23
+ tts_model = pipeline("text-to-speech", model="facebook/mms-tts-eng")
24
+ speech_output = tts_model(dialogue)
25
+ speech_path = "output.wav"
26
+ with open(speech_path, "wb") as f:
27
+ f.write(speech_output["audio"])
28
+ except Exception:
29
+ tts = gTTS(dialogue)
30
+ speech_path = "output.mp3"
31
+ tts.save(speech_path)
32
+
33
+ return image, speech_path
34
+
35
+ gui = gr.Interface(
36
+ fn=generate_character,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  inputs=[
38
  gr.Textbox(label="Character Description"),
39
  gr.Radio(["Happy", "Angry", "Sad"], label="Emotion"),
40
  gr.Radio(["None", "Wind", "Falling"], label="Physics Effect"),
41
  gr.Textbox(label="Dialogue")
42
  ],
43
+ outputs=[gr.Image(label="Generated Character"), gr.Audio(label="Voiceover")],
44
+ allow_flagging="never"
 
 
45
  )
46
 
47
+ gui.launch(debug=True)