Komal-patra commited on
Commit
9a32a08
1 Parent(s): 7713fd5

updated app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from langchain.chat_models import ChatOpenAI
3
+ from langchain.prompts import PromptTemplate
4
+ from langchain.chains.llm import LLMChain
5
+ from openai import OpenAI
6
+ import os
7
+ import requests
8
+ from PIL import Image as PILImage
9
+ from io import BytesIO
10
+
11
+ # Initialize the OpenAI API key
12
+ openai_api_key = os.environ['OPENAI_API_KEY']
13
+
14
+ # Initialize the OpenAI client
15
+ client = OpenAI(api_key=openai_api_key)
16
+
17
+ # Initialize the ChatOpenAI model
18
+ openai_llm = ChatOpenAI()
19
+
20
+ # Define the prompt template
21
+ prompt_template = PromptTemplate.from_template(
22
+ """Imagine '{genre}' Album Cover and provide the detailed prompt for the Following album/song/lyrics: '{lyrics}' to create the realistic imaginery graphic for the album/music chorus by understanding and mimicing the emotional depth with the emotional tone of '{emotion}' .
23
+
24
+ Please ensure that the genre of the lyrics matches the provided genre parameter: 'Rock' for powerful guitar riffs, rebellious energy, and raw emotion; 'Pop' for catchy hooks, vibrant colors, and trendy aesthetics; 'Metal' for dark, intense imagery with heavy and aggressive tones; 'Folk' for earthy, acoustic elements with natural and rustic visuals; '80s' for retro-futuristic vibes, neon colors, and nostalgic references; 'Jazz' for smooth, elegant, and sophisticated visuals with a touch of improvisation; 'K-pop' for sleek, modern, and colorful designs with a polished, high-energy look; and 'Country' for rustic, heartfelt, and down-to-earth imagery often set in rural or natural environments.
25
+
26
+ For emotions, match the tone: cinematic night street scene 'Heartbreak' for visuals with 28 year old Rock chick model, 'Fantasy' for cinematic, 'Dark' for high contrast, city lights.
27
+
28
+ Consider the age and gender of the subject(s) in the lyrics to ensure the visuals align with their emotional experience, whether depicting a young woman in the throes of first love, an older man reflecting on past sorrows, or a child lost in a fantasy world.
29
+
30
+ The visual style should be photo-realistic, 8K resolution, hyper-detailed, and cinematic. Include elements such as high-contrast lighting, intricate detail, and realistic textures. Depending on the emotion, use glamour photography for 'Fantasy', stark contrasts for 'Dark', and soft, muted tones for 'Heartbreak'. The image should also reflect a high-fashion or art-inspired aesthetic, with influences from award-winning photography styles, cinematic lighting, and advanced rendering techniques to create a masterpiece that is both visually stunning and emotionally evocative.
31
+ """
32
+
33
+ )
34
+
35
+ # Define the lyrics response chain
36
+ lyrics_response_chain = LLMChain(llm=openai_llm, prompt=prompt_template, verbose=True)
37
+
38
+ # Create a directory for saving images if it doesn't exist
39
+ os.makedirs("generated_images", exist_ok=True)
40
+
41
+ # Function to generate and save image based on lyrics, genre, and emotion
42
+ def generate_image(lyrics, genre, emotion):
43
+ try:
44
+ formatted_prompt = prompt_template.format(lyrics=lyrics, genre=genre, emotion=emotion)
45
+ response = lyrics_response_chain.run({'lyrics': lyrics, 'genre': genre, 'emotion': emotion})
46
+
47
+ # Generate the image using DALL-E 3
48
+ image_response = client.images.generate(
49
+ model="dall-e-3",
50
+ prompt=response,
51
+ size="1024x1024",
52
+ quality="hd",
53
+ n=1,
54
+ )
55
+
56
+ image_url = image_response.data[0].url
57
+
58
+ # Save the image locally
59
+ response = requests.get(image_url)
60
+ image = PILImage.open(BytesIO(response.content))
61
+ file_path = os.path.join("generated_images", "album_cover.png")
62
+ image.save(file_path)
63
+
64
+ return image_url
65
+
66
+ except Exception as e:
67
+ print(f"An error occurred: {e}")
68
+ return None
69
+
70
+ # Create Gradio interface
71
+ def gradio_interface():
72
+ lyrics_input = gr.Textbox(label="Lyrics", placeholder="Enter the song lyrics here...")
73
+ genre_input = gr.Dropdown(label="Genre", choices=['Rock', 'Pop', 'Metal', 'Folk', '80s', 'Jazz', 'K-pop', 'Country'])
74
+ emotion_input = gr.Dropdown(label="Emotion", choices=['Heartbreak', 'Fantasy', 'Dark'])
75
+ output_image = gr.Image(label="Generated Album Cover")
76
+
77
+ gr.Interface(fn=generate_image,
78
+ inputs=[lyrics_input, genre_input, emotion_input],
79
+ outputs=output_image,
80
+ title="Lyrics Canvas").launch()
81
+
82
+ # Run the Gradio interface
83
+ gradio_interface()