ciaochris commited on
Commit
9ea7aeb
·
verified ·
1 Parent(s): 55bae06

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +175 -187
app.py CHANGED
@@ -12,222 +12,210 @@
12
  # See the License for the specific language governing permissions and
13
  # limitations under the License.
14
 
 
15
  import gradio as gr
 
16
  import numpy as np
17
  import soundfile as sf
18
  from sentence_transformers import SentenceTransformer
19
  from sklearn.metrics.pairwise import cosine_similarity
 
 
 
20
 
21
- # Load the pre-trained Sentence Transformer model
22
- model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
 
 
23
 
24
- # Function to generate a subliminal message
25
- def generate_subliminal(message, sample_rate=44100, duration=30, subliminal_volume=0.1):
26
- t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
27
-
28
- # Convert the message to a series of frequencies
29
- frequencies = [ord(char) * 10 for char in message] # Simple mapping of characters to frequencies
30
-
31
- subliminal_wave = np.zeros_like(t)
32
- for freq in frequencies:
33
- subliminal_wave += np.sin(2 * np.pi * freq * t)
34
-
35
- # Normalize and adjust volume
36
- subliminal_wave = subliminal_wave / np.max(np.abs(subliminal_wave)) * subliminal_volume
37
-
38
- return subliminal_wave
39
-
40
- # Function to combine subliminal message with a frequency tone
41
- def combine_audio(subliminal_waveform, frequency, duration=30, sample_rate=44100):
42
- t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
43
- sine_wave = 0.5 * np.sin(2 * np.pi * frequency * t)
44
- combined_waveform = sine_wave + subliminal_waveform
45
-
46
- # Normalize the combined waveform
47
- combined_waveform = combined_waveform / np.max(np.abs(combined_waveform))
48
-
49
- file_name = "combined_audio.wav"
50
- sf.write(file_name, combined_waveform, sample_rate)
51
- return file_name
52
 
53
- # Define vibrational frequencies and corresponding advice
54
  vibrational_advice = {
55
  "anxious": {
56
- "name": "396 Hz - Liberation from Fear",
57
  "frequency": 396,
58
- "advice": "1. Practice deep breathing exercises to calm your mind.\n"
59
- "2. Write down your fears and reflect on their source.\n"
60
- "3. Surround yourself with positive affirmations and energy.\n"
61
- "4. Visualize letting go of fear during meditation.\n"
62
- "5. Listen to 396 Hz music while meditating.",
63
- "subliminal": "You are calm and at peace."
 
64
  },
65
  "stressed": {
66
- "name": "417 Hz - Facilitating Change",
67
- "frequency": 417,
68
- "advice": "1. Take short breaks to clear your mind during work.\n"
69
- "2. Practice progressive muscle relaxation to reduce tension.\n"
70
- "3. Break down overwhelming tasks into smaller steps.\n"
71
- "4. Focus on things you can control and let go of what you can't.\n"
72
- "5. Listen to 417 Hz music to release negative energy.",
73
- "subliminal": "You handle challenges with ease."
74
  },
75
  "calm": {
76
- "name": "528 Hz - Transformation and Miracles",
77
- "frequency": 528,
78
- "advice": "1. Embrace gratitude by listing things you're thankful for.\n"
79
- "2. Focus on activities that enhance your inner peace.\n"
80
- "3. Connect with nature to maintain your calm state.\n"
81
- "4. Visualize your energy expanding harmoniously.\n"
82
- "5. Listen to 528 Hz music to amplify feelings of love and peace.",
83
- "subliminal": "You radiate peace and harmony."
84
  },
85
  "sad": {
86
- "name": "639 Hz - Harmonizing Relationships",
87
- "frequency": 639,
88
- "advice": "1. Reach out to a friend or loved one to talk about your feelings.\n"
89
- "2. Practice self-compassion and avoid self-criticism.\n"
90
- "3. Engage in creative expression, such as journaling or drawing.\n"
91
- "4. Focus on rebuilding positive relationships in your life.\n"
92
- "5. Listen to 639 Hz music to nurture emotional healing.",
93
- "subliminal": "You are worthy of love and happiness."
94
  },
95
  "angry": {
96
- "name": "741 Hz - Expression and Solutions",
97
- "frequency": 741,
98
- "advice": "1. Take a step back and breathe deeply to cool down.\n"
99
- "2. Channel your anger into physical activity, like a workout.\n"
100
- "3. Practice mindfulness to understand the root of your anger.\n"
101
- "4. Use 'I' statements when expressing your feelings to others.\n"
102
- "5. Listen to 741 Hz music to balance your energy.",
103
- "subliminal": "You respond to life with patience and understanding."
104
  },
105
  "fearful": {
106
- "name": "852 Hz - Awakening Intuition",
107
- "frequency": 852,
108
- "advice": "1. Ground yourself by focusing on the present moment.\n"
109
- "2. Visualize releasing fear and embracing trust.\n"
110
- "3. Focus on building resilience and inner strength.\n"
111
- "4. Journal your fears and look for patterns.\n"
112
- "5. Listen to 852 Hz music to activate your intuition.",
113
- "subliminal": "You are safe and protected."
114
  },
115
  "confused": {
116
- "name": "285 Hz - Returning to Source",
117
  "frequency": 285,
118
- "advice": "1. Take time to reflect in a quiet space.\n"
119
- "2. Ask yourself what truly matters in your current situation.\n"
120
- "3. Break down complex problems into simpler parts.\n"
121
- "4. Meditate to clear your mind and reduce mental clutter.\n"
122
- "5. Listen to 285 Hz music while reflecting or problem-solving.",
123
- "subliminal": "Your mind is clear and focused."
 
 
 
 
 
 
 
 
 
 
 
124
  }
125
  }
