Spaces:
Running
Running
Update app.py
Browse files
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 |
-
#
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 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 |
-
#
|
| 54 |
vibrational_advice = {
|
| 55 |
"anxious": {
|
| 56 |
-
"name": "396 Hz - Liberation from Fear",
|
| 57 |
"frequency": 396,
|
| 58 |
-
"
|
| 59 |
-
|
| 60 |
-
"
|
| 61 |
-
"
|
| 62 |
-
"
|
| 63 |
-
|
|
|
|
| 64 |
},
|
| 65 |
"stressed": {
|
| 66 |
-
"
|
| 67 |
-
"
|
| 68 |
-
"advice": "
|
| 69 |
-
"
|
| 70 |
-
"
|
| 71 |
-
"
|
| 72 |
-
"
|
| 73 |
-
|
| 74 |
},
|
| 75 |
"calm": {
|
| 76 |
-
"
|
| 77 |
-
"
|
| 78 |
-
"advice": "
|
| 79 |
-
"
|
| 80 |
-
"
|
| 81 |
-
"
|
| 82 |
-
"
|
| 83 |
-
|
| 84 |
},
|
| 85 |
"sad": {
|
| 86 |
-
"
|
| 87 |
-
"
|
| 88 |
-
"advice": "
|
| 89 |
-
"
|
| 90 |
-
"
|
| 91 |
-
"
|
| 92 |
-
"
|
| 93 |
-
|
| 94 |
},
|
| 95 |
"angry": {
|
| 96 |
-
"
|
| 97 |
-
"
|
| 98 |
-
"advice": "
|
| 99 |
-
"
|
| 100 |
-
"
|
| 101 |
-
"
|
| 102 |
-
"
|
| 103 |
-
|
| 104 |
},
|
| 105 |
"fearful": {
|
| 106 |
-
"
|
| 107 |
-
"
|
| 108 |
-
"advice": "
|
| 109 |
-
"
|
| 110 |
-
"
|
| 111 |
-
"
|
| 112 |
-
"
|
| 113 |
-
|
| 114 |
},
|
| 115 |
"confused": {
|
| 116 |
-
"name": "285 Hz - Returning to Source",
|
| 117 |
"frequency": 285,
|
| 118 |
-
"
|
| 119 |
-
|
| 120 |
-
"
|
| 121 |
-
"
|
| 122 |
-
"
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
}
|
| 125 |
}
|
| 126 |
|
| 127 |
-
#
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
| 165 |
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 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 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
)
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|