File size: 3,969 Bytes
59e4f9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# MODAL INTEGRATION GUIDE

## Step 1: Install Modal

```bash
pip install modal
```

## Step 2: Set up Modal Account

1. Go to https://modal.com
2. Sign up (free tier available + your $250 hackathon credit)
3. Get your token:
   ```bash
   modal token new
   ```

## Step 3: Deploy Modal Function

```bash
modal deploy modal_video_processing.py
```

This will give you a URL like:
```
https://your-username--aiquoteclipgenerator-process-video-endpoint.modal.run
```

## Step 4: Add to Your Hugging Face Space

Add this environment variable:
```
MODAL_ENDPOINT_URL=your_modal_endpoint_url_here
```

## Step 5: Update app.py

Replace the `create_quote_video_tool` function with this Modal-powered version:

```python
@tool
def create_quote_video_tool(video_url: str, quote_text: str, output_path: str, audio_path: str = None) -> dict:
    """
    Create a final quote video using Modal for fast processing.
    """
    
    try:
        import requests
        import base64
        
        modal_endpoint = os.getenv("MODAL_ENDPOINT_URL")
        
        if not modal_endpoint:
            # Fallback to local processing if Modal not configured
            return create_quote_video_local(video_url, quote_text, output_path, audio_path)
        
        print("πŸš€ Processing on Modal (fast!)...")
        
        # Upload audio to temporary storage if provided
        audio_url = None
        if audio_path and os.path.exists(audio_path):
            # For now, we'll skip audio in Modal version
            # In production, upload audio to S3/GCS and pass URL
            pass
        
        # Call Modal endpoint
        response = requests.post(
            modal_endpoint,
            json={
                "video_url": video_url,
                "quote_text": quote_text,
                "audio_url": audio_url
            },
            timeout=120
        )
        
        if response.status_code != 200:
            raise Exception(f"Modal error: {response.text}")
        
        result = response.json()
        
        if not result.get("success"):
            raise Exception(result.get("error", "Unknown error"))
        
        # Decode video bytes
        video_b64 = result["video"]
        video_bytes = base64.b64decode(video_b64)
        
        # Save to output path
        with open(output_path, 'wb') as f:
            f.write(video_bytes)
        
        print(f"βœ… Modal processing complete! {result['size_mb']:.2f}MB")
        
        return {
            "success": True,
            "output_path": output_path,
            "message": f"Video created via Modal ({result['size_mb']:.2f}MB)"
        }
    
    except Exception as e:
        print(f"Modal processing failed: {e}")
        # Fallback to local processing
        return create_quote_video_local(video_url, quote_text, output_path, audio_path)


def create_quote_video_local(video_url: str, quote_text: str, output_path: str, audio_path: str = None) -> dict:
    """
    Fallback local processing (your current implementation)
    """
    # Your existing create_quote_video_tool code here
    pass
```

## Benefits of Modal:

### Speed Comparison:
- **Before (HF Spaces):** 119 seconds
- **After (Modal):** ~15-30 seconds (4-8x faster!)

### Why Modal is Faster:
1. βœ… **4 CPUs** instead of shared CPU on HF Spaces
2. βœ… **4GB RAM** dedicated to your function
3. βœ… **Optimized infrastructure** for video processing
4. βœ… **Fast I/O** for downloading/uploading

### Cost:
- Uses your $250 hackathon credit
- After that: ~$0.01-0.02 per video (very cheap!)

## Testing Modal Function

```python
# Test locally before deploying
python modal_video_processing.py
```

## Monitoring

View logs and metrics at:
https://modal.com/apps

## Hackathon Impact:

βœ… **Much faster** - Better UX
βœ… **Uses sponsor credit** - Shows engagement
βœ… **Professional infrastructure** - Impressive to judges
βœ… **Scalable** - Handles multiple users

This is a HUGE upgrade! πŸš€