126
 
127
- # Function to analyze user's emotional state and provide a tone
128
- def analyze_emotion(input_text, uploaded_file=None):
129
- input_embedding = model.encode([input_text])
130
- emotions = {
131
- "anxious": ["I feel nervous and uneasy.", "I'm afraid of something going wrong."],
132
- "stressed": ["I'm overwhelmed with work.", "I feel like I'm under a lot of pressure."],
133
- "calm": ["I feel peaceful and at ease.", "Everything is going well."],
134
- "sad": ["I feel down and unhappy.", "I've been feeling quite low."],
135
- "angry": ["I'm furious about something.", "I can't control my anger."],
136
- "fearful": ["I'm scared and worried.", "I have a lot of fear right now."],
137
- "confused": ["I don't know what to do.", "I'm feeling uncertain and lost."]
138
- }
139
- emotion_embeddings = {emotion: model.encode(phrases) for emotion, phrases in emotions.items()}
140
- best_emotion = max(emotion_embeddings.items(), key=lambda x: np.max(cosine_similarity(input_embedding, x[1])))[0]
141
- advice = vibrational_advice[best_emotion]
142
- subliminal_message = advice["subliminal"]
143
- subliminal_waveform = generate_subliminal(subliminal_message, duration=30)
144
- combined_audio = combine_audio(subliminal_waveform, advice["frequency"], duration=30)
145
-
146
- if uploaded_file:
147
- uploaded_audio, sample_rate = sf.read(uploaded_file.name)
148
- combined_waveform, _ = sf.read(combined_audio)
149
-
150
- # Adjust the length of the combined waveform to match the uploaded audio
151
- if len(combined_waveform) > len(uploaded_audio):
152
- combined_waveform = combined_waveform[:len(uploaded_audio)]
153
- else:
154
- combined_waveform = np.pad(combined_waveform, (0, len(uploaded_audio) - len(combined_waveform)))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
 
156
- combined_waveform = combined_waveform + uploaded_audio
157
- combined_audio = "combined_with_upload.wav"
158
- sf.write(combined_audio, combined_waveform, sample_rate)
159
 
160
- return best_emotion, advice["name"], advice["advice"], combined_audio
 
 
 
 
 
161
 
162
- # Gradio interface function
163
- def wellness_analyzer(input_text, uploaded_file=None):
164
- emotion, tone_name, advice, audio_file = analyze_emotion(input_text, uploaded_file)
 
165
 
166
- output = """
167
- <div style="font-family: Arial, sans-serif; line-height: 1.6;">
168
- <h3 style="color: #4a4a4a;">Detected Emotional State: {0}</h3>
169
- <p><strong>Recommended Tone:</strong> {1}</p>
170
- <p><strong>Personalized Advice:</strong></p>
171
- <ol>
172
- {2}
173
- </ol>
174
- <p><em>Listen to the generated audio file for a custom 30-second vibrational experience with subliminal messaging.</em></p>
175
- </div>
176
- """.format(
177
- emotion.capitalize(),
178
- tone_name,
179
- "".join("<li>{0}</li>".format(advice_item.strip()) for advice_item in advice.split("\n") if advice_item.strip())
180
- )
181
 
