Update app.py
Browse files
app.py
CHANGED
@@ -13,25 +13,57 @@ class DeepDreamInterpreter:
|
|
13 |
|
14 |
def setup_client(self, api_key):
|
15 |
"""Set up the OpenAI client with the provided API key"""
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
extra_headers={
|
36 |
"HTTP-Referer": "https://deepdreaminterpreter.com",
|
37 |
"X-Title": "Deep Dream Interpreter",
|
@@ -40,7 +72,7 @@ class DeepDreamInterpreter:
|
|
40 |
messages=[
|
41 |
{
|
42 |
"role": "system",
|
43 |
-
"content":
|
44 |
},
|
45 |
{
|
46 |
"role": "user",
|
@@ -49,7 +81,11 @@ class DeepDreamInterpreter:
|
|
49 |
]
|
50 |
)
|
51 |
|
52 |
-
|
|
|
|
|
|
|
|
|
53 |
|
54 |
# If visualization is not needed, return just the analysis
|
55 |
if not include_visualization:
|
@@ -57,7 +93,19 @@ class DeepDreamInterpreter:
|
|
57 |
|
58 |
# Second pass: Visual interpretation
|
59 |
print("Generating visualization prompt...")
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
extra_headers={
|
62 |
"HTTP-Referer": "https://deepdreaminterpreter.com",
|
63 |
"X-Title": "Deep Dream Interpreter",
|
@@ -66,7 +114,7 @@ class DeepDreamInterpreter:
|
|
66 |
messages=[
|
67 |
{
|
68 |
"role": "system",
|
69 |
-
"content":
|
70 |
},
|
71 |
{
|
72 |
"role": "user",
|
@@ -75,6 +123,18 @@ class DeepDreamInterpreter:
|
|
75 |
]
|
76 |
)
|
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
# Combine the results
|
79 |
full_response = f"""## Dream Analysis
|
80 |
|
@@ -82,7 +142,7 @@ class DeepDreamInterpreter:
|
|
82 |
|
83 |
## Visualization Prompt
|
84 |
|
85 |
-
{
|
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.*
|
@@ -90,34 +150,38 @@ class DeepDreamInterpreter:
|
|
90 |
return full_response
|
91 |
|
92 |
except Exception as e:
|
93 |
-
|
|
|
|
|
94 |
|
95 |
def save_dream(self, user_name, dream_description, analysis):
|
96 |
"""Save the dream and its analysis to a JSON file"""
|
97 |
-
|
98 |
-
user_name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
103 |
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
"
|
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
|
@@ -127,17 +191,24 @@ def create_gradio_interface():
|
|
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 |
-
|
|
|
133 |
|
134 |
# Define the process function for dream analysis
|
135 |
def process_dream(api_key, dream_description, include_visualization, user_name):
|
136 |
-
|
|
|
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 |
|
@@ -146,7 +217,7 @@ def create_gradio_interface():
|
|
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
|
150 |
setup_message = interpreter.setup_client(api_key)
|
151 |
if "successfully" not in setup_message:
|
152 |
return setup_message
|
|
|
13 |
|
14 |
def setup_client(self, api_key):
|
15 |
"""Set up the OpenAI client with the provided API key"""
|
16 |
+
if not api_key or not api_key.strip():
|
17 |
+
return "Please provide a valid API key."
|
18 |
+
|
19 |
+
try:
|
20 |
+
self.api_key = api_key
|
21 |
+
self.client = OpenAI(
|
22 |
+
base_url="https://openrouter.ai/api/v1",
|
23 |
+
api_key=api_key,
|
24 |
+
)
|
25 |
+
# Test the API key with a simple request
|
26 |
+
test_response = self.client.chat.completions.create(
|
27 |
+
extra_headers={
|
28 |
+
"HTTP-Referer": "https://deepdreaminterpreter.com",
|
29 |
+
"X-Title": "Deep Dream Interpreter",
|
30 |
+
},
|
31 |
+
model="google/gemini-2.5-flash-preview:thinking",
|
32 |
+
messages=[
|
33 |
+
{"role": "user", "content": "Hello, this is a test."}
|
34 |
+
],
|
35 |
+
max_tokens=5 # Keep it minimal for the test
|
36 |
+
)
|
37 |
+
return "API key configured successfully!"
|
38 |
+
except Exception as e:
|
39 |
+
self.client = None
|
40 |
+
self.api_key = None
|
41 |
+
return f"API configuration failed: {str(e)}"
|
42 |
|
43 |
def analyze_dream(self, dream_description, include_visualization=True, user_name="Anonymous"):
|
44 |
"""Analyze the provided dream description using Gemini 2.5 Flash"""
|
45 |
if not self.client:
|
46 |
return "Please configure your API key first."
|
47 |
|
48 |
+
if not dream_description or not dream_description.strip():
|
49 |
return "Please enter a dream description to analyze."
|
50 |
|
51 |
try:
|
52 |
# First pass: Dream analysis with thinking capabilities
|
53 |
print("Starting dream analysis...")
|
54 |
+
|
55 |
+
# System prompt for dream analysis
|
56 |
+
system_prompt = """You are an expert dream analyst. Analyze the following dream description, identifying key symbols, emotions, themes, and potential psychological meanings.
|
57 |
+
Structure your analysis in these sections:
|
58 |
+
- Summary
|
59 |
+
- Key Symbols
|
60 |
+
- Emotional Landscape
|
61 |
+
- Themes
|
62 |
+
- Psychological Interpretation
|
63 |
+
|
64 |
+
Be thoughtful, nuanced, and avoid overgeneralizations."""
|
65 |
+
|
66 |
+
analysis_response = self.client.chat.completions.create(
|
67 |
extra_headers={
|
68 |
"HTTP-Referer": "https://deepdreaminterpreter.com",
|
69 |
"X-Title": "Deep Dream Interpreter",
|
|
|
72 |
messages=[
|
73 |
{
|
74 |
"role": "system",
|
75 |
+
"content": system_prompt
|
76 |
},
|
77 |
{
|
78 |
"role": "user",
|
|
|
81 |
]
|
82 |
)
|
83 |
|
84 |
+
# Check if we got a valid response
|
85 |
+
if not analysis_response or not hasattr(analysis_response, 'choices') or not analysis_response.choices:
|
86 |
+
return "Error: Received an invalid response from the API during analysis."
|
87 |
+
|
88 |
+
dream_analysis = analysis_response.choices[0].message.content
|
89 |
|
90 |
# If visualization is not needed, return just the analysis
|
91 |
if not include_visualization:
|
|
|
93 |
|
94 |
# Second pass: Visual interpretation
|
95 |
print("Generating visualization prompt...")
|
96 |
+
|
97 |
+
# System prompt for visualization
|
98 |
+
visualization_system_prompt = """Based on this dream analysis, create a detailed visual description that could be used as a prompt for an image generation model.
|
99 |
+
Make it evocative and detailed, including:
|
100 |
+
- Colors and lighting
|
101 |
+
- Atmosphere and mood
|
102 |
+
- Composition and perspective
|
103 |
+
- Key elements and their arrangement
|
104 |
+
- Symbolic representations
|
105 |
+
|
106 |
+
Create something that captures the essence and emotional quality of the dream."""
|
107 |
+
|
108 |
+
visualization_response = self.client.chat.completions.create(
|
109 |
extra_headers={
|
110 |
"HTTP-Referer": "https://deepdreaminterpreter.com",
|
111 |
"X-Title": "Deep Dream Interpreter",
|
|
|
114 |
messages=[
|
115 |
{
|
116 |
"role": "system",
|
117 |
+
"content": visualization_system_prompt
|
118 |
},
|
119 |
{
|
120 |
"role": "user",
|
|
|
123 |
]
|
124 |
)
|
125 |
|
126 |
+
# Check if we got a valid response
|
127 |
+
if not visualization_response or not hasattr(visualization_response, 'choices') or not visualization_response.choices:
|
128 |
+
# If visualization fails, still return the analysis
|
129 |
+
return f"""## Dream Analysis
|
130 |
+
|
131 |
+
{dream_analysis}
|
132 |
+
|
133 |
+
## Visualization Prompt
|
134 |
+
|
135 |
+
Error: Unable to generate visualization prompt. Please try again later.
|
136 |
+
"""
|
137 |
+
|
138 |
# Combine the results
|
139 |
full_response = f"""## Dream Analysis
|
140 |
|
|
|
142 |
|
143 |
## Visualization Prompt
|
144 |
|
145 |
+
{visualization_response.choices[0].message.content}
|
146 |
|
147 |
---
|
148 |
*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.*
|
|
|
150 |
return full_response
|
151 |
|
152 |
except Exception as e:
|
153 |
+
error_message = str(e)
|
154 |
+
print(f"Error during analysis: {error_message}")
|
155 |
+
return f"Error during analysis: {error_message}\n\nPlease check your API key and try again."
|
156 |
|
157 |
def save_dream(self, user_name, dream_description, analysis):
|
158 |
"""Save the dream and its analysis to a JSON file"""
|
159 |
+
try:
|
160 |
+
if not user_name or not user_name.strip():
|
161 |
+
user_name = "Anonymous"
|
162 |
+
|
163 |
+
# Create dreams directory if it doesn't exist
|
164 |
+
os.makedirs("dreams", exist_ok=True)
|
165 |
+
|
166 |
+
# Create a filename based on user and timestamp
|
167 |
+
timestamp = time.strftime("%Y%m%d-%H%M%S")
|
168 |
+
filename = f"dreams/{user_name.replace(' ', '_')}_{timestamp}.json"
|
169 |
|
170 |
+
# Create the dream data
|
171 |
+
dream_data = {
|
172 |
+
"user": user_name,
|
173 |
+
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
|
174 |
+
"dream_description": dream_description,
|
175 |
+
"analysis": analysis
|
176 |
+
}
|
177 |
|
178 |
+
# Save to file
|
179 |
+
with open(filename, "w", encoding="utf-8") as f:
|
180 |
+
json.dump(dream_data, f, indent=4, ensure_ascii=False)
|
181 |
+
|
182 |
+
return f"Dream saved to {filename}"
|
183 |
+
except Exception as e:
|
184 |
+
return f"Error saving dream: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
|
186 |
|
187 |
# Set up the Gradio interface
|
|
|
191 |
|
192 |
# Define the process function for the API key
|
193 |
def process_api_key(api_key):
|
194 |
+
if not api_key or not api_key.strip():
|
195 |
return "Please enter a valid API key."
|
196 |
+
result = interpreter.setup_client(api_key)
|
197 |
+
return result
|
198 |
|
199 |
# Define the process function for dream analysis
|
200 |
def process_dream(api_key, dream_description, include_visualization, user_name):
|
201 |
+
# If API key changed or not set up, configure it first
|
202 |
+
if not interpreter.client or api_key.strip() != interpreter.api_key:
|
203 |
setup_message = interpreter.setup_client(api_key)
|
204 |
if "successfully" not in setup_message:
|
205 |
return setup_message
|
206 |
|
207 |
+
# Validate dream description
|
208 |
+
if not dream_description or not dream_description.strip():
|
209 |
+
return "Please enter a dream description."
|
210 |
+
|
211 |
+
# Perform analysis
|
212 |
analysis = interpreter.analyze_dream(dream_description, include_visualization, user_name)
|
213 |
return analysis
|
214 |
|
|
|
217 |
if not analysis_output or "Error" in analysis_output:
|
218 |
return "Nothing to save or analysis contains errors."
|
219 |
|
220 |
+
if not interpreter.client or api_key.strip() != interpreter.api_key:
|
221 |
setup_message = interpreter.setup_client(api_key)
|
222 |
if "successfully" not in setup_message:
|
223 |
return setup_message
|