Bhavibond commited on
Commit
cab0672
·
verified ·
1 Parent(s): 1f7fa19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -38
app.py CHANGED
@@ -1,43 +1,76 @@
1
- import torch
2
  import gradio as gr
3
- from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
4
- from transformers import CLIPImageProcessor
5
- from PIL import Image
 
 
6
  import numpy as np
7
 
8
- def load_controlnet_model():
9
- """Load Stable Diffusion ControlNet pipeline with IP-Adapter."""
10
- controlnet = ControlNetModel.from_pretrained(
11
- "lllyasviel/sd-controlnet-depth", torch_dtype=torch.float16
12
- )
13
- pipe = StableDiffusionControlNetPipeline.from_pretrained(
14
- "runwayml/stable-diffusion-v1-5",
15
- controlnet=controlnet,
16
- torch_dtype=torch.float16
17
- ).to("cuda")
18
- return pipe
19
-
20
- def preprocess_image(image):
21
- """Convert image to depth map for ControlNet input."""
22
- image = image.convert("L") # Convert to grayscale
23
- image = np.array(image)
24
- depth_map = np.clip(image, 0, 255)
25
- return Image.fromarray(depth_map)
26
-
27
- def generate_image(input_image):
28
- """Generate an image using Stable Diffusion ControlNet with IP-Adapter."""
29
- pipe = load_controlnet_model()
30
- processed_image = preprocess_image(input_image)
31
- result = pipe(image=processed_image, num_inference_steps=30).images[0]
32
- return result
33
-
34
- # Gradio Interface
35
- demo = gr.Interface(
36
- fn=generate_image,
37
- inputs=gr.Image(type="pil", label="Upload reference image"),
38
- outputs=gr.Image(type="pil", label="Generated Image"),
39
- title="Stable Diffusion with ControlNet and IP-Adapter",
40
- description="Generate images with precise object placement and consistent style using ControlNet and IP-Adapter.",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  )
42
 
43
- demo.launch(share=True)
 
 
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()