182
- return output, audio_file
183
-
184
- # Define custom CSS
185
- custom_css = """
186
- .footer {
187
- text-align: center;
188
- margin-top: 20px;
189
- font-style: italic;
190
- }
191
- """
192
-
193
- # Update the Gradio interface
194
- with gr.Blocks(css=custom_css) as interface:
195
- gr.Markdown(
196
- """
197
- <div style="text-align: center; max-width: 800px; margin: 0 auto;">
198
- <h1>Vers3Dynamics Vibrational Wellness 🌟</h1>
199
- <p>Discover your emotional resonance and receive personalized vibrational advice with subliminal messaging.</p>
200
- </div>
201
- """
202
- )
203
-
204
- with gr.Row():
205
- with gr.Column(scale=2):
206
- input_text = gr.Textbox(
207
- label="Describe your current emotional state 🤲",
208
- placeholder="How are you feeling right now?",
209
- lines=3
210
- )
211
- uploaded_file = gr.File(label="Upload music (optional) 🎵")
212
- analyze_btn = gr.Button("Analyze Mood", variant="primary")
213
-
214
- with gr.Column(scale=3):
215
- output_text = gr.HTML(label="Analysis and Advice")
216
- output_audio = gr.Audio(label="Your Custom 30-second Vibrational Tone with Subliminal Message 🎶", type="filepath")
217
-
218
- gr.Markdown(
219
- """
220
- <div class="footer">
221
- <p>Vers3Dynamics - Harmonizing your well-being through vibrational frequencies and subliminal messaging.</p>
222
- </div>
223
- """
224
- )
225
-
226
- analyze_btn.click(
227
- wellness_analyzer,
228
- inputs=[input_text, uploaded_file],
229
- outputs=[output_text, output_audio]
230
- )
231
-
232
- # Launch the interface
233
- interface.launch()
 
12
  # See the License for the specific language governing permissions and
13
  # limitations under the License.
14
 
15
+ import os
16
  import gradio as gr
17
+ from groq import Groq
18
  import numpy as np
19
  import soundfile as sf
20
  from sentence_transformers import SentenceTransformer
21
  from sklearn.metrics.pairwise import cosine_similarity
22
+ import matplotlib.pyplot as plt
23
+ from PIL import Image
24
+ import io
25
 
26
+ # Initialize Groq client
27
+ client = Groq(
28
+ api_key=os.environ.get("GROQ_API_KEY"),
29
+ )
30
 
