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
+
import os
|
4 |
+
import time
|
5 |
+
from openai import OpenAI
|
6 |
+
|
7 |
+
class DeepDreamInterpreter:
|
8 |
+
def __init__(self, api_key=None):
|
9 |
+
self.api_key = api_key
|
10 |
+
self.client = None
|
11 |
+
if api_key:
|
12 |
+
self.setup_client(api_key)
|
13 |
+
|
14 |
+
def setup_client(self, api_key):
|
15 |
+
"""Set up the OpenAI client with the provided API key"""
|
16 |
+
self.api_key = api_key
|
17 |
+
self.client = OpenAI(
|
18 |
+
base_url="https://openrouter.ai/api/v1",
|
19 |
+
api_key=api_key,
|
20 |
+
)
|
21 |
+
return "API key configured successfully!"
|
22 |
+
|
23 |
+
def analyze_dream(self, dream_description, include_visualization=True, user_name="Anonymous"):
|
24 |
+
"""Analyze the provided dream description using Gemini 2.5 Flash"""
|
25 |
+
if not self.client:
|
26 |
+
return "Please configure your API key first."
|
27 |
+
|
28 |
+
if not dream_description.strip():
|
29 |
+
return "Please enter a dream description to analyze."
|
30 |
+
|
31 |
+
try:
|
32 |
+
# First pass: Dream analysis with thinking capabilities
|
33 |
+
print("Starting dream analysis...")
|
34 |
+
analysis = self.client.chat.completions.create(
|
35 |
+
extra_headers={
|
36 |
+
"HTTP-Referer": "https://deepdreaminterpreter.com",
|
37 |
+
"X-Title": "Deep Dream Interpreter",
|
38 |
+
},
|
39 |
+
model="google/gemini-2.5-flash-preview:thinking",
|
40 |
+
messages=[
|
41 |
+
{
|
42 |
+
"role": "system",
|
43 |
+
"content": "You are an expert dream analyst. Analyze the following dream description, identifying key symbols, emotions, themes, and potential psychological meanings. Structure your analysis in these sections: Summary, Key Symbols, Emotional Landscape, Themes, and Psychological Interpretation."
|
44 |
+
},
|
45 |
+
{
|
46 |
+
"role": "user",
|
47 |
+
"content": f"Dream description from user {user_name}: {dream_description}"
|
48 |
+
}
|
49 |
+
]
|
50 |
+
)
|
51 |
+
|
52 |
+
dream_analysis = analysis.choices[0].message.content
|
53 |
+
|
54 |
+
# If visualization is not needed, return just the analysis
|
55 |
+
if not include_visualization:
|
56 |
+
return dream_analysis
|
57 |
+
|
58 |
+
# Second pass: Visual interpretation
|
59 |
+
print("Generating visualization prompt...")
|
60 |
+
visualization_prompt = self.client.chat.completions.create(
|
61 |
+
extra_headers={
|
62 |
+
"HTTP-Referer": "https://deepdreaminterpreter.com",
|
63 |
+
"X-Title": "Deep Dream Interpreter",
|
64 |
+
},
|
65 |
+
model="google/gemini-2.5-flash-preview:thinking",
|
66 |
+
messages=[
|
67 |
+
{
|
68 |
+
"role": "system",
|
69 |
+
"content": "Based on this dream analysis, create a detailed visual description that could be used as a prompt for an image generation model. Make it evocative and detailed, including colors, atmosphere, composition, and key elements."
|
70 |
+
},
|
71 |
+
{
|
72 |
+
"role": "user",
|
73 |
+
"content": f"Dream analysis: {dream_analysis}"
|
74 |
+
}
|
75 |
+
]
|
76 |
+
)
|
77 |
+
|
78 |
+
# Combine the results
|
79 |
+
full_response = f"""## Dream Analysis
|
80 |
+
|
81 |
+
{dream_analysis}
|
82 |
+
|
83 |
+
## Visualization Prompt
|
84 |
+
|
85 |
+
{visualization_prompt.choices[0].message.content}
|
86 |
+
|
87 |
+
---
|
88 |
+
*Note: This visualization prompt can be used with image generation models like DALL-E, Midjourney, or Stable Diffusion to create a visual representation of your dream.*
|
89 |
+
"""
|
90 |
+
return full_response
|
91 |
+
|
92 |
+
except Exception as e:
|
93 |
+
return f"Error: {str(e)}"
|
94 |
+
|
95 |
+
def save_dream(self, user_name, dream_description, analysis):
|
96 |
+
"""Save the dream and its analysis to a JSON file"""
|
97 |
+
if not user_name.strip():
|
98 |
+
user_name = "Anonymous"
|
99 |
+
|
100 |
+
# Create dreams directory if it doesn't exist
|
101 |
+
if not os.path.exists("dreams"):
|
102 |
+
os.makedirs("dreams")
|
103 |
+
|
104 |
+
# Create a filename based on user and timestamp
|
105 |
+
timestamp = time.strftime("%Y%m%d-%H%M%S")
|
106 |
+
filename = f"dreams/{user_name.replace(' ', '_')}_{timestamp}.json"
|
107 |
+
|
108 |
+
# Create the dream data
|
109 |
+
dream_data = {
|
110 |
+
"user": user_name,
|
111 |
+
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
|
112 |
+
"dream_description": dream_description,
|
113 |
+
"analysis": analysis
|
114 |
+
}
|
115 |
+
|
116 |
+
# Save to file
|
117 |
+
with open(filename, "w") as f:
|
118 |
+
json.dump(dream_data, f, indent=4)
|
119 |
+
|
120 |
+
return f"Dream saved to {filename}"
|
121 |
+
|
122 |
+
|
123 |
+
# Set up the Gradio interface
|
124 |
+
def create_gradio_interface():
|
125 |
+
# Initialize the interpreter
|
126 |
+
interpreter = DeepDreamInterpreter()
|
127 |
+
|
128 |
+
# Define the process function for the API key
|
129 |
+
def process_api_key(api_key):
|
130 |
+
if not api_key.strip():
|
131 |
+
return "Please enter a valid API key."
|
132 |
+
return interpreter.setup_client(api_key)
|
133 |
+
|
134 |
+
# Define the process function for dream analysis
|
135 |
+
def process_dream(api_key, dream_description, include_visualization, user_name):
|
136 |
+
if api_key.strip() != interpreter.api_key or not interpreter.client:
|
137 |
+
setup_message = interpreter.setup_client(api_key)
|
138 |
+
if "successfully" not in setup_message:
|
139 |
+
return setup_message
|
140 |
+
|
141 |
+
analysis = interpreter.analyze_dream(dream_description, include_visualization, user_name)
|
142 |
+
return analysis
|
143 |
+
|
144 |
+
# Define the save function
|
145 |
+
def save_dream_entry(api_key, user_name, dream_description, analysis_output):
|
146 |
+
if not analysis_output or "Error" in analysis_output:
|
147 |
+
return "Nothing to save or analysis contains errors."
|
148 |
+
|
149 |
+
if api_key.strip() != interpreter.api_key or not interpreter.client:
|
150 |
+
setup_message = interpreter.setup_client(api_key)
|
151 |
+
if "successfully" not in setup_message:
|
152 |
+
return setup_message
|
153 |
+
|
154 |
+
return interpreter.save_dream(user_name, dream_description, analysis_output)
|
155 |
+
|
156 |
+
# Create the Gradio interface
|
157 |
+
with gr.Blocks(title="Deep Dream Interpreter") as app:
|
158 |
+
gr.Markdown("# 🌙 Deep Dream Interpreter")
|
159 |
+
gr.Markdown("Analyze your dreams using Gemini 2.5 Flash with thinking capabilities.")
|
160 |
+
|
161 |
+
with gr.Tab("Dream Analysis"):
|
162 |
+
with gr.Row():
|
163 |
+
with gr.Column():
|
164 |
+
api_key = gr.Textbox(
|
165 |
+
label="OpenRouter API Key",
|
166 |
+
placeholder="Enter your OpenRouter API key here",
|
167 |
+
type="password"
|
168 |
+
)
|
169 |
+
api_status = gr.Textbox(label="API Status", interactive=False)
|
170 |
+
api_button = gr.Button("Configure API")
|
171 |
+
|
172 |
+
with gr.Row():
|
173 |
+
with gr.Column():
|
174 |
+
user_name = gr.Textbox(
|
175 |
+
label="Your Name (Optional)",
|
176 |
+
placeholder="Enter your name or leave blank for anonymous"
|
177 |
+
)
|
178 |
+
dream_description = gr.Textbox(
|
179 |
+
label="Dream Description",
|
180 |
+
placeholder="Describe your dream in detail...",
|
181 |
+
lines=10
|
182 |
+
)
|
183 |
+
include_visualization = gr.Checkbox(
|
184 |
+
label="Include visualization prompt",
|
185 |
+
value=True
|
186 |
+
)
|
187 |
+
|
188 |
+
with gr.Row():
|
189 |
+
analyze_button = gr.Button("Analyze Dream", variant="primary")
|
190 |
+
save_button = gr.Button("Save Analysis")
|
191 |
+
|
192 |
+
with gr.Column():
|
193 |
+
analysis_output = gr.Markdown(label="Analysis Results")
|
194 |
+
save_status = gr.Textbox(label="Save Status", interactive=False)
|
195 |
+
|
196 |
+
with gr.Tab("About"):
|
197 |
+
gr.Markdown("""
|
198 |
+
## About Deep Dream Interpreter
|
199 |
+
|
200 |
+
This application uses Google's Gemini 2.5 Flash Preview model with thinking capabilities to analyze and interpret dreams. The model analyzes the narrative structure, emotional content, and symbolic elements of your dreams to provide insights and generate visualization prompts.
|
201 |
+
|
202 |
+
### Features:
|
203 |
+
- **Dream Analysis**: Get a detailed analysis of your dream's symbols, emotions, and themes
|
204 |
+
- **Visualization Prompts**: Receive detailed prompts that can be used with image generation models
|
205 |
+
- **Save Your Dreams**: Save your dream descriptions and analyses for future reference
|
206 |
+
|
207 |
+
### Privacy Note:
|
208 |
+
Your dream descriptions and analyses are processed using the Gemini model via OpenRouter. Your data is not stored on our servers unless you explicitly save your analysis.
|
209 |
+
|
210 |
+
### Getting Started:
|
211 |
+
1. Enter your OpenRouter API key
|
212 |
+
2. Enter your dream description
|
213 |
+
3. Click "Analyze Dream"
|
214 |
+
4. Optionally save your analysis
|
215 |
+
|
216 |
+
### Requirements:
|
217 |
+
- An OpenRouter API key with access to Google's Gemini 2.5 Flash Preview model
|
218 |
+
""")
|
219 |
+
|
220 |
+
# Set up the event handlers
|
221 |
+
api_button.click(process_api_key, inputs=api_key, outputs=api_status)
|
222 |
+
analyze_button.click(
|
223 |
+
process_dream,
|
224 |
+
inputs=[api_key, dream_description, include_visualization, user_name],
|
225 |
+
outputs=analysis_output
|
226 |
+
)
|
227 |
+
save_button.click(
|
228 |
+
save_dream_entry,
|
229 |
+
inputs=[api_key, user_name, dream_description, analysis_output],
|
230 |
+
outputs=save_status
|
231 |
+
)
|
232 |
+
|
233 |
+
return app
|
234 |
+
|
235 |
+
# Create and launch the app
|
236 |
+
if __name__ == "__main__":
|
237 |
+
app = create_gradio_interface()
|
238 |
+
app.launch()
|