Spaces:
Sleeping
Sleeping
Update README.md
Browse files
README.md
CHANGED
@@ -11,3 +11,184 @@ license: mit
|
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
14 |
+
|
15 |
+
|
16 |
+
# Therapeutic Music Generator π΅ π§ββοΈ
|
17 |
+
|
18 |
+
This Hugging Face Space hosts an interactive application that generates personalized therapeutic music based on your current emotional state and desired mood. By combining mood assessment with AI-powered music generation, it creates unique musical pieces designed to support emotional well-being and mood transformation.
|
19 |
+
|
20 |
+
## Features
|
21 |
+
|
22 |
+
- **Comprehensive Mood Assessment**: Complete a quick questionnaire about your current emotional state, including:
|
23 |
+
- Energy levels
|
24 |
+
- Stress levels
|
25 |
+
- Happiness levels
|
26 |
+
- Current emotions
|
27 |
+
- Desired mood state
|
28 |
+
|
29 |
+
- **Customizable Music Preferences**: Optional settings to tailor the generated music to your taste:
|
30 |
+
- Genre preferences
|
31 |
+
- Preferred instruments
|
32 |
+
- Tempo preferences
|
33 |
+
- Musical mood preferences
|
34 |
+
|
35 |
+
- **AI-Powered Music Generation**: Utilizes Facebook's MusicGen model to create unique, therapeutic music pieces based on your inputs
|
36 |
+
|
37 |
+
## How It Works
|
38 |
+
|
39 |
+
1. **Mood Assessment**: Users complete a simple questionnaire rating their current emotional state and desired mood
|
40 |
+
2. **Music Preferences**: Optionally specify musical preferences to personalize the generated content
|
41 |
+
3. **Prompt Generation**: The system uses GPT-4-mini through LangChain to create a specialized music generation prompt
|
42 |
+
4. **Music Creation**: The prompt is processed by Facebook's MusicGen model to create a unique piece of music
|
43 |
+
5. **Delivery**: Listen to your personalized therapeutic music directly in the browser
|
44 |
+
|
45 |
+
## Code Structure and Flow
|
46 |
+
|
47 |
+
### Core Components
|
48 |
+
|
49 |
+
1. **Constants and Configurations**
|
50 |
+
```python
|
51 |
+
MOOD_QUESTIONS = [
|
52 |
+
"On a scale of 1-5, how would you rate your current energy level?",
|
53 |
+
# ... other questions
|
54 |
+
]
|
55 |
+
```
|
56 |
+
- Defines the core assessment questions used in the interface
|
57 |
+
- Maintains consistency in the mood evaluation process
|
58 |
+
|
59 |
+
2. **LangChain Setup**
|
60 |
+
```python
|
61 |
+
llm = ChatOpenAI(model="gpt-4o-mini")
|
62 |
+
music_prompt_template = """
|
63 |
+
Based on the user's mood assessment:
|
64 |
+
- Energy level: {energy}
|
65 |
+
# ... template structure
|
66 |
+
"""
|
67 |
+
```
|
68 |
+
- Initializes the language model for prompt generation
|
69 |
+
- Defines the structured template for converting mood data into music generation prompts
|
70 |
+
|
71 |
+
3. **Core Functions**
|
72 |
+
|
73 |
+
#### `analyze_mood_and_generate_prompt(responses, preferences)`
|
74 |
+
```python
|
75 |
+
def analyze_mood_and_generate_prompt(responses, preferences):
|
76 |
+
"""Convert questionnaire responses and preferences into a music generation prompt"""
|
77 |
+
try:
|
78 |
+
prompt_result = music_chain.invoke({
|
79 |
+
"energy": responses[0],
|
80 |
+
# ... parameter mapping
|
81 |
+
})
|
82 |
+
return prompt_result.content
|
83 |
+
except Exception as e:
|
84 |
+
return f"Error generating prompt: {str(e)}"
|
85 |
+
```
|
86 |
+
- Processes user inputs into a structured format
|
87 |
+
- Invokes LangChain for prompt generation
|
88 |
+
- Handles error cases gracefully
|
89 |
+
|
90 |
+
#### `generate_music(prompt, duration=10)`
|
91 |
+
```python
|
92 |
+
def generate_music(prompt, duration=10):
|
93 |
+
"""Generate music using the MusicGen API"""
|
94 |
+
API_URL = "https://api-inference.huggingface.co/models/facebook/musicgen-small"
|
95 |
+
# ... implementation
|
96 |
+
```
|
97 |
+
- Interfaces with the MusicGen API
|
98 |
+
- Handles temporary file creation for audio storage
|
99 |
+
- Manages API communication and error handling
|
100 |
+
|
101 |
+
### Application Flow
|
102 |
+
|
103 |
+
1. **Input Processing**
|
104 |
+
```mermaid
|
105 |
+
graph LR
|
106 |
+
A[User Input] --> B[Mood Assessment]
|
107 |
+
B --> C[Musical Preferences]
|
108 |
+
C --> D[Prompt Generation]
|
109 |
+
D --> E[Music Generation]
|
110 |
+
E --> F[Audio Output]
|
111 |
+
```
|
112 |
+
|
113 |
+
2. **Data Flow**
|
114 |
+
- User inputs β Gradio interface
|
115 |
+
- Interface β LangChain prompt generation
|
116 |
+
- Prompt β MusicGen API
|
117 |
+
- API response β Audio file
|
118 |
+
- Audio file β User interface
|
119 |
+
|
120 |
+
3. **Error Handling Flow**
|
121 |
+
- Input validation at the Gradio interface level
|
122 |
+
- Exception handling in prompt generation
|
123 |
+
- API error management in music generation
|
124 |
+
- User feedback through the interface
|
125 |
+
|
126 |
+
### Gradio Interface Structure
|
127 |
+
|
128 |
+
```python
|
129 |
+
with gr.Blocks() as demo:
|
130 |
+
# Interface layout
|
131 |
+
with gr.Row():
|
132 |
+
with gr.Column():
|
133 |
+
# Input components
|
134 |
+
with gr.Column():
|
135 |
+
# Output components
|
136 |
+
```
|
137 |
+
- Organized in a two-column layout
|
138 |
+
- Left column: User inputs and controls
|
139 |
+
- Right column: Generated outputs and status
|
140 |
+
|
141 |
+
## Usage
|
142 |
+
|
143 |
+
1. Move the sliders to rate your current energy, stress, and happiness levels (1-5 scale)
|
144 |
+
2. Type in your current emotions and desired mood state
|
145 |
+
3. (Optional) Fill in your musical preferences
|
146 |
+
4. Click "Generate Therapeutic Music"
|
147 |
+
5. Wait for the system to generate your personalized music
|
148 |
+
6. Listen to the generated audio and read the prompt that created it
|
149 |
+
|
150 |
+
## Technical Requirements
|
151 |
+
|
152 |
+
### Dependencies
|
153 |
+
```python
|
154 |
+
gradio
|
155 |
+
requests
|
156 |
+
langchain_openai
|
157 |
+
langchain_core
|
158 |
+
```
|
159 |
+
|
160 |
+
### Environment Variables
|
161 |
+
- `HF_API_KEY`: Your Hugging Face API key with access to the MusicGen model
|
162 |
+
|
163 |
+
### API Specifications
|
164 |
+
- MusicGen API endpoint: `https://api-inference.huggingface.co/models/facebook/musicgen-small`
|
165 |
+
- Maximum request timeout: 300 seconds
|
166 |
+
- Output format: WAV audio file
|
167 |
+
|
168 |
+
## Limitations
|
169 |
+
|
170 |
+
- Music generation may take a few minutes
|
171 |
+
- Generated audio clips are limited in duration
|
172 |
+
- The system works best with clear, specific emotional descriptions
|
173 |
+
- API rate limits may apply
|
174 |
+
- Temporary file storage considerations
|
175 |
+
|
176 |
+
## Future Improvements
|
177 |
+
|
178 |
+
- Extended music duration options
|
179 |
+
- More detailed musical customization
|
180 |
+
- Batch generation capabilities
|
181 |
+
- History tracking of generated music
|
182 |
+
- Mood improvement tracking
|
183 |
+
- Integration with additional music generation models
|
184 |
+
- Enhanced error handling and retry mechanisms
|
185 |
+
- User session management
|
186 |
+
- Feedback collection system
|
187 |
+
|
188 |
+
## Credits
|
189 |
+
|
190 |
+
- MusicGen by Facebook Research
|
191 |
+
- GPT-4-mini for prompt generation
|
192 |
+
- Hugging Face for model hosting
|
193 |
+
- Gradio for the user interface
|
194 |
+
|