File size: 2,696 Bytes
25d091e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()