File size: 3,189 Bytes
a6d6e0d d7d12bb a6d6e0d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
import gradio as gr
from gtts import gTTS
import os
from datetime import datetime
import tempfile
from transformers import pipeline
# Emotion detection model
classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base")
# Chakra data (simplified example; extend as needed)
chakra_data = {
"Root": {
"Emotion": ["fear", "anxiety"],
"Purpose": "Grounding, survival",
"Why_Imbalanced": "Feeling unsafe or insecure",
"How_to_Balance": "Practice grounding, connect with nature",
"Affirmation": "I am safe and secure."
},
"Heart": {
"Emotion": ["sadness", "grief"],
"Purpose": "Love, compassion",
"Why_Imbalanced": "Holding on to past pain or resentment",
"How_to_Balance": "Practice forgiveness and self-love",
"Affirmation": "I am open to love and kindness."
},
"Throat": {
"Emotion": ["frustration"],
"Purpose": "Communication and expression",
"Why_Imbalanced": "Fear of speaking out or not being heard",
"How_to_Balance": "Speak your truth and express yourself",
"Affirmation": "I speak my truth with clarity and courage."
}
}
# Detect emotion
def detect_emotion_nlp(text):
result = classifier(text)
return result[0]['label'].lower()
# Analyze input
def analyze_from_text_nlp(journal_text):
detected_emotion = detect_emotion_nlp(journal_text)
for chakra, data in chakra_data.items():
if detected_emotion in [e.lower() for e in data["Emotion"]]:
return (
f"### Chakra Affected: {chakra}",
f"**Purpose**: {data['Purpose']}",
f"**Why Imbalanced**: {data['Why_Imbalanced']}",**How to Balance**: {data['How_to_Balance']}",
data["Affirmation"]
)
return ("No chakra affected", "", "", "")
# Play affirmation
def play_affirmation_audio(affirmation_text):
tts = gTTS(text=affirmation_text, lang='en')
tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
tts.save(tmp_file.name)
return tmp_file.name
# Gradio UI
with gr.Blocks() as demo:
gr.Markdown("## Shunya - Chakra Emotion Analyzer")
with gr.Tab("Analyze"):
journal_input = gr.Textbox(label="Enter your journal or emotion")
analyze_btn = gr.Button("Analyze Emotion")
chakra_output = gr.Markdown()
purpose_output = gr.Markdown()
balance_output = gr.Markdown()
affirmation_output = gr.Textbox(label="Affirmation", interactive=False)
analyze_btn.click(
analyze_from_text_nlp,
inputs=journal_input,
outputs=[chakra_output, purpose_output, balance_output, affirmation_output]
)
with gr.Tab("Affirmation"):
gr.Markdown("## Listen to Your Affirmation")
affirmation_text = gr.Textbox(label="Affirmation", interactive=False)
play_audio_btn = gr.Button("Play Affirmation")
affirmation_audio = gr.Audio(label="Audio", interactive=False, type="filepath")
play_audio_btn.click(play_affirmation_audio, inputs=affirmation_text, outputs=affirmation_audio)
demo.launch()
|