File size: 9,773 Bytes
f7892e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283

"""Data models and project management for ScriptVoice."""

import json
import os
from datetime import datetime
from typing import Dict, Any, Tuple, Optional, List
from config import PROJECTS_FILE

# ... keep existing code (Story Intelligence Data Models: Character, WorldElement, Scene, Chapter, Story classes)

def load_projects() -> Dict[str, Any]:
    """Load projects from JSON file."""
    if os.path.exists(PROJECTS_FILE):
        with open(PROJECTS_FILE, 'r') as f:
            data = json.load(f)
            # Ensure new story intelligence fields exist
            if "stories" not in data:
                data["stories"] = {}
            if "characters" not in data:
                data["characters"] = {}
            if "world_elements" not in data:
                data["world_elements"] = {}
            return data
    return {
        "projects": {
            "1": {
                "id": "1",
                "name": "Sample Script",
                "content": "Welcome to ScriptVoice! This is your first script. Start editing to create amazing voice content.",
                "notes": "This is a sample note for your script.",
                "created_at": datetime.now().isoformat(),
                "word_count": 0
            }
        },
        "stories": {},
        "characters": {},
        "world_elements": {},
        "settings": {
            "dyslexic_mode": False,
            "voice_speed": 1.0,
            "voice_volume": 1.0
        }
    }


def save_projects(data: Dict[str, Any]) -> None:
    """Save projects to JSON file."""
    with open(PROJECTS_FILE, 'w') as f:
        json.dump(data, f, indent=2)


# ... keep existing code (get_word_count, update_word_count functions)

def create_new_project(name: str) -> Tuple[str, Optional[Any]]:
    """Create a new project."""
    if not name.strip():
        return '<div class="status-error">❌ Please enter a project name</div>', None
    
    data = load_projects()
    new_id = str(len(data["projects"]) + 1)
    
    data["projects"][new_id] = {
        "id": new_id,
        "name": name.strip(),
        "content": "",
        "notes": "",
        "created_at": datetime.now().isoformat(),
        "word_count": 0
    }
    
    save_projects(data)
    
    # Return updated project choices and select the new project
    import gradio as gr
    choices = [(proj["name"], proj_id) for proj_id, proj in data["projects"].items()]
    return f'<div class="status-success">βœ… Project "{name}" created successfully!</div>', gr.update(choices=choices, value=new_id)


def load_project(project_id: str) -> Tuple[str, str, str]:
    """Load a specific project."""
    if not project_id:
        return "", "", '<div class="word-count-highlight">πŸ“Š Word Count: 0</div>'
    
    data = load_projects()
    if project_id in data["projects"]:
        project = data["projects"][project_id]
        word_count = get_word_count(project["content"])
        return project["content"], project["notes"], f'<div class="word-count-highlight">πŸ“Š Word Count: {word_count}</div>'
    
    return "", "", '<div class="word-count-highlight">πŸ“Š Word Count: 0</div>'


def save_script_content(project_id: str, content: str, notes: str) -> str:
    """Save script content and notes."""
    if not project_id:
        return '<div class="status-error">❌ No project selected</div>'
    
    data = load_projects()
    if project_id in data["projects"]:
        data["projects"][project_id]["content"] = content
        data["projects"][project_id]["notes"] = notes
        data["projects"][project_id]["word_count"] = get_word_count(content)
        save_projects(data)
        
        # Update knowledge base
        try:
            from enhancement_services import update_knowledge_base
            project_name = data["projects"][project_id]["name"]
            update_knowledge_base("script", project_id, project_name, content)
        except ImportError:
            pass  # RAG services not available yet
        
        return '<div class="status-success">βœ… Saved successfully</div>'
    
    return '<div class="status-error">❌ Error saving</div>'


# Story Management Functions with RAG Integration
def create_story(title: str, description: str = "") -> Tuple[str, Any]:
    """Create a new story."""
    if not title.strip():
        return '<div class="status-error">❌ Please enter a story title</div>', None
    
    data = load_projects()
    new_id = str(len(data["stories"]) + 1)
    
    story_data = {
        "id": new_id,
        "title": title.strip(),
        "description": description,
        "content": "",
        "tags": [],
        "characters": [],
        "world_elements": [],
        "chapters": [],
        "created_at": datetime.now().isoformat(),
        "updated_at": datetime.now().isoformat()
    }
    
    data["stories"][new_id] = story_data
    save_projects(data)
    
    # Update knowledge base
    try:
        from enhancement_services import update_knowledge_base
        content = f"{title}\n\n{description}"
        update_knowledge_base("story", new_id, title, content)
    except ImportError:
        pass  # RAG services not available yet
    
    import gradio as gr
    choices = [(story["title"], story_id) for story_id, story in data["stories"].items()]
    return f'<div class="status-success">βœ… Story "{title}" created successfully!</div>', gr.update(choices=choices, value=new_id)


