Text_yaml / app.py
Lavlu118557's picture
Create app.py
25d091e verified
import gradio as gr
import yaml
import difflib
import os
from datetime import datetime
# 🔍 Emotion detection
def detect_emotion(text):
emotions = {
'happy': ['আনন্দ', 'খুশি', 'সুখ'],
'sad': ['দুঃখ', 'কষ্ট', 'বেদনা'],
'angry': ['রাগ', 'ক্ষোভ'],
'love': ['ভালোবাসা', 'প্রেম'],
}
for tag, words in emotions.items():
for word in words:
if word in text:
return tag
return 'neutral'
# 😊 Emoji mapping
def emotion_to_emoji(emotion):
return {
'happy': '😊',
'sad': '😢',
'angry': '😠',
'love': '❤️',
'neutral': '😐'
}.get(emotion, '😐')
# 🏷️ Fuzzy tag matcher
def fuzzy_tag(text, known_tags):
matches = difflib.get_close_matches(text, known_tags, n=1, cutoff=0.6)
return matches[0] if matches else 'unknown'
# 🧠 YAML builder
def build_yaml(text):
emotion = detect_emotion(text)
emoji = emotion_to_emoji(emotion)
tag = fuzzy_tag(text, ['ভালোবাসা', 'আনন্দ', 'দুঃখ', 'রাগ', 'শান্তি', 'উৎসব'])
return {
'text': text,
'emotion': emotion,
'emoji': emoji,
'tag': tag,
'timestamp': datetime.now().isoformat()
}
# 💾 Save to SD card (Android-friendly)
def save_to_sd(yaml_data):
sd_path = '/storage/emulated/0/Bonolota_YAML/'
os.makedirs(sd_path, exist_ok=True)
filename = f"bonolota_{datetime.now().strftime('%Y%m%d_%H%M%S')}.yaml"
file_path = os.path.join(sd_path, filename)
with open(file_path, 'w', encoding='utf-8') as f:
yaml.dump(yaml_data, f, allow_unicode=True)
return file_path
# 🔄 Main processor
def process_script(text):
if not text.strip():
return "⚠️ Please enter some text.", ""
yaml_data = build_yaml(text)
preview = yaml.dump(yaml_data, allow_unicode=True)
file_path = save_to_sd(yaml_data)
return f"✅ YAML saved to:\n{file_path}", preview
# 🎨 Gradio UI
with gr.Blocks() as app:
gr.Markdown("## ✍️ Emotion-aware YAML Generator (Bonolota Style)")
gr.Markdown("Paste Bengali script below. It will auto-detect emotion, emoji, tag, and save YAML to SD card.")
with gr.Row():
input_text = gr.Textbox(lines=6, label="Your Script")
output_yaml = gr.Textbox(lines=10, label="YAML Preview")
save_path = gr.Textbox(label="Save Status")
generate_btn = gr.Button("🪄 Generate YAML & Save")
generate_btn.click(fn=process_script, inputs=[input_text], outputs=[save_path, output_yaml])
app.launch()