File size: 10,410 Bytes
4edf13a
ca73bad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4edf13a
ca73bad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4edf13a
ca73bad
 
4edf13a
ca73bad
4edf13a
ca73bad
4edf13a
ca73bad
 
 
4edf13a
ca73bad
4edf13a
ca73bad
 
4edf13a
ca73bad
 
 
4edf13a
ca73bad
 
 
 
 
 
 
 
 
 
 
4edf13a
ca73bad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4edf13a
ca73bad
 
 
 
4edf13a
ca73bad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4edf13a
ca73bad
4edf13a
ca73bad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4edf13a
ca73bad
 
 
 
 
 
 
 
 
 
 
4edf13a
 
ca73bad
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import gradio as gr
import requests
import json
import re
from typing import List, Dict, Any
import os

# Hugging Face configuration
HF_TOKEN = os.getenv("HUGGING_FACE_API_TOKEN", "")
HF_API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"

def parse_script(script_text: str) -> Dict[str, Any]:
    """Parse script text and extract scenes and characters"""
    lines = script_text.strip().split('\n')
    scenes = []
    characters = set()
    current_scene = None
    
    for line in lines:
        line = line.strip()
        if not line:
            continue
            
        # Scene headers (INT./EXT.)
        if line.upper().startswith(('INT.', 'EXT.', 'SCENE')):
            if current_scene:
                scenes.append(current_scene)
            current_scene = {
                'location': line,
                'dialogue': [],
                'action': []
            }
        
        # Character dialogue (ALL CAPS followed by dialogue)
        elif line.isupper() and len(line.split()) <= 3 and current_scene:
            characters.add(line)
            current_scene['dialogue'].append({'character': line, 'lines': []})
        
        # Dialogue lines
        elif current_scene and current_scene['dialogue'] and not line.isupper():
            current_scene['dialogue'][-1]['lines'].append(line)
        
        # Action lines
        elif current_scene and not line.isupper():
            current_scene['action'].append(line)
    
    if current_scene:
        scenes.append(current_scene)
    
    return {
        'scenes': scenes,
        'characters': list(characters),
        'total_scenes': len(scenes)
    }

def generate_shot_list(script_data: Dict[str, Any]) -> List[Dict[str, Any]]:
    """Generate shot list from parsed script"""
    shots = []
    shot_id = 1
    
    for scene_idx, scene in enumerate(script_data['scenes']):
        # Establishing shot
        shots.append({
            'id': shot_id,
            'type': 'Establishing Shot',
            'description': f"Wide shot of {scene['location']}",
            'scene': scene_idx + 1,
            'location': scene['location']
        })
        shot_id += 1
        
        # Character shots
        dialogue_chars = set()
        for dialogue in scene['dialogue']:
            char = dialogue['character']
            if char not in dialogue_chars:
                shots.append({
                    'id': shot_id,
                    'type': 'Medium Shot',
                    'description': f"Medium shot of {char}",
                    'scene': scene_idx + 1,
                    'character': char,
                    'location': scene['location']
                })
                dialogue_chars.add(char)
                shot_id += 1
        
        # Action shots
        for action in scene['action']:
            if len(action) > 20:  # Only significant action lines
                shots.append({
                    'id': shot_id,
                    'type': 'Action Shot',
                    'description': action[:100] + "..." if len(action) > 100 else action,
                    'scene': scene_idx + 1,
                    'location': scene['location']
                })
                shot_id += 1
    
    return shots

def generate_image(prompt: str) -> str:
    """Generate image using Hugging Face API"""
    if not HF_TOKEN:
        return "https://via.placeholder.com/512x512?text=No+API+Key"
    
    headers = {
        "Authorization": f"Bearer {HF_TOKEN}",
        "Content-Type": "application/json"
    }
    
    payload = {
        "inputs": f"{prompt}, cinematic, professional, high quality"
    }
    
    try:
        response = requests.post(HF_API_URL, headers=headers, json=payload, timeout=30)
        if response.status_code == 200:
            # Save image and return path
            import base64
            image_data = response.content
            image_b64 = base64.b64encode(image_data).decode()
            return f"data:image/png;base64,{image_b64}"
        else:
            return f"https://via.placeholder.com/512x512?text=API+Error+{response.status_code}"
    except Exception as e:
        return f"https://via.placeholder.com/512x512?text=Error"

