Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
|
|
2 |
import json
|
3 |
import os
|
4 |
import time
|
5 |
-
import requests
|
6 |
from openai import OpenAI
|
7 |
|
8 |
class DeepDreamInterpreter:
|
@@ -29,66 +28,35 @@ class DeepDreamInterpreter:
|
|
29 |
self.api_key = None
|
30 |
return f"API configuration failed: {str(e)}"
|
31 |
|
32 |
-
def _make_api_call(self, messages, model="google/gemini-
|
33 |
"""Make an API call to OpenRouter with proper error handling"""
|
34 |
if not self.client:
|
35 |
raise ValueError("API client not configured")
|
36 |
|
37 |
try:
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
payload = {
|
47 |
-
"model": model,
|
48 |
-
"messages": messages
|
49 |
-
}
|
50 |
-
|
51 |
-
response = requests.post(
|
52 |
-
"https://openrouter.ai/api/v1/chat/completions",
|
53 |
-
headers=headers,
|
54 |
-
json=payload
|
55 |
)
|
56 |
|
57 |
-
# Check if response is successful
|
58 |
-
if response.status_code != 200:
|
59 |
-
error_msg = f"API Error (Status {response.status_code}): "
|
60 |
-
try:
|
61 |
-
error_data = response.json()
|
62 |
-
error_msg += error_data.get('error', {}).get('message', 'Unknown error')
|
63 |
-
except:
|
64 |
-
error_msg += response.text[:100] + "..."
|
65 |
-
|
66 |
-
print(f"API error: {error_msg}")
|
67 |
-
raise ValueError(error_msg)
|
68 |
-
|
69 |
-
# Parse the response
|
70 |
-
data = response.json()
|
71 |
-
|
72 |
-
# Validate response structure
|
73 |
-
if 'choices' not in data or not data['choices'] or 'message' not in data['choices'][0]:
|
74 |
-
raise ValueError("Invalid response structure from API")
|
75 |
-
|
76 |
# Extract the content
|
77 |
-
content =
|
78 |
if not content:
|
79 |
raise ValueError("Empty content in API response")
|
80 |
|
81 |
return content
|
82 |
|
83 |
-
except requests.exceptions.RequestException as e:
|
84 |
-
raise ValueError(f"Network error: {str(e)}")
|
85 |
-
except json.JSONDecodeError:
|
86 |
-
raise ValueError("Invalid JSON response from API")
|
87 |
except Exception as e:
|
88 |
-
|
|
|
|
|
89 |
|
90 |
def analyze_dream(self, dream_description, include_visualization=True, user_name="Anonymous"):
|
91 |
-
"""Analyze the provided dream description using Gemini
|
92 |
if not self.client:
|
93 |
return "Please configure your API key first."
|
94 |
|
@@ -135,7 +103,6 @@ class DeepDreamInterpreter:
|
|
135 |
# If visualization is not needed, return just the analysis
|
136 |
if not include_visualization:
|
137 |
return f"""## Dream Analysis
|
138 |
-
|
139 |
{dream_analysis}"""
|
140 |
|
141 |
# Second pass: Visual interpretation
|
@@ -166,13 +133,9 @@ class DeepDreamInterpreter:
|
|
166 |
|
167 |
# Combine the results
|
168 |
full_response = f"""## Dream Analysis
|
169 |
-
|
170 |
{dream_analysis}
|
171 |
-
|
172 |
## Visualization Prompt
|
173 |
-
|
174 |
{visualization_prompt}
|
175 |
-
|
176 |
---
|
177 |
*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.*
|
178 |
"""
|
@@ -182,13 +145,9 @@ class DeepDreamInterpreter:
|
|
182 |
# If visualization fails, still return the analysis
|
183 |
print(f"Visualization error: {str(e)}")
|
184 |
return f"""## Dream Analysis
|
185 |
-
|
186 |
{dream_analysis}
|
187 |
-
|
188 |
## Visualization Prompt
|
189 |
-
|
190 |
Error: Unable to generate visualization prompt. Please try again later.
|
191 |
-
|
192 |
---
|
193 |
*Note: The dream analysis was successful, but we encountered an issue generating the visualization prompt.*
|
194 |
"""
|
@@ -279,7 +238,7 @@ def create_gradio_interface():
|
|
279 |
# Create the Gradio interface
|
280 |
with gr.Blocks(title="Deep Dream Interpreter") as app:
|
281 |
gr.Markdown("# 🌙 Deep Dream Interpreter")
|
282 |
-
gr.Markdown("Analyze your dreams using Gemini
|
283 |
|
284 |
with gr.Tab("Dream Analysis"):
|
285 |
with gr.Row():
|
@@ -320,7 +279,7 @@ def create_gradio_interface():
|
|
320 |
gr.Markdown("""
|
321 |
## About Deep Dream Interpreter
|
322 |
|
323 |
-
This application uses Google's Gemini
|
324 |
|
325 |
### Features:
|
326 |
- **Dream Analysis**: Get a detailed analysis of your dream's symbols, emotions, and themes
|
@@ -337,12 +296,12 @@ def create_gradio_interface():
|
|
337 |
4. Optionally save your analysis
|
338 |
|
339 |
### Troubleshooting:
|
340 |
-
- If you encounter an error, make sure your API key is valid and has access to the Gemini
|
341 |
- Check that your OpenRouter account has sufficient credits
|
342 |
- Try a shorter dream description if you're experiencing timeout issues
|
343 |
|
344 |
### Requirements:
|
345 |
-
- An OpenRouter API key with access to Google's Gemini
|
346 |
""")
|
347 |
|
348 |
# Set up the event handlers
|
|
|
2 |
import json
|
3 |
import os
|
4 |
import time
|
|
|
5 |
from openai import OpenAI
|
6 |
|
7 |
class DeepDreamInterpreter:
|
|
|
28 |
self.api_key = None
|
29 |
return f"API configuration failed: {str(e)}"
|
30 |
|
31 |
+
def _make_api_call(self, messages, model="google/gemini-flash-1.5"):
|
32 |
"""Make an API call to OpenRouter with proper error handling"""
|
33 |
if not self.client:
|
34 |
raise ValueError("API client not configured")
|
35 |
|
36 |
try:
|
37 |
+
response = self.client.chat.completions.create(
|
38 |
+
model=model,
|
39 |
+
messages=messages,
|
40 |
+
extra_headers={
|
41 |
+
"HTTP-Referer": "https://deepdreaminterpreter.com",
|
42 |
+
"X-Title": "Deep Dream Interpreter"
|
43 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
)
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
# Extract the content
|
47 |
+
content = response.choices[0].message.content
|
48 |
if not content:
|
49 |
raise ValueError("Empty content in API response")
|
50 |
|
51 |
return content
|
52 |
|
|
|
|
|
|
|
|
|
53 |
except Exception as e:
|
54 |
+
error_msg = f"API error: {str(e)}"
|
55 |
+
print(f"API error: {error_msg}")
|
56 |
+
raise ValueError(error_msg)
|
57 |
|
58 |
def analyze_dream(self, dream_description, include_visualization=True, user_name="Anonymous"):
|
59 |
+
"""Analyze the provided dream description using Gemini Flash 1.5"""
|
60 |
if not self.client:
|
61 |
return "Please configure your API key first."
|
62 |
|
|
|
103 |
# If visualization is not needed, return just the analysis
|
104 |
if not include_visualization:
|
105 |
return f"""## Dream Analysis
|
|
|
106 |
{dream_analysis}"""
|
107 |
|
108 |
# Second pass: Visual interpretation
|
|
|
133 |
|
134 |
# Combine the results
|
135 |
full_response = f"""## Dream Analysis
|
|
|
136 |
{dream_analysis}
|
|
|
137 |
## Visualization Prompt
|
|
|
138 |
{visualization_prompt}
|
|
|
139 |
---
|
140 |
*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.*
|
141 |
"""
|
|
|
145 |
# If visualization fails, still return the analysis
|
146 |
print(f"Visualization error: {str(e)}")
|
147 |
return f"""## Dream Analysis
|
|
|
148 |
{dream_analysis}
|
|
|
149 |
## Visualization Prompt
|
|
|
150 |
Error: Unable to generate visualization prompt. Please try again later.
|
|
|
151 |
---
|
152 |
*Note: The dream analysis was successful, but we encountered an issue generating the visualization prompt.*
|
153 |
"""
|
|
|
238 |
# Create the Gradio interface
|
239 |
with gr.Blocks(title="Deep Dream Interpreter") as app:
|
240 |
gr.Markdown("# 🌙 Deep Dream Interpreter")
|
241 |
+
gr.Markdown("Analyze your dreams using Gemini Flash 1.5.")
|
242 |
|
243 |
with gr.Tab("Dream Analysis"):
|
244 |
with gr.Row():
|
|
|
279 |
gr.Markdown("""
|
280 |
## About Deep Dream Interpreter
|
281 |
|
282 |
+
This application uses Google's Gemini Flash 1.5 model 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.
|
283 |
|
284 |
### Features:
|
285 |
- **Dream Analysis**: Get a detailed analysis of your dream's symbols, emotions, and themes
|
|
|
296 |
4. Optionally save your analysis
|
297 |
|
298 |
### Troubleshooting:
|
299 |
+
- If you encounter an error, make sure your API key is valid and has access to the Gemini Flash 1.5 model
|
300 |
- Check that your OpenRouter account has sufficient credits
|
301 |
- Try a shorter dream description if you're experiencing timeout issues
|
302 |
|
303 |
### Requirements:
|
304 |
+
- An OpenRouter API key with access to Google's Gemini Flash 1.5 model
|
305 |
""")
|
306 |
|
307 |
# Set up the event handlers
|