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 
 
 
 
 
a613fd9
26ed6a6
 
 
 
 
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import gradio as gr
from huggingface_hub import InferenceClient
import os
import json
import base64
from PIL import Image
import io
import time
import tempfile
import uuid

# Access token from environment variable
ACCESS_TOKEN = os.getenv("HF_TOKEN")
print("Access token loaded.")

def generate_video(
    prompt,
    negative_prompt,
    num_frames,
    fps,
    width,
    height,
    num_inference_steps,
    guidance_scale,
    motion_bucket_id,
    seed,
    provider,
    custom_api_key,  
    custom_model,    
    model_search_term,
    selected_model
):
    """Generate a video based on the provided parameters"""
    print(f"Received prompt: {prompt}")
    print(f"Negative prompt: {negative_prompt}")
    print(f"Num frames: {num_frames}, FPS: {fps}")
    print(f"Width: {width}, Height: {height}")
    print(f"Steps: {num_inference_steps}, Guidance Scale: {guidance_scale}")
    print(f"Motion Bucket ID: {motion_bucket_id}, Seed: {seed}")
    print(f"Selected provider: {provider}")         
    print(f"Custom API Key provided: {bool(custom_api_key.strip())}")
    print(f"Selected model (custom_model): {custom_model}")  
    print(f"Model search term: {model_search_term}")
    print(f"Selected model from radio: {selected_model}")

    # Determine which token to use - custom API key if provided, otherwise the ACCESS_TOKEN
    token_to_use = custom_api_key if custom_api_key.strip() != "" else ACCESS_TOKEN
    
    # Log which token source we're using (without printing the actual token)
    if custom_api_key.strip() != "":
        print("USING CUSTOM API KEY: BYOK token provided by user is being used for authentication")
    else:
        print("USING DEFAULT API KEY: Environment variable HF_TOKEN is being used for authentication")
    
    # Initialize the Inference Client with the provider and appropriate token
    client = InferenceClient(token=token_to_use, provider=provider)
    print(f"Hugging Face Inference Client initialized with {provider} provider.")

    # Convert seed to None if -1 (meaning random)
    if seed == -1:
        seed = None
    else:
        # Ensure seed is an integer
        seed = int(seed)

    # Determine which model to use, prioritizing custom_model if provided
    model_to_use = custom_model.strip() if custom_model.strip() != "" else selected_model
    print(f"Model selected for inference: {model_to_use}")

    # Create a unique ID for this generation
    generation_id = uuid.uuid4().hex[:8]
    print(f"Generation ID: {generation_id}")
    
    # Define supported parameters for each provider
    provider_param_support = {
        "hf-inference": {
            "supported": ["prompt", "model", "negative_prompt", "num_frames", "num_inference_steps", "guidance_scale", "seed"],
            "extra_info": "HF Inference doesn't support 'fps', 'width', 'height', or 'motion_bucket_id' parameters"
        },
        "fal-ai": {
            "supported": ["prompt", "model", "negative_prompt", "num_frames", "num_inference_steps", "guidance_scale", "seed"],
            "extra_info": "Fal-AI doesn't support 'fps', 'width', 'height', or 'motion_bucket_id' parameters"
        },
        "novita": {
            "supported": ["prompt", "model", "negative_prompt", "num_frames", "num_inference_steps", "guidance_scale", "seed", "fps", "width", "height"],
            "extra_info": "Novita may not support 'motion_bucket_id' parameter"
        },
        "replicate": {
            "supported": ["prompt", "model", "negative_prompt", "num_frames", "num_inference_steps", "guidance_scale", "seed", "fps", "width", "height"],
            "extra_info": "Replicate parameters vary by specific model"
        }
    }
    
    # Get supported parameters for the current provider
    supported_params = provider_param_support.get(provider, {}).get("supported", [])
    provider_info = provider_param_support.get(provider, {}).get("extra_info", "No specific information available")
    
    print(f"Provider info: {provider_info}")
    print(f"Supported parameters: {supported_params}")
    
    # Create a parameters dictionary with only supported parameters
    parameters = {}
    
    if "negative_prompt" in supported_params:
        parameters["negative_prompt"] = negative_prompt
    
    if "num_frames" in supported_params:
        parameters["num_frames"] = num_frames
    
    if "num_inference_steps" in supported_params:
        parameters["num_inference_steps"] = num_inference_steps
    
    if "guidance_scale" in supported_params:
        parameters["guidance_scale"] = guidance_scale
    
    if "seed" in supported_params and seed is not None:
        parameters["seed"] = seed
    
    if "fps" in supported_params:
        parameters["fps"] = fps
    
    if "width" in supported_params:
        parameters["width"] = width
    
    if "height" in supported_params:
        parameters["height"] = height
    
    if "motion_bucket_id" in supported_params:
        parameters["motion_bucket_id"] = motion_bucket_id

    # Now that we have a clean parameter set, handle provider-specific logic
    print(f"Final parameters for {provider}: {parameters}")
    
    # For Replicate provider - uses post method
    if provider == "replicate":
        print("Using Replicate provider, using post method...")
        try:
            response = client.post(
                model=model_to_use,
                input={
                    "prompt": prompt,
                    **parameters
                },
            )
            
            # Replicate typically returns a URL to the generated video
            if isinstance(response, dict) and "output" in response:
                video_url = response["output"]
                print(f"Video generated, URL: {video_url}")
                return video_url
            else:
                return str(response)
                
        except Exception as e:
            print(f"Error during Replicate video generation: {e}")
            return f"Error: {str(e)}"
    
    # For all other providers, use the standard text_to_video method
    try:
        print(f"Sending request to {provider} provider with model {model_to_use}.")
        
        # Use the text_to_video method of the InferenceClient with only supported parameters
        video_data = client.text_to_video(
            prompt=prompt,
            model=model_to_use,
            **parameters
        )
        
        # Save the video to a temporary file
        temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
        temp_file.write(video_data)
        video_path = temp_file.name
        temp_file.close()
        
        print(f"Video saved to temporary file: {video_path}")
        return video_path
        
    except Exception as e:
        print(f"Error during video generation: {e}")
        return f"Error: {str(e)}"