31
+ # Load pre-trained sentence transformer model
32
+ model = SentenceTransformer('all-MiniLM-L6-v2')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+ # Vibrational advice dictionary
35
  vibrational_advice = {
36
  "anxious": {
 
37
  "frequency": 396,
38
+ "name": "Liberating Guilt and Fear",
39
+ "advice": "The 396 Hz frequency is associated with releasing fear and guilt. Try these techniques:\n"
40
+ "1. Practice deep, slow breathing exercises for 5-10 minutes.\n"
41
+ "2. Use positive affirmations like 'I am safe' or 'I release my fears'.\n"
42
+ "3. Ground yourself by walking barefoot on grass or soil.\n"
43
+ "4. Listen to 396 Hz music while meditating or before sleep.\n"
44
+ "5. Try progressive muscle relaxation to release physical tension."
45
  },
46
  "stressed": {
47
+ "frequency": 528,
48
+ "name": "Transformation and Miracles",
49
+ "advice": "The 528 Hz frequency is known as the 'miracle tone' for transformation and DNA repair. Consider these practices:\n"
50
+ "1. Start your day with a gratitude journal, listing 3-5 things you're thankful for.\n"
51
+ "2. Practice mindfulness meditation focusing on your breath for 10-15 minutes.\n"
52
+ "3. Take regular breaks during work to stretch and reset your mind.\n"
53
+ "4. Listen to 528 Hz music during your self-care routine or while working.\n"
54
+ "5. Engage in creative activities like drawing, coloring, or crafting to reduce stress."
55
  },
56
  "calm": {
57
+ "frequency": 741,
58
+ "name": "Awakening Intuition",
59
+ "advice": "The 741 Hz frequency is associated with awakening intuition and solving problems. Enhance your state with these tips:\n"
60
+ "1. Start a dream journal to tap into your subconscious mind.\n"
61
+ "2. Practice visualization techniques, imagining your goals and aspirations.\n"
62
+ "3. Engage in mindful movement like yoga or tai chi.\n"
63
+ "4. Spend time in nature to maintain your sense of calm and clarity.\n"
64
+ "5. Listen to 741 Hz music during problem-solving or creative sessions."
65
  },
66
  "sad": {
67
+ "frequency": 417,
68
+ "name": "Facilitating Change",
69
+ "advice": "The 417 Hz frequency is believed to facilitate change and let go of negative energy. Try these approaches:\n"
70
+ "1. Write a letter expressing your feelings, then safely burn or discard it as a symbolic release.\n"
71
+ "2. Engage in physical exercise to boost endorphins and improve mood.\n"
72
+ "3. Reach out to a friend or loved one for support and connection.\n"
73
+ "4. Practice self-compassion meditation, treating yourself with kindness.\n"
74
+ "5. Listen to 417 Hz music while journaling or during a relaxing bath."
75
  },
76
  "angry": {
77
+ "frequency": 852,
78
+ "name": "Returning to Spiritual Order",
79
+ "advice": "The 852 Hz frequency is associated with returning to spiritual order and inner strength. Consider these techniques:\n"
80
+ "1. Practice 'square breathing': inhale for 4 counts, hold for 4, exhale for 4, hold for 4, repeat.\n"
81
+ "2. Use positive self-talk to reframe the situation causing anger.\n"
82
+ "3. Engage in a physical activity like running or punching a pillow to release tension.\n"
83
+ "4. Write down your angry thoughts, then rewrite them from a calm perspective.\n"
84
+ "5. Listen to 852 Hz music during meditation or while practicing forgiveness exercises."
85
  },
86
  "fearful": {
87
+ "frequency": 639,
88
+ "name": "Connecting Relationships",
89
+ "advice": "The 639 Hz frequency is linked to connecting relationships and understanding. When feeling fearful, try these:\n"
90
+ "1. Reach out to a trusted friend or family member to share your fears.\n"
91
+ "2. Practice 'loving-kindness' meditation to foster feelings of safety and connection.\n"
92
+ "3. Create a 'fear ladder' and gradually expose yourself to what you fear in small steps.\n"
93
+ "4. Use positive visualization to imagine successfully overcoming your fears.\n"
94
+ "5. Listen to 639 Hz music while working on building your support network."
95
  },
96
  "confused": {
 
97
  "frequency": 285,
98
+ "name": "Quantum Cognition",
99
+ "advice": "The 285 Hz frequency is believed to influence energy fields and aid in healing. When feeling confused, consider:\n"
100
+ "1. Create a mind map to visually organize your thoughts and ideas.\n"
101
+ "2. Take a short walk to clear your mind and gain new perspectives.\n"
102
+ "3. Practice the 'brain dump' technique: write down all your thoughts without judgment.\n"
103
+ "4. Use the Pomodoro technique to break tasks into manageable chunks.\n"
104
+ "5. Listen to 285 Hz music while studying or problem-solving to enhance focus."
105
+ },
106
+ "happy": {
107
+ "frequency": 432,
108
+ "name": "Harmonizing Vibrations",
109
+ "advice": "The 432 Hz frequency is associated with harmonizing vibrations and promoting wellbeing. Enhance your happy state with these practices:\n"
110
+ "1. Express gratitude by sharing your joy with others or writing in a gratitude journal.\n"
111
+ "2. Engage in activities that bring you joy and fulfillment.\n"
112
+ "3. Practice 'savoring' by fully immersing yourself in positive experiences.\n"
113
+ "4. Share your positive energy through acts of kindness or volunteering.\n"
114
+ "5. Listen to 432 Hz music to maintain and amplify your positive vibrations."
115
  }
116
  }
117
 
