Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import yaml
|
| 3 |
+
import difflib
|
| 4 |
+
import os
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
| 7 |
+
# 🔍 Emotion detection
|
| 8 |
+
def detect_emotion(text):
|
| 9 |
+
emotions = {
|
| 10 |
+
'happy': ['আনন্দ', 'খুশি', 'সুখ'],
|
| 11 |
+
'sad': ['দুঃখ', 'কষ্ট', 'বেদনা'],
|
| 12 |
+
'angry': ['রাগ', 'ক্ষোভ'],
|
| 13 |
+
'love': ['ভালোবাসা', 'প্রেম'],
|
| 14 |
+
}
|
| 15 |
+
for tag, words in emotions.items():
|
| 16 |
+
for word in words:
|
| 17 |
+
if word in text:
|
| 18 |
+
return tag
|
| 19 |
+
return 'neutral'
|
| 20 |
+
|
| 21 |
+
# 😊 Emoji mapping
|
| 22 |
+
def emotion_to_emoji(emotion):
|
| 23 |
+
return {
|
| 24 |
+
'happy': '😊',
|
| 25 |
+
'sad': '😢',
|
| 26 |
+
'angry': '😠',
|
| 27 |
+
'love': '❤️',
|
| 28 |
+
'neutral': '😐'
|
| 29 |
+
}.get(emotion, '😐')
|
| 30 |
+
|
| 31 |
+
# 🏷️ Fuzzy tag matcher
|
| 32 |
+
def fuzzy_tag(text, known_tags):
|
| 33 |
+
matches = difflib.get_close_matches(text, known_tags, n=1, cutoff=0.6)
|
| 34 |
+
return matches[0] if matches else 'unknown'
|
| 35 |
+
|
| 36 |
+
# 🧠 YAML builder
|
| 37 |
+
def build_yaml(text):
|
| 38 |
+
emotion = detect_emotion(text)
|
| 39 |
+
emoji = emotion_to_emoji(emotion)
|
| 40 |
+
tag = fuzzy_tag(text, ['ভালোবাসা', 'আনন্দ', 'দুঃখ', 'রাগ', 'শান্তি', 'উৎসব'])
|
| 41 |
+
|
| 42 |
+
return {
|
| 43 |
+
'text': text,
|
| 44 |
+
'emotion': emotion,
|
| 45 |
+
'emoji': emoji,
|
| 46 |
+
'tag': tag,
|
| 47 |
+
'timestamp': datetime.now().isoformat()
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
# 💾 Save to SD card (Android-friendly)
|
| 51 |
+
def save_to_sd(yaml_data):
|
| 52 |
+
sd_path = '/storage/emulated/0/Bonolota_YAML/'
|
| 53 |
+
os.makedirs(sd_path, exist_ok=True)
|
| 54 |
+
filename = f"bonolota_{datetime.now().strftime('%Y%m%d_%H%M%S')}.yaml"
|
| 55 |
+
file_path = os.path.join(sd_path, filename)
|
| 56 |
+
with open(file_path, 'w', encoding='utf-8') as f:
|
| 57 |
+
yaml.dump(yaml_data, f, allow_unicode=True)
|
| 58 |
+
return file_path
|
| 59 |
+
|
| 60 |
+
# 🔄 Main processor
|
| 61 |
+
def process_script(text):
|
| 62 |
+
if not text.strip():
|
| 63 |
+
return "⚠️ Please enter some text.", ""
|
| 64 |
+
yaml_data = build_yaml(text)
|
| 65 |
+
preview = yaml.dump(yaml_data, allow_unicode=True)
|
| 66 |
+
file_path = save_to_sd(yaml_data)
|
| 67 |
+
return f"✅ YAML saved to:\n{file_path}", preview
|
| 68 |
+
|
| 69 |
+
# 🎨 Gradio UI
|
| 70 |
+
with gr.Blocks() as app:
|
| 71 |
+
gr.Markdown("## ✍️ Emotion-aware YAML Generator (Bonolota Style)")
|
| 72 |
+
gr.Markdown("Paste Bengali script below. It will auto-detect emotion, emoji, tag, and save YAML to SD card.")
|
| 73 |
+
|
| 74 |
+
with gr.Row():
|
| 75 |
+
input_text = gr.Textbox(lines=6, label="Your Script")
|
| 76 |
+
output_yaml = gr.Textbox(lines=10, label="YAML Preview")
|
| 77 |
+
|
| 78 |
+
save_path = gr.Textbox(label="Save Status")
|
| 79 |
+
|
| 80 |
+
generate_btn = gr.Button("🪄 Generate YAML & Save")
|
| 81 |
+
|
| 82 |
+
generate_btn.click(fn=process_script, inputs=[input_text], outputs=[save_path, output_yaml])
|
| 83 |
+
|
| 84 |
+
app.launch()
|