# Function to validate provider selection based on BYOK
def validate_provider(api_key, provider):
    # If no custom API key is provided, only "hf-inference" can be used
    if not api_key.strip() and provider != "hf-inference":
        return gr.update(value="hf-inference")
    return gr.update(value=provider)

# Define the GRADIO UI
with gr.Blocks(theme="Nymbo/Nymbo_Theme") as demo:
    
    with gr.Row():
        with gr.Column(scale=2):
            # Main video output area
            video_output = gr.Video(label="Generated Video", height=400)
            
            # Basic input components
            prompt_box = gr.Textbox(
                value="A beautiful sunset over a calm ocean", 
                placeholder="Enter a prompt for your video", 
                label="Prompt",
                lines=3
            )
            
            # Generate button
            generate_button = gr.Button("🎬 Generate Video", variant="primary")
            
    
    # Advanced settings in an accordion
    with gr.Accordion("Advanced Settings", open=False):
        with gr.Row():
            with gr.Column():
                negative_prompt = gr.Textbox(
                    label="Negative Prompt",
                    placeholder="What should NOT be in the video",
                    value="poor quality, distortion, blurry, low resolution, grainy",
                    lines=2
                )
                
                with gr.Row():
                    width = gr.Slider(
                        minimum=256,
                        maximum=1024,
                        value=512,
                        step=64,
                        label="Width"
                    )
                    
                    height = gr.Slider(
                        minimum=256,
                        maximum=1024,
                        value=512,
                        step=64,
                        label="Height"
                    )
                
                with gr.Row():
                    num_frames = gr.Slider(
                        minimum=8,
                        maximum=64,
                        value=16,
                        step=1,
                        label="Number of Frames"
                    )
                    
                    fps = gr.Slider(
                        minimum=1,
                        maximum=30,
                        value=8,
                        step=1,
                        label="Frames Per Second"
                    )
                    
                # Adding the sliders from the right column to the left column
                with gr.Row():
                    num_inference_steps = gr.Slider(
                        minimum=1,
                        maximum=100,
                        value=25,
                        step=1,
                        label="Inference Steps"
                    )
                    
                    guidance_scale = gr.Slider(
                        minimum=1.0,
                        maximum=20.0,
                        value=7.5,
                        step=0.5,
                        label="Guidance Scale"
                    )
                
                with gr.Row():
                    motion_bucket_id = gr.Slider(
                        minimum=1,
                        maximum=255,
                        value=127,
                        step=1,
                        label="Motion Bucket ID (for SVD models)"
                    )
                    
                    seed = gr.Slider(
                        minimum=-1,
                        maximum=2147483647,
                        value=-1,
                        step=1,
                        label="Seed (-1 for random)"
                    )
                    
            with gr.Column():
                # Provider selection
                providers_list = [
                    "hf-inference",  # Default Hugging Face Inference
                    "fal-ai",        # Fal AI provider
                    "novita",        # Novita provider
                    "replicate",     # Replicate provider
                ]
                
                provider_radio = gr.Radio(
                    choices=providers_list,
                    value="hf-inference",
                    label="Inference Provider",
                    info="Select an inference provider. Note: Requires provider-specific API key except for hf-inference"
                )
                
                # BYOK textbox
                byok_textbox = gr.Textbox(
                    value="",
                    label="BYOK (Bring Your Own Key)",
                    info="Enter a provider API key here. When empty, only 'hf-inference' provider can be used.",
                    placeholder="Enter your provider API token",
                    type="password"  # Hide the API key for security
                )
                
                # Model selection components (moved from left column)
                model_search_box = gr.Textbox(
                    label="Filter Models",
                    placeholder="Search for a model...",
                    lines=1
                )
                
                models_list = [
                    "Lightricks/LTX-Video",
                    "Wan-AI/Wan2.1-T2V-14B",
                    "tencent/HunyuanVideo",
                    "Wan-AI/Wan2.1-T2V-1.3B",
                    "genmo/mochi-1-preview",
                    "THUDM/CogVideoX-5b"
                ]
                
                featured_model_radio = gr.Radio(
                    label="Select a model below",
                    choices=models_list,
                    value="Lightricks/LTX-Video",
                    interactive=True
                )
                
                custom_model_box = gr.Textbox(
                    value="",
                    label="Custom Model",
                    info="(Optional) Provide a custom Hugging Face model path. Overrides any selected featured model.",
                    placeholder="damo-vilab/text-to-video-ms-1.7b"
                )
                
                gr.Markdown("[See all available models](https://huggingface.co/models?inference_provider=all&pipeline_tag=text-to-video&sort=trending)")
    
    # Set up the generation click event
    generate_button.click(
        fn=generate_video,
        inputs=[
            prompt_box,
            negative_prompt,
            num_frames,
            fps,
            width,
            height,
            num_inference_steps,
            guidance_scale,
            motion_bucket_id,
            seed,
            provider_radio,
            byok_textbox,
            custom_model_box,
            model_search_box,
            featured_model_radio
        ],
        outputs=video_output
    )
    
    # Connect the model filter to update the radio choices
    def filter_models(search_term):
        print(f"Filtering models with search term: {search_term}")
        filtered = [m for m in models_list if search_term.lower() in m.lower()]
        print(f"Filtered models: {filtered}")
        return gr.update(choices=filtered)
    
    model_search_box.change(
        fn=filter_models,
        inputs=model_search_box,
        outputs=featured_model_radio
    )
    
    # Connect the featured model radio to update the custom model box
    def set_custom_model_from_radio(selected):
        """
        This function will get triggered whenever someone picks a model from the 'Featured Models' radio.
        We will update the Custom Model text box with that selection automatically.
        """
        print(f"Featured model selected: {selected}")
        return selected
    
    featured_model_radio.change(
        fn=set_custom_model_from_radio,
        inputs=featured_model_radio,
        outputs=custom_model_box
    )
    
    # Connect the BYOK textbox to validate provider selection
    byok_textbox.change(
        fn=validate_provider,
        inputs=[byok_textbox, provider_radio],
        outputs=provider_radio
    )
    
    # Also validate provider when the radio changes to ensure consistency
    provider_radio.change(
        fn=validate_provider,
        inputs=[byok_textbox, provider_radio],
        outputs=provider_radio
    )
    


# Launch the app
if __name__ == "__main__":
    print("Launching the demo application.")
    demo.launch(show_api=True)