Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from diffusers import StableDiffusionPipeline
|
4 |
+
from groq import Groq
|
5 |
+
|
6 |
+
# Initialize the Stable Diffusion model
|
7 |
+
model_id = "stabilityai/stable-diffusion-2" # Example model
|
8 |
+
sd_pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
9 |
+
sd_pipe.to("cuda")
|
10 |
+
|
11 |
+
# Initialize the Groq client with your API key
|
12 |
+
client = Groq(api_key="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")
|
13 |
+
|
14 |
+
# Define the system message for the model
|
15 |
+
system_message = {
|
16 |
+
"role": "system",
|
17 |
+
"content": "You are an experienced scriptwriter who generates engaging and creative movie content based on a given theme. Provide main characters, a storyline, and a movie script."
|
18 |
+
}
|
19 |
+
|
20 |
+
def generate_movie_content(theme):
|
21 |
+
# Generate main characters and storyline using Groq
|
22 |
+
setup_prompt = f"Generate main characters and a storyline for a movie with the theme: '{theme}'."
|
23 |
+
response = client.chat(messages=[
|
24 |
+
system_message,
|
25 |
+
{"role": "user", "content": setup_prompt}
|
26 |
+
])
|
27 |
+
setup = response["choices"][0]["message"]["content"]
|
28 |
+
|
29 |
+
# Extract characters and storyline
|
30 |
+
if "Main Characters:" in setup and "Storyline:" in setup:
|
31 |
+
characters_start = setup.index("Main Characters:") + len("Main Characters:")
|
32 |
+
storyline_start = setup.index("Storyline:") + len("Storyline:")
|
33 |
+
characters = setup[characters_start:storyline_start].strip()
|
34 |
+
storyline = setup[storyline_start:].strip()
|
35 |
+
else:
|
36 |
+
characters = "Unknown characters"
|
37 |
+
storyline = "Unknown storyline"
|
38 |
+
|
39 |
+
# Generate movie script using Groq
|
40 |
+
script_prompt = (
|
41 |
+
f"Write a movie script based on the theme: '{theme}', with main characters: {characters}, and storyline: {storyline}."
|
42 |
+
)
|
43 |
+
script_response = client.chat(messages=[
|
44 |
+
system_message,
|
45 |
+
{"role": "user", "content": script_prompt}
|
46 |
+
])
|
47 |
+
script = script_response["choices"][0]["message"]["content"]
|
48 |
+
|
49 |
+
# Generate image
|
50 |
+
image_prompt = f"A cinematic illustration of the theme: {theme}"
|
51 |
+
image = sd_pipe(image_prompt).images[0]
|
52 |
+
|
53 |
+
return characters, storyline, script, image
|
54 |
+
|
55 |
+
def main_interface(theme):
|
56 |
+
characters, storyline, script, image = generate_movie_content(theme)
|
57 |
+
return f"Main Characters: {characters}\n\nStoryline: {storyline}\n\nScript: {script}", image
|
58 |
+
|
59 |
+
# Gradio Interface
|
60 |
+
title = "Movie Generator"
|
61 |
+
description = "Generate movie scripts, characters, and thematic images based on a single theme."
|
62 |
+
|
63 |
+
demo = gr.Interface(
|
64 |
+
fn=main_interface,
|
65 |
+
inputs=[
|
66 |
+
gr.Textbox(label="Theme", placeholder="Enter the movie theme"),
|
67 |
+
],
|
68 |
+
outputs=[
|
69 |
+
gr.Textbox(label="Generated Content"),
|
70 |
+
gr.Image(label="Generated Image"),
|
71 |
+
],
|
72 |
+
title=title,
|
73 |
+
description=description,
|
74 |
+
)
|
75 |
+
|
76 |
+
if __name__ == "__main__":
|
77 |
+
demo.launch()
|