118
+ # Create embeddings for predefined emotions
119
+ emotion_embeddings = {emotion: model.encode([emotion]) for emotion in vibrational_advice.keys()}
120
+
121
+ def get_closest_emotion(detected_emotion):
122
+ detected_embedding = model.encode([detected_emotion])
123
+ similarities = {emotion: cosine_similarity(detected_embedding, embedding)[0][0]
124
+ for emotion, embedding in emotion_embeddings.items()}
125
+ return max(similarities, key=similarities.get)
126
+
127
+ def generate_tone(frequency, duration=5, sample_rate=44100):
128
+ t = np.linspace(0, duration, int(sample_rate * duration), False)
129
+ tone = np.sin(2 * np.pi * frequency * t)
130
+ return (tone * 32767).astype(np.int16)
131
+
132
+ def save_tone(frequency):
133
+ tone = generate_tone(frequency)
134
+ filename = f"/tmp/tone_{frequency}Hz.wav"
135
+ sf.write(filename, tone, 44100)
136
+ return filename
137
+
138
+ def generate_waveform(frequency):
139
+ duration = 0.1 # Short duration for visualization
140
+ sample_rate = 44100
141
+ t = np.linspace(0, duration, int(sample_rate * duration), False)
142
+ tone = np.sin(2 * np.pi * frequency * t)
143
+
144
+ plt.figure(figsize=(10, 4))
145
+ plt.plot(t, tone)
146
+ plt.title(f"Waveform of {frequency} Hz Tone")
147
+ plt.xlabel("Time (s)")
148
+ plt.ylabel("Amplitude")
149
+ plt.ylim(-1.1, 1.1)
150
+ plt.grid(True)
151
+
152
+ buf = io.BytesIO()
153
+ plt.savefig(buf, format='png')
154
+ buf.seek(0)
155
+ plt.close()
156
+
157
+ return Image.open(buf)
158
+
159
+ def transcribe_audio(audio_path):
160
+ try:
161
+ # Open and read the audio file
162
+ with open(audio_path, "rb") as audio_file:
163
+ audio_data = audio_file.read()
164
+
165
+ # Transcribe the audio using Distil-Whisper
166
+ transcription = client.audio.transcriptions.create(
167
+ file=(os.path.basename(audio_path), audio_data),
168
+ model="distil-whisper-large-v3-en",
169
+ response_format="verbose_json",
170
+ )
171
 
172
+ return transcription.text
173
+ except Exception as e:
174
+ return f"Error in transcription: {str(e)}"
175
 
176
+ def vibrational_wellness(input_text, audio_input):
177
+ if audio_input:
178
+ input_text = transcribe_audio(audio_input)
179
+
180
+ if not input_text.strip():
181
+ return "Please enter a description of your emotional state or provide an audio input.", None, None, None
182
 
183
+ prompt = f"""Analyze the following text and determine the primary emotion expressed.
184
+ Choose the most appropriate emotion from this list: anxious, stressed, calm, sad, angry, fearful, confused, happy.\n
185
+ Input: "{input_text}"\n
186
+ Emotion:"""
187
 
188
+ detected_emotion = client.completions.create(
189
+ model="gpt-3.5-turbo",
190
+ prompt=prompt,
191
+ max_tokens=10,
192
+ temperature=0,
193
+ ).choices[0].text.strip()
194
+
195
+ closest_emotion = get_closest_emotion(detected_emotion)
196
+ frequency = vibrational_advice[closest_emotion]["frequency"]
 
 
 
 
 
 
197
 
198
+ tone_file = save_tone(frequency)
199
+ waveform_image = generate_waveform(frequency)
200
+
201
+ return (vibrational_advice[closest_emotion]["advice"], tone_file, waveform_image)
202
+
203
+ def launch_interface():
204
+ with gr.Blocks() as demo:
205
+ gr.Markdown("<h1>Vibrational Wellness Advisor</h1>")
206
+ with gr.Row():
207
+ with gr.Column():
208
+ input_text = gr.Textbox(label="Describe your emotional state", placeholder="E.g., I feel anxious...")
209
+ audio_input = gr.Audio(label="Or upload an audio file", type="filepath", source="upload")
210
+ submit_btn = gr.Button("Get Vibrational Advice")
211
+ with gr.Column():
212
+ advice_output = gr.Markdown(label="Vibrational Advice")
213
+ tone_output = gr.Audio(label="Generated Tone")
214
+ waveform_output = gr.Image(label="Waveform")
215
+
216
+ submit_btn.click(vibrational_wellness, inputs=[input_text, audio_input], outputs=[advice_output, tone_output, waveform_output])
217
+
218
+ demo.launch()
219
+
220
+ if __name__ == "__main__":
221
+ launch_interface()