esocoder commited on
Commit
996aa19
1 Parent(s): dda7820

first commit

Browse files
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ GROQ_API_KEY="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"
analyze_poems.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ import matplotlib.pyplot as plt
4
+ import pandas as pd
5
+ from sklearn.cluster import KMeans
6
+ from sklearn.preprocessing import StandardScaler
7
+ from transformers import pipeline
8
+ import nltk
9
+ import numpy as np
10
+ from utils import read_poems_from_directory, emotion_labels_with_colors
11
+
12
+ # Download nltk data for tokenization
13
+ nltk.download('punkt')
14
+
15
+ # Initialize emotion classifier pipelines
16
+ models = {
17
+ "Model 1": pipeline('sentiment-analysis', model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True),
18
+ "Model 2": pipeline('sentiment-analysis', model="cardiffnlp/twitter-roberta-base-emotion", return_all_scores=True)
19
+ }
20
+
21
+ poems_directory = "./poems"
22
+ os.makedirs(poems_directory, exist_ok=True)
23
+
24
+ def analyze_poems_page():
25
+ st.header("Analyze Poems")
26
+
27
+ # Sidebar for file upload and listing files
28
+ st.sidebar.title("Upload New Poem")
29
+ uploaded_file = st.sidebar.file_uploader("Choose a text file", type="txt")
30
+
31
+ if uploaded_file is not None:
32
+ with open(os.path.join(poems_directory, uploaded_file.name), "wb") as f:
33
+ f.write(uploaded_file.getbuffer())
34
+ st.sidebar.success(f"Uploaded {uploaded_file.name}")
35
+
36
+ st.sidebar.title("Available Poems")
37
+ poem_files = [f for f in os.listdir(poems_directory) if f.endswith(".txt")]
38
+ st.sidebar.write("\n".join(poem_files))
39
+
40
+ # Sidebar input for user-specified labels
41
+ user_labels_input = st.sidebar.text_input("Enter emotion labels (comma-separated)",
42
+ "happiness,sadness,anger,fear,disgust,surprise,love,joy,anxiety,contentment,frustration,loneliness,excitement,guilt,shame,envy,jealousy,pride,gratitude,empathy,compassion,boredom,relief,curiosity,awe,confusion,nostalgia,hope,despair,embarrassment")
43
+ user_labels = [label.strip() for label in user_labels_input.split(",")]
44
+
45
+ if st.button("Analyze Poems"):
46
+ if os.path.isdir(poems_directory):
47
+ # Read poems from the specified directory
48
+ poems = read_poems_from_directory(poems_directory)
49
+
50
+ if poems:
51
+ def analyze_emotions(poem, model):
52
+ lines = nltk.sent_tokenize(poem)
53
+ emotions = []
54
+ for line in lines:
55
+ result = model(line)
56
+ emotions.append(result)
57
+ return emotions
58
+
59
+ def process_emotions(emotions):
60
+ emotion_scores = []
61
+ all_labels = set()
62
+ for line_emotions in emotions:
63
+ line_score = {emo['label']: emo['score'] for emo in line_emotions[0]}
64
+ all_labels.update(line_score.keys())
65
+ emotion_scores.append(line_score)
66
+ return emotion_scores, all_labels
67
+
68
+ def plot_emotional_arc(processed_emotions, labels, model_name):
69
+ st.subheader(model_name)
70
+ plt.figure(figsize=(15, 10))
71
+ for i, emotions in enumerate(processed_emotions):
72
+ for emotion in labels:
73
+ emotion_arc = [line_emotions.get(emotion, 0) for line_emotions in emotions]
74
+ color = emotion_labels_with_colors.get(emotion, 'black') # default to black if not found
75
+ plt.plot(emotion_arc, label=f'Poem {i+1} - {emotion}', color=color)
76
+ plt.title(f'Emotional Arc of Each Poem ({model_name})')
77
+ plt.xlabel('Line Number')
78
+ plt.ylabel('Emotion Score')
79
+ plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
80
+ st.pyplot(plt)
81
+
82
+ def extract_features(emotion_data, labels):
83
+ features = []
84
+ for emotions in emotion_data:
85
+ poem_features = []
86
+ for label in labels:
87
+ scores = [line_emotions.get(label, 0) for line_emotions in emotions]
88
+ mean_score = np.mean(scores)
89
+ std_score = np.std(scores)
90
+ poem_features.extend([mean_score, std_score])
91
+ features.append(poem_features)
92
+ return features
93
+
94
+ # Analyze and plot for each model
95
+ for model_name, model in models.items():
96
+ poem_emotions = [analyze_emotions(poem, model) for poem in poems]
97
+ processed_emotions = []
98
+ all_labels = set()
99
+ for emotions in poem_emotions:
100
+ processed, labels = process_emotions(emotions)
101
+ processed_emotions.append(processed)
102
+ all_labels.update(labels)
103
+ selected_labels = [label for label in user_labels if label in all_labels]
104
+ plot_emotional_arc(processed_emotions, selected_labels, model_name)
105
+
106
+ # Extract features for clustering
107
+ features = extract_features(processed_emotions, selected_labels)
108
+
109
+ # Create a DataFrame to store the features
110
+ columns = []
111
+ for label in selected_labels:
112
+ columns.extend([f'{label}_mean', f'{label}_std'])
113
+ df = pd.DataFrame(features, columns=columns)
114
+
115
+ # Standardize the features
116
+ scaler = StandardScaler()
117
+ scaled_features = scaler.fit_transform(df)
118
+
119
+ # Apply KMeans clustering
120
+ kmeans = KMeans(n_clusters=2, random_state=42)
121
+ kmeans.fit(scaled_features)
122
+ df['Cluster'] = kmeans.labels_
123
+
124
+ # Display the DataFrame
125
+ st.write(f"Poem Sentiment Features and Clusters ({model_name}):")
126
+ st.dataframe(df)
127
+
128
+ # Visualize the clusters
129
+ if not df.empty:
130
+ plt.figure(figsize=(8, 6))
131
+ plt.scatter(df.iloc[:, 0], df.iloc[:, 1], c=df['Cluster'], cmap='viridis', marker='o')
132
+ plt.title(f'Clusters of Poem Emotional Arcs ({model_name})')
133
+ plt.xlabel(f'{columns[0]}')
134
+ plt.ylabel(f'{columns[1]}')
135
+ st.pyplot(plt)
136
+ else:
137
+ st.warning("No text files found in the specified directory.")
138
+ else:
139
+ st.error("The specified path is not a valid directory.")
app.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from generate_poems import generate_poems_page
3
+ from analyze_poems import analyze_poems_page
4
+ from graph_guided_learning import graph_guided_learning_page
5
+ from individual_analyzes import analyze_individual_page
6
+
7
+ st.title("Poem Analysis and Generation")
8
+
9
+ # Sidebar for navigation
10
+ page = st.sidebar.selectbox("Choose a page", ["Overview sentiment analyzsis", "individual sentiment analyzis", "Generate Poems", "Graph Guided Learning"])
11
+
12
+ if page == "Overview sentiment analyzsis":
13
+ analyze_poems_page()
14
+ elif page == "individual sentiment analyzis":
15
+ analyze_individual_page()
16
+ elif page == "Generate Poems":
17
+ generate_poems_page()
18
+ elif page == "Graph Guided Learning":
19
+ graph_guided_learning_page()
generate_poems.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ from dotenv import load_dotenv
4
+ from groq import Groq
5
+
6
+ load_dotenv()
7
+
8
+ # Initialize Groq client
9
+ client = Groq(api_key=os.getenv("GROQ_API_KEY"))
10
+
11
+ # Directory for poems
12
+ poems_directory = "./poems"
13
+ os.makedirs(poems_directory, exist_ok=True)
14
+
15
+ def generate_poems(num_poems, length_per_poem, output_dir, instructions):
16
+ if not os.path.exists(output_dir):
17
+ os.makedirs(output_dir)
18
+ for i in range(num_poems):
19
+ chat_completion = client.chat.completions.create(
20
+ messages=[
21
+ {"role": "system", "content": "You are a talented poet."},
22
+ {"role": "user", "content": f"{instructions}\n Make sure all the poems have the same length of {length_per_poem} tokens."}
23
+ ],
24
+ model="llama3-8b-8192",
25
+ temperature=0.7,
26
+ max_tokens=length_per_poem,
27
+ top_p=1,
28
+ stop=None,
29
+ stream=False,
30
+ )
31
+ poem = chat_completion.choices[0].message.content.strip()
32
+ with open(os.path.join(output_dir, f'poem_{i + 1}.txt'), 'w') as f:
33
+ f.write(poem)
34
+
35
+ def generate_poems_page():
36
+ st.header("Generate Poems")
37
+
38
+ # Input fields for poem generation parameters
39
+ num_poems = st.number_input("Number of poems to generate", min_value=1, max_value=100, value=5)
40
+ length_per_poem = st.number_input("Length of each poem in tokens", min_value=50, max_value=1000, value=150)
41
+ instructions = st.text_area("Custom instructions", "You are a talented poet.")
42
+
43
+ if st.button("Generate Poems"):
44
+ generate_poems(num_poems, length_per_poem, poems_directory, instructions)
45
+ st.success(f"Generated {num_poems} poems and saved to {poems_directory}")
graph_guided_learning.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ import networkx as nx
4
+ import torch
5
+ import matplotlib.pyplot as plt
6
+ from torch_geometric.utils import from_networkx
7
+ from vae_model import create_vae
8
+ from utils import read_poems_from_directory
9
+ from sklearn.preprocessing import StandardScaler
10
+ import numpy as np
11
+ from individual_analyzes import analyze_sentiment # Assuming the sentiment analysis function is defined here
12
+
13
+ def build_poem_graph(poems, sentiment_labels):
14
+ poem_graph = nx.Graph()
15
+ poem_graph.add_nodes_from(range(len(poems)))
16
+
17
+ # Add edges based on similarity between poems (example: based on shared words)
18
+ for i in range(len(poems)):
19
+ for j in range(i+1, len(poems)):
20
+ if sentiment_labels[i] == sentiment_labels[j]:
21
+ poem_graph.add_edge(i, j)
22
+
23
+ return poem_graph
24
+
25
+ def visualize_poem_graph(poem_graph, sentiment_labels):
26
+ pos = nx.spring_layout(poem_graph)
27
+ colors = ['skyblue' if label == 'positive' else 'lightcoral' for label in sentiment_labels]
28
+ nx.draw_networkx_nodes(poem_graph, pos, node_size=200, node_color=colors)
29
+ nx.draw_networkx_edges(poem_graph, pos, edge_color='gray')
30
+ nx.draw_networkx_labels(poem_graph, pos, font_size=10)
31
+ plt.axis('off')
32
+ st.pyplot(plt)
33
+
34
+ def graph_guided_learning_page():
35
+ st.header("Graph Guided Learning")
36
+
37
+ # Load and process poems
38
+ poems_directory = "./poems"
39
+ if os.path.isdir(poems_directory):
40
+ poems = read_poems_from_directory(poems_directory)
41
+
42
+ if poems:
43
+ # Perform sentiment analysis on the poems
44
+ sentiment_labels = analyze_sentiment(poems)
45
+
46
+ # Example feature extraction from poems
47
+ def extract_features(poems):
48
+ # Placeholder example: each poem is represented by the length of its text
49
+ return np.array([[len(poem)] for poem in poems])
50
+
51
+ features = extract_features(poems)
52
+ scaler = StandardScaler()
53
+ scaled_features = scaler.fit_transform(features)
54
+
55
+ # Create VAE model and encode poems
56
+ input_dim = scaled_features.shape[1]
57
+ latent_dim = 16
58
+ vae, encoder = create_vae(input_dim, latent_dim)
59
+ vae.fit(scaled_features, scaled_features, epochs=50, batch_size=256, validation_split=0.2)
60
+ latent_features = encoder.predict(scaled_features)
61
+
62
+ # Build a graph based on sentiment similarity
63
+ poem_graph = build_poem_graph(poems, sentiment_labels)
64
+
65
+ # Visualize the poem graph with sentiment labels
66
+ visualize_poem_graph(poem_graph, sentiment_labels)
67
+
68
+ # Convert poem graph to PyTorch Geometric data format
69
+ data = from_networkx(poem_graph)
70
+ data.x = torch.tensor(latent_features, dtype=torch.float32)
71
+
72
+ st.write("Latent Features:")
73
+ st.write(latent_features)
74
+ else:
75
+ st.warning("No poems found in the specified directory.")
76
+ else:
77
+ st.error("The specified path is not a valid directory.")
individual_analyzes.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ from utils import read_poems_from_directory
4
+ import matplotlib.pyplot as plt
5
+
6
+ sentiment_classifier = pipeline("sentiment-analysis")
7
+ emotion_classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
8
+
9
+ def analyze_poem(poem):
10
+ sentiment = sentiment_classifier(poem)[0]
11
+ emotion_scores = emotion_classifier(poem)[0]
12
+ emotion = max(emotion_scores, key=lambda x: x['score'])['label']
13
+ return sentiment, emotion, emotion_scores
14
+
15
+ def plot_emotion_scores(emotion_scores):
16
+ emotions = [score['label'] for score in emotion_scores]
17
+ scores = [score['score'] for score in emotion_scores]
18
+
19
+ fig, ax = plt.subplots()
20
+ ax.bar(emotions, scores)
21
+ ax.set_xlabel("Emotion")
22
+ ax.set_ylabel("Score")
23
+ ax.set_title("Emotion Scores")
24
+ plt.xticks(rotation=45)
25
+ plt.tight_layout()
26
+ return fig
27
+
28
+ def analyze_individual_page():
29
+ st.header("Analyze Poems")
30
+ poems_directory = "poems"
31
+
32
+ poems = read_poems_from_directory(poems_directory)
33
+
34
+ if poems:
35
+ for i, poem in enumerate(poems, start=1):
36
+ st.subheader(f"Poem {i}")
37
+ st.text(poem)
38
+ sentiment, emotion, emotion_scores = analyze_poem(poem)
39
+ st.write(f"Sentiment: {sentiment['label']} (score: {sentiment['score']:.2f})")
40
+ st.write(f"Emotion: {emotion}")
41
+
42
+ # Plot emotion scores
43
+ fig = plot_emotion_scores(emotion_scores)
44
+ st.pyplot(fig)
45
+ else:
46
+ st.warning("No poems found in the 'poems' directory.")
47
+
48
+ def analyze_sentiment(poems):
49
+ sentiment_labels = []
50
+ for poem in poems:
51
+ sentiment = sentiment_classifier(poem)[0]
52
+ sentiment_labels.append(sentiment['label'])
53
+ return sentiment_labels
poem_gen.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from groq import Groq
3
+
4
+ client = Groq(api_key='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')
5
+
6
+ def generate_poems(num_poems, length_per_poem, output_dir):
7
+ # Create output directory if it doesn't exist
8
+ if not os.path.exists(output_dir):
9
+ os.makedirs(output_dir)
10
+
11
+ for i in range(num_poems):
12
+ chat_completion = client.chat.completions.create(
13
+ messages=[
14
+ {
15
+ "role": "system",
16
+ "content": "You are a talented poet."
17
+ },
18
+ {
19
+ "role": "user",
20
+ "content": f"Write a complex poem with a length of {length_per_poem} tokens."
21
+ }
22
+ ],
23
+ model="llama3-8b-8192",
24
+ temperature=0.7,
25
+ max_tokens=length_per_poem,
26
+ top_p=1,
27
+ stop=None,
28
+ stream=False,
29
+ )
30
+
31
+ poem = chat_completion.choices[0].message.content.strip()
32
+
33
+ # Save the poem to a text file
34
+ with open(os.path.join(output_dir, f'poem_{i + 1}.txt'), 'w') as f:
35
+ f.write(poem)
36
+
37
+ # Parameters
38
+ num_poems = 20 # Number of poems to generate
39
+ length_per_poem = 150 # Length of each poem in tokens (adjust accordingly)
40
+ output_dir = 'poems' # Directory to save poems
41
+
42
+ # Generate and save poems
43
+ generate_poems(num_poems, length_per_poem, output_dir)
poems/poem_1.txt ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ I'd be delighted to share some poems with you! Here's one that explores the theme of time and its fleeting nature:
2
+
3
+ "Echoes in the Hourglass"
4
+
5
+ Time, a thief in the night, steals away
6
+ Moments we thought were ours to stay
7
+ The hours tick by, a relentless pace
8
+ Leaving memories in an endless space
9
+
10
+ Like grains of sand, our lives slip through
11
+ The hourglass, a symbol of what we once knew
12
+ The past, a fog that we can't regain
13
+ The present, a fleeting dream that we can't sustain
14
+
15
+ The future, a mystery we can't define
16
+ A path we've yet to tread, a journey we can't design
17
+ But in the present, we find our way
18
+ Through the echoes of what's been and what's to stay
19
+
20
+ In this dance of time, we're lost and found
21
+ A paradox of moments, spinning round and round
22
+ The past, the present, and the future blend
23
+ In an endless cycle, a journey without an end
24
+
25
+ Yet, in the stillness, we find our peace
26
+ A moment's silence, a world's release
27
+ The echoes of time, a melody we play
28
+ A symphony of moments, fading away.
29
+
30
+ Tokens:
poems/poem_10.txt ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ I'd be delighted to craft some poems for you! Here's my first attempt:
2
+
3
+ In twilight's hush, where shadows play
4
+ Amidst the whispers of a fading day
5
+ I find myself lost in thought's maze
6
+ Where darkness and light in paradox sway
7
+
8
+ The wind it howls, a mournful sigh
9
+ As petals fall, and stars go by
10
+ The world outside recedes from view
11
+ Leaving only dreams, and whispers anew
12
+
13
+ In this eerie silence, I confess
14
+ A love for chaos, and life's unrest
15
+ For in the turmoil, I find my peace
16
+ A solace that the world's wild heartbeat release
17
+
18
+ The moon, a glowing crescent shape
19
+ Lends light to this nocturnal escape
20
+ Where darkness reigns, and shadows play
21
+ I find my voice, and words to say
22
+
23
+ In this realm of twilight, I am free
24
+ To dance with demons, wild and carefree
25
+ To chase the ghosts that haunt my mind
26
+ And leave the turmoil, left behind
27
+
28
+ In this twilight world, I am at home
29
+ Where paradox and chaos are my throne
30
+ Where darkness and light entwined
31
+ I find my voice, and my heart's aligned
32
+
33
+ The wind it whispers secrets low
34
+ As I
poems/poem_11.txt ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ I'd be delighted to craft some poems for you! Here's a poem that explores the theme of duality and the human experience, using common words and paradoxes:
2
+
3
+ In twilight's hush, where shadows play
4
+ Amidst the trees, I find my way
5
+ Through paths that wind, and roads that stray
6
+ I search for answers, night and day
7
+
8
+ In darkness lies the light I seek
9
+ A paradox that my heart does speak
10
+ That which is lost, I must first yield
11
+ To find the truth, my doubts must conceal
12
+
13
+ The moon's pale glow, a beacon bright
14
+ Illuminates the path, a guiding light
15
+ But as I walk, the darkness creeps
16
+ And I am lost, in endless sleep
17
+
18
+ Yet in the night, I find my strength
19
+ A resilience that does not relent
20
+ For in the darkness, I am free
21
+ To confront the demons that are me
22
+
23
+ In this twilight world, I find my voice
24
+ A symphony of doubts and choice
25
+ A paradox that echoes through my mind
26
+ A reminder that I am human, and I am blind. (250 tokens)
27
+
28
+ I hope you enjoy this poem! Let me know if you have any specific themes or topics you'd like me to explore
poems/poem_12.txt ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ I'd be delighted to craft some poems for you! Here's my first attempt:
2
+
3
+ In twilight's hush, where shadows play
4
+ A fleeting thought whispers, "Go away"
5
+ Yet, I remain, entranced by the night
6
+ As stars align, and darkness takes flight
7
+ The moon, a glowing crescent smile
8
+ Fades to a whisper, a distant mile
9
+ The world, a canvas, painted gray
10
+ A paradox of life, in endless sway
11
+ The wind, a gentle zephyr's sigh
12
+ Echoes of memories, that touch the sky
13
+ In this quiet hour, I find my peace
14
+ A moment's calm, in life's wild release
15
+ Where darkness reigns, and light takes flight
16
+ I'll hold on to this fleeting night
17
+ And let the shadows dance, in silent delight
18
+
19
+ (250 tokens)
20
+
21
+ Please let me know if you'd like me to make any changes or if you'd like me to come up with a new poem!
poems/poem_13.txt ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ What an intriguing task! Here's a poem that explores common words and paradoxes, with a length of 250 tokens:
2
+
3
+ Silence screams in empty spaces
4
+ Where shadows dance, and darkness prances
5
+ The weight of words, a heavy chain
6
+ Binding thought, in linguistic pain
7
+
8
+ The paradox of truth, a fleeting dream
9
+ Ephemeral whispers, in a world unseen
10
+ Reality's mask, a fragile disguise
11
+ Concealing secrets, behind watchful eyes
12
+
13
+ In the mirror's gaze, a stranger stares
14
+ Reflections blur, as emotions share
15
+ The duality of love and hate
16
+ A tangled web, where hearts create
17
+
18
+ The rhythm of life, a symphony plays
19
+ A cacophony, of conflicting ways
20
+ The melody of hope, a guiding light
21
+ A beacon in darkness, on a stormy night
22
+
23
+ The paradox of time, a mysterious force
24
+ A river's flow, where moments are lost
25
+ The weight of memories, a heavy heart
26
+ A treasure trove, of love and departed parts
27
+
28
+ In the silence, a voice is heard
29
+ A whisper of wisdom, in a world so blurred
30
+ The paradox of words, a double-edged sword
31
+ A tool of creation, or a source of discord
32
+
33
+ The dance
poems/poem_14.txt ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ I'd be delighted to craft some poems for you! Here's a first attempt:
2
+
3
+ In twilight's hush, where shadows play
4
+ Amidst the whispers of a bygone day
5
+ I find myself lost in thought's gray maze
6
+ Where paradoxes reign, and truth's a daze
7
+
8
+ The wind whispers secrets, yet I'm unsure
9
+ If silence is the language of the pure
10
+ The stars up high, a twinkling sea
11
+ Reflecting all the mysteries of me
12
+
13
+ The world outside recedes, a distant hum
14
+ As I retreat to the comfort of my thumb
15
+ I touch the earth, yet feel the height
16
+ A fragile bond between darkness and light
17
+
18
+ In this precarious balance, I reside
19
+ A leaf on the wind, or a feather glide
20
+ The journey's end, or just a start anew
21
+ A paradox within, a poem or two
22
+
23
+ The words I write, a reflection true
24
+ Of the chaos within, and the calm anew
25
+ A dance of contradictions, a waltz of might
26
+ A poem's rhythm, in the silence of night
27
+
28
+ The words I speak, a whispered sigh
29
+ A prayer to the universe, passing by
30
+ The meaning's lost, in the words' design
31
+ A paradox within, a
poems/poem_15.txt ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ I'd be delighted to craft some poems for you! Here's my first attempt:
2
+
3
+ Silent whispers echo through the night
4
+ As darkness clings to the morning light
5
+ A paradox of shadows and of sight
6
+ Where the unseen becomes the guiding light
7
+
8
+ In this twilight realm, I find my peace
9
+ Where contradictions weave a mystic release
10
+ The silence speaks a language I can't cease
11
+ A symphony of whispers, wild and free
12
+
13
+ The wind whispers secrets in my ear
14
+ Of distant lands and memories so dear
15
+ Yet in the stillness, I find my fear
16
+ A tremble within, a doubt, a tear
17
+
18
+ But in the darkness, I find my strength
19
+ A resilience that can withstand the length
20
+ Of time and trials, of joy and woe
21
+ A wisdom born of the unknown, and the slow
22
+
23
+ I'll hold on to the silence, like a thread
24
+ That weaves a tapestry of words unsaid
25
+ And in the shadows, I'll find my way
26
+ Through the labyrinth of a poet's day
27
+
28
+ (250 tokens)
29
+
30
+ Please let me know if you'd like me to make any changes or if you have any specific requests!
poems/poem_16.txt ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ I'd be delighted to craft some poems for you! Here's a poem that explores the paradox of light and darkness:
2
+
3
+ "In Twilight's Hush"
4
+
5
+ In twilight's hush, where shadows play
6
+ The darkness whispers secrets of the day
7
+ As sunbeams fade, and stars begin to sway
8
+ The world is bathed in a gentle gray
9
+
10
+ The light that lingers, a reminder stays
11
+ Of all that's been, and all that's yet to sway
12
+ In this liminal space, I find my way
13
+ Through the veil of night, to a brighter day
14
+
15
+ The stars up high, a twinkling sea
16
+ Reflect the hopes, and dreams that are me
17
+ In their celestial dance, I find release
18
+ From the weight of darkness, and the world's disease
19
+
20
+ In twilight's hush, I find my peace
21
+ A stillness that soothes my troubled release
22
+ A time to reflect, to let my heart be free
23
+ To dance with shadows, wild and carelessly
24
+
25
+ This poem explores the paradox of light and darkness, where the two states coexist in a liminal space. It suggests that even in the darkness, there is still a glimmer of hope and light, and that the two states are interconnected.
26
+
27
+ I
poems/poem_17.txt ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ I'd be delighted to craft some poems for you! Here's a collection of four poems, each with 250 tokens, exploring common words and paradoxes:
2
+
3
+ **Poem 1: Echoes in the Silence**
4
+
5
+ In the depths of solitude, I find my voice
6
+ A whispered echo of a distant choice
7
+ The silence screams, a cacophony of thought
8
+ As I confront the shadows that I've brought
9
+
10
+ The darkness whispers secrets in my ear
11
+ Of forgotten dreams and distant fear
12
+ The echoes of my past, a haunting refrain
13
+ A reminder of the love that remains in vain
14
+
15
+ In this abyss of sound, I search for peace
16
+ A respite from the tumult that ceaselessly increases
17
+ The din of thoughts, a symphony of pain
18
+ A reminder that the noise will forever remain
19
+
20
+ The silence screams, a call to the unknown
21
+ A challenge to confront the ghosts that I've sown
22
+ And in the echoes, I find my way
23
+ Through the labyrinth of a endless day
24
+
25
+ **Poem 2: The Weight of Wings**
26
+
27
+ I spread my wings, and the world is mine
28
+ A fleeting dream, a moment's shine
29
+ But the weight of wings is crushing me
30
+ A burden of responsibility
31
+
32
+ The winds of change
poems/poem_18.txt ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Thank you for the kind introduction! Here's a poem that I've crafted, using common words and paradoxes, with a length of 250 tokens:
2
+
3
+ "Moonlit whispers echo through the night
4
+ A silent scream that's lost in flight
5
+ The darkness speaks in hushed tones low
6
+ A language only shadows know
7
+
8
+ The winds they whisper secrets sweet
9
+ Of distant memories to greet
10
+ The stars up high, a twinkling sea
11
+ Reflecting tears that only I can see
12
+
13
+ The paradox of time, a winding road
14
+ Where past and present are forever to and fro
15
+ The future's path, a mystery to unfold
16
+ A journey through the labyrinth of the mind
17
+
18
+ The echoes of what's yet to be
19
+ A haunting melody that's meant for me
20
+ The whispers of the unknown, a gentle breeze
21
+ That carries the scent of forgotten dreams
22
+
23
+ The moon, a glowing crescent smile
24
+ A beacon in the dark, a guiding light for a while
25
+ The darkness, a canvas, black as coal
26
+ A backdrop for the stories that my heart does unfold"
27
+
28
+ I hope you enjoy it!
poems/poem_19.txt ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ What an intriguing task! Here's a poem that explores the paradox of darkness in the light of understanding:
2
+
3
+ In twilight's hush, where shadows dance and play
4
+ Amidst the whispers of a fading day
5
+ The darkness gathers, like a thief in the night
6
+ Stealing the light, yet shedding its own blinding light
7
+
8
+ Like a mirror reflecting the soul's darkest fears
9
+ The shadows hide, yet reveal the tears
10
+ Of a heart that's heavy with the weight of years
11
+ And the silence screams, a cacophony of cheers
12
+
13
+ Yet, in this void, a glimmer of hope does gleam
14
+ A spark of understanding, a whispered dream
15
+ That even in the darkest of times, there's a beam
16
+ Of light that guides us, a path to redeem
17
+
18
+ But how can we find solace in the dark of night
19
+ When the light that guides us is but a distant flight?
20
+ Perhaps, in this paradox, we find our greatest might
21
+ To confront the shadows, and bring forth the light
22
+
23
+ For in the darkness, we find the strength to face
24
+ The fears that haunt us, the demons in our place
25
+ And though the shadows hide, they also reveal our face
26
+ And in that mirror, we find our
poems/poem_2.txt ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ What an intriguing task! I shall weave a tapestry of words, a symphony of sense and nonsense, a dance of paradox and poetry. Here's my first creation:
2
+
3
+ "Ink-stained fingers weave a tapestry of time,
4
+ As memories unfold like a worn-out rhyme.
5
+ The past is present, yet the future's mine,
6
+ A paradox of moments, intertwined like a vine.
7
+
8
+ The wind whispers secrets, a gentle breeze that brings
9
+ The scent of forgotten dreams and distant rings.
10
+ Echoes of what's been, a haunting refrain,
11
+ A chorus of what's to be, a melody in vain.
12
+
13
+ In the mirror's gaze, a reflection stares,
14
+ A stranger's face, with stories to share.
15
+ The eyes that see, the heart that beats,
16
+ A symphony of contradictions, a poetic repeat.
17
+
18
+ The weight of words, a burden to bear,
19
+ A responsibility to convey the care.
20
+ The art of poetry, a delicate dance,
21
+ A balancing act of sense and senseless trance.
22
+
23
+ Ink-stained fingers weave a tapestry of time,
24
+ As memories unfold like a worn-out rhyme.
25
+ The past is present, yet the future's mine,
26
+ A paradox of moments, intertwined like a vine."
27
+
28
+ (250 tokens)
poems/poem_20.txt ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ What an intriguing request! I shall craft a poem that weaves together common words and paradoxes, and shall ensure it reaches the desired length of 250 tokens. Here's my creation:
2
+
3
+ "In twilight's hush, where shadows play,
4
+ A paradox unfolds, a paradox stays.
5
+ The darkness whispers secrets sweet,
6
+ Yet light prevails, the truth to meet.
7
+
8
+ The more we know, the less we see,
9
+ A mystery that puzzles humanity.
10
+ The more we learn, the more we stray,
11
+ From answers found, to questions gray.
12
+
13
+ The calm before the storm's dark guise,
14
+ Belies the turmoil that it compromises.
15
+ The peaceful surface, a stillness deep,
16
+ Conceals the turmoil that the heart does keep.
17
+
18
+ The more we progress, the more we slide,
19
+ Into the abyss, where time does reside.
20
+ The more we grow, the more we fade,
21
+ Into the shadows, where our stories are made.
22
+
23
+ In twilight's hush, where shadows play,
24
+ A paradox unfolds, a paradox stays.
25
+ The darkness whispers secrets sweet,
26
+ Yet light prevails, the truth to meet."
27
+
28
+ This poem explores the paradoxes of knowledge, progress, and the human experience, weaving together common words and phrases to create a thought-provoking and lyrical piece.
poems/poem_3.txt ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ I'd be delighted to craft some poems for you! Here's my first attempt:
2
+
3
+ In twilight's hush, where shadows play
4
+ Amidst the whispers of a fading day
5
+ The moon's pale gaze upon me lies
6
+ A silver thread that weaves the skies
7
+
8
+ The stars, like diamonds in the night
9
+ Twinkle and wink, a celestial delight
10
+ Their gentle hum, a lullaby sweet
11
+ Echoes through the chambers of my heart's retreat
12
+
13
+ The world, a canvas, painted gray
14
+ A tapestry of contrasts, night and day
15
+ Where darkness flees, and light takes hold
16
+ A dance of opposites, forever to unfold
17
+
18
+ In this precarious balance, I reside
19
+ A fleeting moment, 'twixt darkness and pride
20
+ Where paradox and truth entwine like vines
21
+ And the beauty of the world, in fragments, divine
22
+
23
+ In this fragile dance, I lose myself
24
+ A drop of dew upon the morning's wealth
25
+ The world, a kaleidoscope in spin
26
+ A whirlwind of colors, forever to begin
27
+
28
+ (250 tokens)
29
+
30
+ Please let me know if you'd like me to make any changes or if you have any specific requests!
poems/poem_4.txt ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ I'd be delighted to craft some poems for you! Here's my first attempt:
2
+
3
+ Silent whispers in the dark of night
4
+ Echoes of a truth that's hard to fight
5
+ A paradox of shadows and of light
6
+ Where darkness shines and darkness takes flight
7
+
8
+ Invisible threads that bind and connect
9
+ The dots between the past and what's inspect
10
+ A tapestry of memories and of regret
11
+ A patchwork quilt of love and bitter debt
12
+
13
+ The weight of words that spoken or unspoken
14
+ Leave scars that linger, hearts that broken
15
+ The power of silence, a gentle breeze
16
+ That soothes the soul, and brings us to our knees
17
+
18
+ A dance of duality, a delicate balance
19
+ Between the yin and yang, the darkness and the dance
20
+ A harmony of contrasts, a symphony of strife
21
+ A love-hate relationship with the fragile life
22
+
23
+ The search for meaning in a chaotic sea
24
+ A quest for answers, a never-ending spree
25
+ A journey of self-discovery, a path unwinding
26
+ A story of growth, of trial and of finding
27
+
28
+ The beauty of the mundane, the ordinary
29
+ A celebration of life, a daily ritual
30
+ A poem of paradoxes, a dance of duality
31
+ A celebration of
poems/poem_5.txt ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ I'd be delighted to craft some poems for you! Here's a piece that explores the paradox of freedom and confinement:
2
+
3
+ In cages of iron, I find my wings
4
+ A prison of bars, where I sing
5
+ The sweetest melodies, that echo and cling
6
+ To the walls that confine, my heart that sings
7
+
8
+ With every note, I feel my soul take flight
9
+ But chains of doubt, bind me to the night
10
+ A paradox of freedom, in this darkest place
11
+ Where I'm trapped, yet untethered from the space
12
+
13
+ In this labyrinth of thoughts, I wander and roam
14
+ A maze of mirrors, reflecting my heart's home
15
+ Where every door, leads to another's face
16
+ A never-ending journey, without a set pace
17
+
18
+ Through the gates of dawn, I'll find my way
19
+ To the freedom within, that's been here all day
20
+ The chains of doubt, will break and fade away
21
+ And I'll spread my wings, to seize the brand new day
22
+
23
+ Token count: 250
poems/poem_6.txt ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ I'd be delighted to craft some poems for you. Here's my first attempt:
2
+
3
+ In twilight's hush, where shadows play
4
+ Amidst the whispers of a fading day
5
+ The moon, a ghostly galleon, rides
6
+ The midnight sky, where stars hide
7
+
8
+ The wind, a zephyr, whispers low
9
+ Of secrets kept, and tales to know
10
+ The trees, like sentinels of old
11
+ Stand guard, their wisdom yet untold
12
+
13
+ In this realm of dreams and reality
14
+ Where paradoxes reign supreme, you'll see
15
+ The beauty of the ordinary
16
+ And the magic that's yet to be
17
+
18
+ The world, a tapestry so fine
19
+ Weaves together threads of time
20
+ Past, present, and the yet to come
21
+ In an intricate, cosmic drum
22
+
23
+ In this dance of light and shade
24
+ We're swept away, our hearts not made
25
+ To comprehend the mysteries deep
26
+ That lie beneath our troubled sleep
27
+
28
+ The universe, a riddle, we pursue
29
+ A puzzle, with no solution anew
30
+ Yet, in its depths, we find our peace
31
+ And the beauty that our souls release
32
+
33
+ In this world of paradox and strife
34
+ We search for answers, and a glimpse of life
35
+ A
poems/poem_7.txt ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ I'd be delighted to craft some poems for you! Here's a poem that uses common words and paradoxes:
2
+
3
+ "In twilight's hush, where shadows play
4
+ Amidst the silence, words have sway
5
+ A whispered truth, a hidden lie
6
+ A paradox that catches the eye
7
+
8
+ The more I search, the more I find
9
+ The less I know, the more I'm blind
10
+ In endless loops, I lose my way
11
+ Yet in the darkness, I find my day
12
+
13
+ The farther I stray, the closer I roam
14
+ The more I'm lost, the more I'm home
15
+ In labyrinthine corridors of thought
16
+ I chase the wind, and find what's caught
17
+
18
+ In mirrors reflecting, I behold
19
+ A stranger's face, yet my own gold
20
+ A reflection of what's yet to be
21
+ A glimpse of truth, for you and me
22
+
23
+ In twilight's hush, where shadows play
24
+ I'll weave a tapestry of paradox and gray"
25
+
26
+ This poem has 250 tokens and explores the themes of paradox, truth, and self-discovery. It uses words and phrases that create a sense of intrigue and mystery, drawing the reader in to unravel the complexities of the poem.
poems/poem_8.txt ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ What an intriguing task! Here's a poem that explores the paradoxes of existence, using common words and imagery:
2
+
3
+ In twilight's hush, where shadows play
4
+ Amidst the whispers of a fading day
5
+ I find myself lost in thoughts astray
6
+ A world of contradictions, come what may
7
+
8
+ The wind whispers secrets in my ear
9
+ Of lives lived, loved, and lost, yet still unclear
10
+ The stars up high, a twinkling sea
11
+ Reflecting the turmoil of humanity
12
+
13
+ In dreams, I soar on eagle's wings
14
+ Free from the chains that bind and cling
15
+ But wake to find the chains remain
16
+ A prisoner of thought, in endless pain
17
+
18
+ The clock ticks on, a relentless beat
19
+ A reminder of the time I'll never meet
20
+ The hours slip away, like grains of sand
21
+ A never-ending quest, in this desolate land
22
+
23
+ Yet, in the stillness, I find my voice
24
+ A whispered prayer, a heartfelt choice
25
+ To rise above the noise and strife
26
+ And find solace in the beauty of life
27
+
28
+ In this paradox of darkness and of light
29
+ I search for answers, through the endless night
30
+ For though the world is full of strife and pain
31
+ There's beauty too, in the
poems/poem_9.txt ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ I'd be delighted to craft some poems for you! Here's my first attempt:
2
+
3
+ Infinite Silence, Echoing Whispers
4
+
5
+ In the stillness of the night, I find my voice
6
+ A whispered secret, echoing without a choice
7
+ The stars above, a twinkling sea
8
+ Reflecting the depths of my soul's mystery
9
+
10
+ The wind whispers secrets, a gentle breeze
11
+ That rustles leaves, a symphony of ease
12
+ The world outside, a canvas wide
13
+ A tapestry of colors, woven with pride
14
+
15
+ Yet, in this silence, I am free to roam
16
+ A traveler without a map, or a set home
17
+ The world's din and chaos, left behind
18
+ As I wander through the shadows of my mind
19
+
20
+ Infinite silence, echoing whispers deep
21
+ A mysterious language, only I can keep
22
+ A code of dreams, a cipher of the heart
23
+ A poem of paradox, a work of art
24
+
25
+ The silence speaks, a voice so clear
26
+ A gentle warning, a whispered fear
27
+ That in the stillness, I may find my way
28
+ Through the labyrinth of life, to a brighter day
29
+
30
+ Infinite silence, echoing whispers wide
31
+ A poetic paradox, that I must abide
32
+ A dance of contradictions, a w
requirements.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ groq==0.9.0
2
+ matplotlib==3.9.1.post1
3
+ networkx==3.3
4
+ nltk==3.8.1
5
+ numpy==2.0.1
6
+ pandas==2.2.2
7
+ python-dotenv==1.0.1
8
+ scikit_learn==1.5.1
9
+ streamlit==1.37.1
10
+ tensorflow==2.17.0
11
+ torch==2.4.0
12
+ torch_geometric==2.5.3
13
+ transformers==4.44.0
utils.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ def read_poems_from_directory(directory):
4
+ poems = []
5
+ seen_files = set()
6
+ for filename in os.listdir(directory):
7
+ if filename.endswith(".txt") and filename not in seen_files:
8
+ seen_files.add(filename)
9
+ with open(os.path.join(directory, filename), 'r') as file:
10
+ poems.append(file.read())
11
+ return poems
12
+
13
+ emotion_labels_with_colors = {
14
+ "happiness": "yellow",
15
+ "sadness": "blue",
16
+ "anger": "red",
17
+ "fear": "purple",
18
+ "disgust": "green",
19
+ "surprise": "orange",
20
+ "love": "pink",
21
+ "joy": "gold",
22
+ "anxiety": "lightblue",
23
+ "contentment": "lightgreen",
24
+ "frustration": "brown",
25
+ "loneliness": "grey",
26
+ "excitement": "cyan",
27
+ "guilt": "darkred",
28
+ "shame": "darkblue",
29
+ "envy": "darkgreen",
30
+ "jealousy": "olive",
31
+ "pride": "magenta",
32
+ "gratitude": "lavender",
33
+ "empathy": "peachpuff",
34
+ "compassion": "coral",
35
+ "boredom": "beige",
36
+ "relief": "lightyellow",
37
+ "curiosity": "lightcoral",
38
+ "awe": "turquoise",
39
+ "confusion": "plum",
40
+ "nostalgia": "orchid",
41
+ "hope": "khaki",
42
+ "despair": "maroon",
43
+ "embarrassment": "salmon"
44
+ }
vae_model.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ from tensorflow.keras.layers import Input, Dense, Lambda, Layer
3
+ from tensorflow.keras.models import Model
4
+ from tensorflow.keras.losses import binary_crossentropy
5
+ from tensorflow.keras import backend as K
6
+
7
+ def sampling(args):
8
+ z_mean, z_log_var = args
9
+ batch = tf.shape(z_mean)[0]
10
+ dim = tf.shape(z_mean)[1]
11
+ epsilon = K.random_normal(shape=(batch, dim))
12
+ return z_mean + K.exp(0.5 * z_log_var) * epsilon
13
+
14
+ def vae_loss(inputs, x_decoded_mean, z_log_var, z_mean):
15
+ xent_loss = binary_crossentropy(inputs, x_decoded_mean)
16
+ kl_loss = -0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
17
+ return K.mean(xent_loss + kl_loss)
18
+
19
+ class VAELossLayer(Layer):
20
+ def call(self, inputs):
21
+ x = inputs[0]
22
+ x_decoded_mean = inputs[1]
23
+ z_log_var = inputs[2]
24
+ z_mean = inputs[3]
25
+ loss = vae_loss(x, x_decoded_mean, z_log_var, z_mean)
26
+ self.add_loss(loss)
27
+ return x
28
+
29
+ def compute_output_shape(self, input_shape):
30
+ return input_shape[0]
31
+
32
+ def create_vae(input_dim, latent_dim):
33
+ # Encoder
34
+ inputs = Input(shape=(input_dim,))
35
+ h = Dense(512, activation='relu')(inputs)
36
+ z_mean = Dense(latent_dim)(h)
37
+ z_log_var = Dense(latent_dim)(h)
38
+
39
+ # Use reparameterization trick
40
+ z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var])
41
+
42
+ # Decoder
43
+ decoder_h = Dense(512, activation='relu')
44
+ decoder_mean = Dense(input_dim, activation='sigmoid')
45
+ h_decoded = decoder_h(z)
46
+ x_decoded_mean = decoder_mean(h_decoded)
47
+
48
+ # Define VAE model
49
+ outputs = VAELossLayer()([inputs, x_decoded_mean, z_log_var, z_mean])
50
+ vae = Model(inputs, outputs)
51
+ vae.compile(optimizer='rmsprop')
52
+ vae.summary()
53
+
54
+ return vae, Model(inputs, z_mean)