Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
from diffusers import StableDiffusionPipeline
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Check for GPU availability
|
7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
8 |
+
|
9 |
+
# Set appropriate dtype based on device
|
10 |
+
torch_dtype = torch.float16 if device == "cuda" else torch.float32
|
11 |
+
|
12 |
+
# Load sentiment analysis model
|
13 |
+
@st.cache_resource
|
14 |
+
def load_sentiment_analyzer():
|
15 |
+
return pipeline("text-classification", model="SamLowe/roberta-base-go_emotions", top_k=5, device=device)
|
16 |
+
|
17 |
+
# Load text-to-image model
|
18 |
+
@st.cache_resource
|
19 |
+
def load_text2img():
|
20 |
+
model = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch_dtype)
|
21 |
+
model = model.to(device)
|
22 |
+
return model
|
23 |
+
|
24 |
+
sentiment_analyzer = load_sentiment_analyzer()
|
25 |
+
text2img = load_text2img()
|
26 |
+
|
27 |
+
def analyze_sentiment(text):
|
28 |
+
results = sentiment_analyzer(text)
|
29 |
+
emotions = [(result['label'], result['score']) for result in results[0]]
|
30 |
+
return emotions
|
31 |
+
|
32 |
+
def generate_prompt(emotions):
|
33 |
+
era_styles = {
|
34 |
+
"joy": ("Impressionist", "Claude Monet"),
|
35 |
+
"sadness": ("Romantic", "Caspar David Friedrich"),
|
36 |
+
"anger": ("Expressionist", "Edvard Munch"),
|
37 |
+
"fear": ("Surrealist", "Salvador Dali"),
|
38 |
+
"disgust": ("Abstract", "Jackson Pollock"),
|
39 |
+
"surprise": ("Pop Art", "Andy Warhol"),
|
40 |
+
"neutral": ("Minimalist", "Piet Mondrian"),
|
41 |
+
"love": ("Renaissance", "Sandro Botticelli"),
|
42 |
+
"admiration": ("Baroque", "Rembrandt"),
|
43 |
+
"approval": ("Neoclassical", "Jacques-Louis David"),
|
44 |
+
"caring": ("Pre-Raphaelite", "John Everett Millais"),
|
45 |
+
"excitement": ("Fauvism", "Henri Matisse"),
|
46 |
+
"gratitude": ("Rococo", "Jean-Honoré Fragonard"),
|
47 |
+
"pride": ("Art Nouveau", "Gustav Klimt"),
|
48 |
+
"relief": ("Pointillism", "Georges Seurat"),
|
49 |
+
"remorse": ("Symbolism", "Odilon Redon"),
|
50 |
+
"confusion": ("Cubism", "Pablo Picasso"),
|
51 |
+
"curiosity": ("Futurism", "Umberto Boccioni"),
|
52 |
+
"desire": ("Art Deco", "Tamara de Lempicka"),
|
53 |
+
"disapproval": ("Dada", "Marcel Duchamp"),
|
54 |
+
"embarrassment": ("Naive Art", "Henri Rousseau"),
|
55 |
+
"nervousness": ("Constructivism", "Vladimir Tatlin"),
|
56 |
+
"optimism": ("De Stijl", "Theo van Doesburg"),
|
57 |
+
"realization": ("Bauhaus", "Wassily Kandinsky"),
|
58 |
+
"amusement": ("Suprematism", "Kazimir Malevich"),
|
59 |
+
"annoyed": ("Social Realism", "Diego Rivera"),
|
60 |
+
"disappointment": ("Color Field", "Mark Rothko"),
|
61 |
+
"grief": ("Neo-Expressionism", "Jean-Michel Basquiat")
|
62 |
+
}
|
63 |
+
|
64 |
+
prompt_parts = []
|
65 |
+
for emotion, _ in emotions:
|
66 |
+
style, artist = era_styles.get(emotion, ("Contemporary", "Various"))
|
67 |
+
prompt_parts.append(f"a {style} painting in the style of {artist} depicting {emotion}")
|
68 |
+
|
69 |
+
return "Create a multi-style artwork combining " + ", ".join(prompt_parts)
|
70 |
+
|
71 |
+
def generate_image(prompt):
|
72 |
+
image = text2img(prompt, num_inference_steps=50).images[0]
|
73 |
+
return image
|
74 |
+
|
75 |
+
def main():
|
76 |
+
st.title("MoodAlbum - Visualise your feelings.")
|
77 |
+
|
78 |
+
user_text = st.text_area("How are you feeling? Describe your emotions:")
|
79 |
+
|
80 |
+
if st.button("Generate Painting"):
|
81 |
+
if user_text:
|
82 |
+
with st.spinner("Analyzing emotions..."):
|
83 |
+
emotions = analyze_sentiment(user_text)
|
84 |
+
|
85 |
+
st.write("Detected emotions:")
|
86 |
+
for emotion, score in emotions:
|
87 |
+
st.write(f"- {emotion}")
|
88 |
+
|
89 |
+
prompt = generate_prompt(emotions)
|
90 |
+
st.write("Generated prompt:")
|
91 |
+
st.write(prompt)
|
92 |
+
|
93 |
+
with st.spinner("Generating painting... This may take a while."):
|
94 |
+
image = generate_image(prompt)
|
95 |
+
|
96 |
+
st.image(image, caption="Your feelings coming to life!", use_column_width=True)
|
97 |
+
else:
|
98 |
+
st.warning("Please enter some text describing your emotions.")
|
99 |
+
|
100 |
+
if __name__ == "__main__":
|
101 |
+
main()
|