|
import gradio as gr |
|
import json |
|
import os |
|
import time |
|
from openai import OpenAI |
|
|
|
class DeepDreamInterpreter: |
|
def __init__(self, api_key=None): |
|
self.api_key = api_key |
|
self.client = None |
|
if api_key: |
|
self.setup_client(api_key) |
|
|
|
def setup_client(self, api_key): |
|
"""Set up the OpenAI client with the provided API key""" |
|
if not api_key or not api_key.strip(): |
|
return "Please provide a valid API key." |
|
|
|
try: |
|
self.api_key = api_key |
|
self.client = OpenAI( |
|
base_url="https://openrouter.ai/api/v1", |
|
api_key=api_key, |
|
) |
|
|
|
test_response = self.client.chat.completions.create( |
|
extra_headers={ |
|
"HTTP-Referer": "https://deepdreaminterpreter.com", |
|
"X-Title": "Deep Dream Interpreter", |
|
}, |
|
model="google/gemini-2.5-flash-preview:thinking", |
|
messages=[ |
|
{"role": "user", "content": "Hello, this is a test."} |
|
], |
|
max_tokens=5 |
|
) |
|
return "API key configured successfully!" |
|
except Exception as e: |
|
self.client = None |
|
self.api_key = None |
|
return f"API configuration failed: {str(e)}" |
|
|
|
def analyze_dream(self, dream_description, include_visualization=True, user_name="Anonymous"): |
|
"""Analyze the provided dream description using Gemini 2.5 Flash""" |
|
if not self.client: |
|
return "Please configure your API key first." |
|
|
|
if not dream_description or not dream_description.strip(): |
|
return "Please enter a dream description to analyze." |
|
|
|
try: |
|
|
|
print("Starting dream analysis...") |
|
|
|
|
|
system_prompt = """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 |
|
- Psychological Interpretation |
|
|
|
Be thoughtful, nuanced, and avoid overgeneralizations.""" |
|
|
|
analysis_response = self.client.chat.completions.create( |
|
extra_headers={ |
|
"HTTP-Referer": "https://deepdreaminterpreter.com", |
|
"X-Title": "Deep Dream Interpreter", |
|
}, |
|
model="google/gemini-2.5-flash-preview:thinking", |
|
messages=[ |
|
{ |
|
"role": "system", |
|
"content": system_prompt |
|
}, |
|
{ |
|
"role": "user", |
|
"content": f"Dream description from user {user_name}: {dream_description}" |
|
} |
|
] |
|
) |
|
|
|
|
|
if not analysis_response or not hasattr(analysis_response, 'choices') or not analysis_response.choices: |
|
return "Error: Received an invalid response from the API during analysis." |
|
|
|
dream_analysis = analysis_response.choices[0].message.content |
|
|
|
|
|
if not include_visualization: |
|
return dream_analysis |
|
|
|
|
|
print("Generating visualization prompt...") |
|
|
|
|
|
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. |
|
Make it evocative and detailed, including: |
|
- Colors and lighting |
|
- Atmosphere and mood |
|
- Composition and perspective |
|
- Key elements and their arrangement |
|
- Symbolic representations |
|
|
|
Create something that captures the essence and emotional quality of the dream.""" |
|
|
|
visualization_response = self.client.chat.completions.create( |
|
extra_headers={ |
|
"HTTP-Referer": "https://deepdreaminterpreter.com", |
|
"X-Title": "Deep Dream Interpreter", |
|
}, |
|
model="google/gemini-2.5-flash-preview:thinking", |
|
messages=[ |
|
{ |
|
"role": "system", |
|
"content": visualization_system_prompt |
|
}, |
|
{ |
|
"role": "user", |
|
"content": f"Dream analysis: {dream_analysis}" |
|
} |
|
] |
|
) |
|
|
|
|
|
if not visualization_response or not hasattr(visualization_response, 'choices') or not visualization_response.choices: |
|
|
|
return f"""## Dream Analysis |
|
|
|
{dream_analysis} |
|
|
|
## Visualization Prompt |
|
|
|
Error: Unable to generate visualization prompt. Please try again later. |
|
""" |
|
|
|
|
|
full_response = f"""## Dream Analysis |
|
|
|
{dream_analysis} |
|
|
|
## Visualization Prompt |
|
|
|
{visualization_response.choices[0].message.content} |
|
|
|
--- |
|
*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.* |
|
""" |
|
return full_response |
|
|
|
except Exception as e: |
|
error_message = str(e) |
|
print(f"Error during analysis: {error_message}") |
|
return f"Error during analysis: {error_message}\n\nPlease check your API key and try again." |
|
|
|
def save_dream(self, user_name, dream_description, analysis): |
|
"""Save the dream and its analysis to a JSON file""" |
|
try: |
|
if not user_name or not user_name.strip(): |
|
user_name = "Anonymous" |
|
|
|
|
|
os.makedirs("dreams", exist_ok=True) |
|
|
|
|
|
timestamp = time.strftime("%Y%m%d-%H%M%S") |
|
filename = f"dreams/{user_name.replace(' ', '_')}_{timestamp}.json" |
|
|
|
|
|
dream_data = { |
|
"user": user_name, |
|
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"), |
|
"dream_description": dream_description, |
|
"analysis": analysis |
|
} |
|
|
|
|
|
with open(filename, "w", encoding="utf-8") as f: |
|
json.dump(dream_data, f, indent=4, ensure_ascii=False) |
|
|
|
return f"Dream saved to {filename}" |
|
except Exception as e: |
|
return f"Error saving dream: {str(e)}" |
|
|
|
|
|
|
|
def create_gradio_interface(): |
|
|
|
interpreter = DeepDreamInterpreter() |
|
|
|
|
|
def process_api_key(api_key): |
|
if not api_key or not api_key.strip(): |
|
return "Please enter a valid API key." |
|
result = interpreter.setup_client(api_key) |
|
return result |
|
|
|
|
|
def process_dream(api_key, dream_description, include_visualization, user_name): |
|
|
|
if not interpreter.client or api_key.strip() != interpreter.api_key: |
|
setup_message = interpreter.setup_client(api_key) |
|
if "successfully" not in setup_message: |
|
return setup_message |
|
|
|
|
|
if not dream_description or not dream_description.strip(): |
|
return "Please enter a dream description." |
|
|
|
|
|
analysis = interpreter.analyze_dream(dream_description, include_visualization, user_name) |
|
return analysis |
|
|
|
|
|
def save_dream_entry(api_key, user_name, dream_description, analysis_output): |
|
if not analysis_output or "Error" in analysis_output: |
|
return "Nothing to save or analysis contains errors." |
|
|
|
if not interpreter.client or api_key.strip() != interpreter.api_key: |
|
setup_message = interpreter.setup_client(api_key) |
|
if "successfully" not in setup_message: |
|
return setup_message |
|
|
|
return interpreter.save_dream(user_name, dream_description, analysis_output) |
|
|
|
|
|
with gr.Blocks(title="Deep Dream Interpreter") as app: |
|
gr.Markdown("# 🌙 Deep Dream Interpreter") |
|
gr.Markdown("Analyze your dreams using Gemini 2.5 Flash with thinking capabilities.") |
|
|
|
with gr.Tab("Dream Analysis"): |
|
with gr.Row(): |
|
with gr.Column(): |
|
api_key = gr.Textbox( |
|
label="OpenRouter API Key", |
|
placeholder="Enter your OpenRouter API key here", |
|
type="password" |
|
) |
|
api_status = gr.Textbox(label="API Status", interactive=False) |
|
api_button = gr.Button("Configure API") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
user_name = gr.Textbox( |
|
label="Your Name (Optional)", |
|
placeholder="Enter your name or leave blank for anonymous" |
|
) |
|
dream_description = gr.Textbox( |
|
label="Dream Description", |
|
placeholder="Describe your dream in detail...", |
|
lines=10 |
|
) |
|
include_visualization = gr.Checkbox( |
|
label="Include visualization prompt", |
|
value=True |
|
) |
|
|
|
with gr.Row(): |
|
analyze_button = gr.Button("Analyze Dream", variant="primary") |
|
save_button = gr.Button("Save Analysis") |
|
|
|
with gr.Column(): |
|
analysis_output = gr.Markdown(label="Analysis Results") |
|
save_status = gr.Textbox(label="Save Status", interactive=False) |
|
|
|
with gr.Tab("About"): |
|
gr.Markdown(""" |
|
## About Deep Dream Interpreter |
|
|
|
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. |
|
|
|
### Features: |
|
- **Dream Analysis**: Get a detailed analysis of your dream's symbols, emotions, and themes |
|
- **Visualization Prompts**: Receive detailed prompts that can be used with image generation models |
|
- **Save Your Dreams**: Save your dream descriptions and analyses for future reference |
|
|
|
### Privacy Note: |
|
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. |
|
|
|
### Getting Started: |
|
1. Enter your OpenRouter API key |
|
2. Enter your dream description |
|
3. Click "Analyze Dream" |
|
4. Optionally save your analysis |
|
|
|
### Requirements: |
|
- An OpenRouter API key with access to Google's Gemini 2.5 Flash Preview model |
|
""") |
|
|
|
|
|
api_button.click(process_api_key, inputs=api_key, outputs=api_status) |
|
analyze_button.click( |
|
process_dream, |
|
inputs=[api_key, dream_description, include_visualization, user_name], |
|
outputs=analysis_output |
|
) |
|
save_button.click( |
|
save_dream_entry, |
|
inputs=[api_key, user_name, dream_description, analysis_output], |
|
outputs=save_status |
|
) |
|
|
|
return app |
|
|
|
|
|
if __name__ == "__main__": |
|
app = create_gradio_interface() |
|
app.launch() |