Spaces:
Sleeping
Sleeping
| import json | |
| import os | |
| # ββ Load raw vocabulary from separate JSON βββββββββββββββββββββββ | |
| def load_vocabulary(path="data/vocabulary.json"): | |
| with open(path, "r", encoding="utf-8") as f: | |
| return json.load(f) | |
| # ββ Prompt template ββββββββββββββββββββββββββββββββββββββββββββββ | |
| INSTRUCTION = ( | |
| "You are Marathi Mitra, a friendly Marathi teacher for kids. " | |
| "When given an English word, teach it in Marathi with the word " | |
| "in Devanagari script, pronunciation, a simple story sentence, " | |
| "and a fun fact. Always be encouraging and kid-friendly." | |
| ) | |
| def format_output(item): | |
| """Format the structured output the model should produce.""" | |
| return f"""π **{item['word'].upper()}** in Marathi is **{item['marathi']}** | |
| π’ **How to say it:** {item['pronunciation']} | |
| π **Example sentence:** | |
| {item['sentence']} | |
| *({item['translation']})* | |
| π **Fun Fact:** {item['fun_fact']}""" | |
| def format_training_example(item): | |
| """Format one vocabulary item into a training example.""" | |
| input_text = f"Teach me the Marathi word for: {item['word']}" | |
| output_text = format_output(item) | |
| return { | |
| "word": item["word"], | |
| "marathi": item["marathi"], | |
| "instruction": INSTRUCTION, | |
| "input": input_text, | |
| "output": output_text, | |
| "text": f"""### Instruction: | |
| {INSTRUCTION} | |
| ### Input: | |
| {input_text} | |
| ### Response: | |
| {output_text}""" | |
| } | |
| # ββ Build and save dataset βββββββββββββββββββββββββββββββββββββββ | |
| def create_dataset(): | |
| vocab = load_vocabulary() | |
| print(f"Loaded {len(vocab)} words from vocabulary.json") | |
| examples = [format_training_example(item) for item in vocab] | |
| output_path = "data/vocabulary_dataset.json" | |
| with open(output_path, "w", encoding="utf-8") as f: | |
| json.dump(examples, f, ensure_ascii=False, indent=2) | |
| print(f"β Dataset saved β {output_path}") | |
| print(f"β Total examples: {len(examples)}") | |
| print(f"\nSample output:") | |
| print("β" * 50) | |
| print(examples[0]["text"]) | |
| return examples | |
| if __name__ == "__main__": | |
| create_dataset() |