def create_character(name: str, description: str = "") -> Tuple[str, Any]:
    """Create a new character."""
    if not name.strip():
        return '<div class="status-error">❌ Please enter a character name</div>', None
    
    data = load_projects()
    new_id = str(len(data["characters"]) + 1)
    
    character_data = {
        "id": new_id,
        "name": name.strip(),
        "description": description,
        "traits": [],
        "relationships": {},
        "notes": "",
        "created_at": datetime.now().isoformat(),
        "updated_at": datetime.now().isoformat()
    }
    
    data["characters"][new_id] = character_data
    save_projects(data)
    
    # Update knowledge base
    try:
        from enhancement_services import update_knowledge_base
        content = f"{name}\n\n{description}"
        update_knowledge_base("character", new_id, name, content)
    except ImportError:
        pass  # RAG services not available yet
    
    import gradio as gr
    choices = [(char["name"], char_id) for char_id, char in data["characters"].items()]
    return f'<div class="status-success">βœ… Character "{name}" created successfully!</div>', gr.update(choices=choices, value=new_id)


def create_world_element(name: str, element_type: str, description: str = "") -> Tuple[str, Any]:
    """Create a new world element."""
    if not name.strip():
        return '<div class="status-error">❌ Please enter an element name</div>', None
    
    data = load_projects()
    new_id = str(len(data["world_elements"]) + 1)
    
    element_data = {
        "id": new_id,
        "name": name.strip(),
        "type": element_type,
        "description": description,
        "tags": [],
        "notes": "",
        "created_at": datetime.now().isoformat(),
        "updated_at": datetime.now().isoformat()
    }
    
    data["world_elements"][new_id] = element_data
    save_projects(data)
    
    # Update knowledge base
    try:
        from enhancement_services import update_knowledge_base
        content = f"{name} ({element_type})\n\n{description}"
        update_knowledge_base("world_element", new_id, name, content)
    except ImportError:
        pass  # RAG services not available yet
    
    import gradio as gr
    choices = [(elem["name"], elem_id) for elem_id, elem in data["world_elements"].items()]
    return f'<div class="status-success">βœ… World element "{name}" created successfully!</div>', gr.update(choices=choices, value=new_id)


# ... keep existing code (get_all_stories, get_all_characters, get_all_world_elements, search_content functions)

def get_word_count(text: str) -> int:
    """Count words in text."""
    if not text:
        return 0
    return len(text.split())


def update_word_count(text: str) -> str:
    """Update word count display with gold highlighting."""
    count = get_word_count(text)
    return f'<div class="word-count-highlight">πŸ“Š Word Count: {count}</div>'


def get_all_stories() -> List[Dict]:
    """Get all stories."""
    data = load_projects()
    return list(data["stories"].values())


def get_all_characters() -> List[Dict]:
    """Get all characters."""
    data = load_projects()
    return list(data["characters"].values())


def get_all_world_elements() -> List[Dict]:
    """Get all world elements."""
    data = load_projects()
    return list(data["world_elements"].values())


def search_content(query: str) -> Dict[str, List[Dict]]:
    """Search across stories, characters, and world elements."""
    data = load_projects()
    query_lower = query.lower()
    
    stories = [story for story in data["stories"].values() 
               if query_lower in story["title"].lower() or 
                  query_lower in story["description"].lower() or
                  query_lower in story["content"].lower()]
    
    characters = [char for char in data["characters"].values()
                  if query_lower in char["name"].lower() or
                     query_lower in char["description"].lower()]
    
    world_elements = [elem for elem in data["world_elements"].values()
                      if query_lower in elem["name"].lower() or
                         query_lower in elem["description"].lower()]
    
    return {
        "stories": stories,
        "characters": characters,
        "world_elements": world_elements
    }