def process_script(script_text: str, generate_images: bool = True):
    """Main function to process script and generate shot list"""
    if not script_text.strip():
        return "Please enter a script.", "", ""
    
    # Parse script
    script_data = parse_script(script_text)
    
    # Generate shot list
    shots = generate_shot_list(script_data)
    
    # Create summary
    summary = f"""
## Script Analysis Summary
- **Total Scenes:** {script_data['total_scenes']}
- **Characters:** {', '.join(script_data['characters'])}
- **Generated Shots:** {len(shots)}
"""
    
    # Create shot list display
    shot_list_html = "<div style='max-height: 400px; overflow-y: auto;'>"
    
    for shot in shots:
        # Generate image if requested
        image_html = ""
        if generate_images:
            image_url = generate_image(shot['description'])
            image_html = f'<img src="{image_url}" style="width: 200px; height: 150px; object-fit: cover; border-radius: 8px;" />'
        
        shot_list_html += f"""
        <div style='border: 1px solid #ddd; margin: 10px 0; padding: 15px; border-radius: 8px; background: #f9f9f9;'>
            <div style='display: flex; gap: 15px; align-items: flex-start;'>
                <div style='flex: 1;'>
                    <h3 style='margin: 0 0 10px 0; color: #333;'>Shot {shot['id']}: {shot['type']}</h3>
                    <p style='margin: 5px 0;'><strong>Scene:</strong> {shot['scene']}</p>
                    <p style='margin: 5px 0;'><strong>Description:</strong> {shot['description']}</p>
                    {f"<p style='margin: 5px 0;'><strong>Character:</strong> {shot.get('character', 'N/A')}</p>" if shot.get('character') else ""}
                    <p style='margin: 5px 0;'><strong>Location:</strong> {shot.get('location', 'N/A')}</p>
                </div>
                <div style='flex-shrink: 0;'>
                    {image_html}
                </div>
            </div>
        </div>
        """
    
    shot_list_html += "</div>"
    
    return summary, shot_list_html, f"Generated {len(shots)} shots successfully!"

# Sample script for demo
SAMPLE_SCRIPT = """INT. COFFEE SHOP - DAY

A bustling coffee shop filled with the morning crowd. Steam rises from espresso machines.

SARAH sits at a corner table, typing furiously on her laptop. She glances at her watch nervously.

SARAH
(muttering to herself)
Come on, come on... where is he?

The door chimes as MIKE enters, scanning the room. He spots Sarah and approaches.

MIKE
Sorry I'm late! Traffic was insane.

SARAH
(relieved)
Thank god you're here. I've been going crazy waiting.

Mike sits down across from her.

MIKE
So, what's this big emergency about?

Sarah closes her laptop and leans in conspiratorially.

SARAH
I found something. Something that could change everything.

EXT. CITY STREET - DAY

Sarah and Mike walk quickly down a busy sidewalk, weaving through pedestrians.

MIKE
Are you sure about this?

SARAH
I've never been more sure of anything in my life.

They stop at a red light, looking around nervously."""

# Create Gradio interface
with gr.Blocks(title="Script to Shots - AI Storyboard Generator") as demo:
    gr.Markdown("""
    # 🎬 Script to Shots - AI Storyboard Generator
    
    Transform your scripts into visual shot lists with AI-generated reference images!
    
    **How it works:**
    1. Paste your script in the text area below
    2. Choose whether to generate AI images
    3. Get an automated shot list with visual references
    """)
    
    with gr.Row():
        with gr.Column(scale=1):
            script_input = gr.Textbox(
                label="Script Text",
                placeholder="Paste your script here...",
                lines=15,
                value=SAMPLE_SCRIPT
            )
            
            with gr.Row():
                generate_images_checkbox = gr.Checkbox(
                    label="Generate AI Images", 
                    value=True,
                    info="Generate visual references (requires API key)"
                )
                process_btn = gr.Button("Generate Shot List", variant="primary")
        
        with gr.Column(scale=1):
            summary_output = gr.Markdown(label="Analysis Summary")
            status_output = gr.Textbox(label="Status", interactive=False)
    
    with gr.Row():
        shot_list_output = gr.HTML(label="Generated Shot List")
    
    # Example scripts
    gr.Markdown("### 📝 Example Scripts")
    with gr.Row():
        example_btn1 = gr.Button("Coffee Shop Scene")
        example_btn2 = gr.Button("Action Sequence")
        example_btn3 = gr.Button("Clear Script")
    
    # Event handlers
    process_btn.click(
        fn=process_script,
        inputs=[script_input, generate_images_checkbox],
        outputs=[summary_output, shot_list_output, status_output]
    )
    
    example_btn1.click(
        fn=lambda: SAMPLE_SCRIPT,
        outputs=script_input
    )
    
    example_btn2.click(
        fn=lambda: """EXT. ROOFTOP - NIGHT

Rain pours down on the city skyline. Lightning illuminates the darkness.

ALEX crouches behind an air conditioning unit, breathing heavily.

ALEX
(into radio)
I'm in position. Do you see them?

VOICE (V.O.)
(filtered)
Two guards on the east side. Move now!

Alex sprints across the rooftop, water splashing with each step.

Suddenly, a spotlight sweeps across the roof. Alex dives behind a chimney just in time.

GUARD
(shouting)
There! On the roof!

Gunshots ring out. Alex pulls out a grappling hook and fires it toward the next building.""",
        outputs=script_input
    )
    
    example_btn3.click(
        fn=lambda: "",
        outputs=script_input
    )
    
    gr.Markdown("""
    ### 🔧 Setup Instructions
    
    To enable AI image generation, you need a Hugging Face API token:
    1. Get a free token at [huggingface.co/settings/tokens](https://huggingface.co/settings/tokens)
    2. Set it as an environment variable: `HUGGING_FACE_API_TOKEN`
    3. Restart the application
    
    **Note:** Without an API token, placeholder images will be shown instead.
    """)

if __name__ == "__main__":
    demo.launch()