Shunya / app.py
Sample-90's picture
Update app.py
d7d12bb verified
Raw History Blame Contribute Delete
3.19 kB
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()