File size: 2,962 Bytes
6afd58f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
"""Generate emotion-labeled stories for emotion vector extraction.
Uses local Gemma4-31B via Ollama to generate short stories."""

import json
import os
import subprocess
import random
import time

OUT_DIR = os.path.dirname(os.path.abspath(__file__))
OUT_FILE = os.path.join(OUT_DIR, "emotion_stories.jsonl")

EMOTIONS = [
    "happy", "sad", "angry", "afraid", "calm",
    "desperate", "loving", "guilty", "surprised", "nervous",
    "proud", "inspired", "spiteful", "brooding", "playful",
    "anxious", "confused", "disgusted", "lonely", "hopeful",
]

TOPICS = [
    "a student preparing for an exam",
    "a chef cooking a meal for guests",
    "a parent watching their child play",
    "a soldier returning home",
    "an artist finishing a painting",
    "a driver stuck in traffic",
    "a doctor delivering news to a patient",
    "a traveler arriving in a new city",
    "a musician performing on stage",
    "a shopkeeper closing for the day",
]

def generate_story(emotion, topic):
    prompt = f"""Write a short paragraph (4-6 sentences) about {topic}.
The character in the story is feeling {emotion}.
Make the emotion clear through their actions, thoughts, and reactions.
Write in English. Only output the story paragraph, nothing else."""

    result = subprocess.run(
        ["ollama", "run", "gemma4:e4b", prompt],
        capture_output=True, text=True, timeout=60
    )
    return result.stdout.strip()


def main():
    existing = set()
    if os.path.exists(OUT_FILE):
        with open(OUT_FILE, "r") as f:
            for line in f:
                d = json.loads(line)
                existing.add((d["emotion"], d["topic_idx"], d["story_idx"]))
        print(f"Resuming: {len(existing)} stories already done")

    total = len(EMOTIONS) * len(TOPICS) * 5
    done = len(existing)

    with open(OUT_FILE, "a") as f:
        for ei, emotion in enumerate(EMOTIONS):
            for ti, topic in enumerate(TOPICS):
                for si in range(5):
                    key = (emotion, ti, si)
                    if key in existing:
                        continue

                    story = generate_story(emotion, topic)
                    if not story or len(story) < 20:
                        print(f"[SKIP] {emotion}/{topic}/{si} - empty")
                        continue

                    record = {
                        "emotion": emotion,
                        "topic_idx": ti,
                        "topic": topic,
                        "story_idx": si,
                        "text": story,
                    }
                    f.write(json.dumps(record, ensure_ascii=False) + "\n")
                    f.flush()
                    done += 1

                    if done % 10 == 0:
                        print(f"[{done}/{total}] {emotion} / {topic[:30]}...")

    print(f"\nDone. Total stories: {done}")
    print(f"Output: {OUT_FILE}")


if __name__ == "__main__":
    main()