Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -201,11 +201,10 @@ HTML_CONTENT = """
|
|
201 |
margin: 15% auto;
|
202 |
padding: 20px;
|
203 |
border: 1px solid #333;
|
204 |
-
width:
|
205 |
max-width: 600px;
|
206 |
border-radius: 10px;
|
207 |
color: #e0e0e0;
|
208 |
-
text-align: center;
|
209 |
}
|
210 |
.close {
|
211 |
color: #aaa;
|
@@ -227,23 +226,7 @@ HTML_CONTENT = """
|
|
227 |
border: 1px solid #555;
|
228 |
color: #e0e0e0;
|
229 |
border-radius: 5px;
|
230 |
-
|
231 |
-
word-break: break-all;
|
232 |
-
}
|
233 |
-
.copy-embed-btn {
|
234 |
-
background-color: #ffffff;
|
235 |
-
color: #121212;
|
236 |
-
border: none;
|
237 |
-
padding: 10px 20px;
|
238 |
-
border-radius: 5px;
|
239 |
-
cursor: pointer;
|
240 |
-
transition: all 0.3s ease;
|
241 |
-
font-weight: 600;
|
242 |
-
font-size: 16px;
|
243 |
-
margin-top: 15px;
|
244 |
-
}
|
245 |
-
.copy-embed-btn:hover {
|
246 |
-
background-color: #f0f0f0;
|
247 |
}
|
248 |
</style>
|
249 |
</head>
|
@@ -273,7 +256,7 @@ HTML_CONTENT = """
|
|
273 |
<h2>Embed Video Link</h2>
|
274 |
<p>Copy the following link to embed the video:</p>
|
275 |
<input type="text" id="embedLink" readonly>
|
276 |
-
<button onclick="copyEmbedLink()" class="
|
277 |
</div>
|
278 |
</div>
|
279 |
|
@@ -438,7 +421,7 @@ HTML_CONTENT = """
|
|
438 |
const copyBtn = document.createElement('button');
|
439 |
copyBtn.textContent = 'Copy Link';
|
440 |
copyBtn.className = 'copy-btn';
|
441 |
-
|
442 |
navigator.clipboard.writeText(window.location.origin + url).then(() => {
|
443 |
alert('Link copied to clipboard!');
|
444 |
});
|
@@ -447,7 +430,7 @@ HTML_CONTENT = """
|
|
447 |
linkContainer.appendChild(link);
|
448 |
linkContainer.appendChild(copyBtn);
|
449 |
|
450 |
-
|
451 |
const embedBtn = document.createElement('button');
|
452 |
embedBtn.textContent = 'Embed Video for Discord';
|
453 |
embedBtn.className = 'embed-btn';
|
@@ -510,11 +493,11 @@ async def handle_video_stream(path: str, request: Request):
|
|
510 |
range_header = request.headers.get('Range')
|
511 |
|
512 |
headers = {'Range': range_header} if range_header else {}
|
513 |
-
|
514 |
-
response = await client.get(original_url, headers=headers)
|
515 |
|
516 |
def generate():
|
517 |
-
|
|
|
518 |
|
519 |
headers = dict(response.headers)
|
520 |
headers['Access-Control-Allow-Origin'] = '*'
|
@@ -567,16 +550,19 @@ async def embed_video(url: str, thumbnail: str):
|
|
567 |
return HTMLResponse(content=html)
|
568 |
|
569 |
async def get_cookies() -> Dict[str, str]:
|
570 |
-
|
571 |
-
response =
|
572 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
|
573 |
})
|
574 |
return dict(response.cookies)
|
|
|
|
|
|
|
575 |
|
576 |
async def initiate_upload(cookies: Dict[str, str], filename: str, content_type: str) -> Dict:
|
577 |
url = f'https://replicate.com/api/upload/{filename}?content_type={content_type}'
|
578 |
-
|
579 |
-
response =
|
580 |
'X-CSRFToken': cookies.get('csrftoken'),
|
581 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',
|
582 |
'Referer': 'https://replicate.com/levelsio/neon-tokyo',
|
@@ -594,13 +580,19 @@ async def initiate_upload(cookies: Dict[str, str], filename: str, content_type:
|
|
594 |
print(f'Initiate upload response headers: {response.headers}')
|
595 |
print(f'Response body: {response.text}')
|
596 |
return response.json()
|
|
|
|
|
|
|
597 |
|
598 |
async def upload_file(upload_url: str, file_content: bytes, content_type: str) -> bool:
|
599 |
-
|
600 |
-
response =
|
601 |
print(f'File upload response status: {response.status_code}')
|
602 |
print(f'File upload response headers: {response.headers}')
|
603 |
return response.status_code == 200
|
|
|
|
|
|
|
604 |
|
605 |
async def retry_upload(upload_url: str, file_content: bytes, content_type: str, max_retries: int = 5, delay: int = 1) -> bool:
|
606 |
for attempt in range(1, max_retries + 1):
|
@@ -611,7 +603,7 @@ async def retry_upload(upload_url: str, file_content: bytes, content_type: str,
|
|
611 |
return True
|
612 |
if attempt < max_retries:
|
613 |
print(f'Upload failed, retrying in {delay}s...')
|
614 |
-
|
615 |
delay *= 2 # Exponential backoff
|
616 |
print('Upload failed after all retry attempts')
|
617 |
return False
|
|
|
201 |
margin: 15% auto;
|
202 |
padding: 20px;
|
203 |
border: 1px solid #333;
|
204 |
+
width: 80%;
|
205 |
max-width: 600px;
|
206 |
border-radius: 10px;
|
207 |
color: #e0e0e0;
|
|
|
208 |
}
|
209 |
.close {
|
210 |
color: #aaa;
|
|
|
226 |
border: 1px solid #555;
|
227 |
color: #e0e0e0;
|
228 |
border-radius: 5px;
|
229 |
+
text-align: center;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
230 |
}
|
231 |
</style>
|
232 |
</head>
|
|
|
256 |
<h2>Embed Video Link</h2>
|
257 |
<p>Copy the following link to embed the video:</p>
|
258 |
<input type="text" id="embedLink" readonly>
|
259 |
+
<button onclick="copyEmbedLink()" class="btn" style="margin-top: 10px;">Copy Embed Link</button>
|
260 |
</div>
|
261 |
</div>
|
262 |
|
|
|
421 |
const copyBtn = document.createElement('button');
|
422 |
copyBtn.textContent = 'Copy Link';
|
423 |
copyBtn.className = 'copy-btn';
|
424 |
+
copyBtn.onclick = () => {
|
425 |
navigator.clipboard.writeText(window.location.origin + url).then(() => {
|
426 |
alert('Link copied to clipboard!');
|
427 |
});
|
|
|
430 |
linkContainer.appendChild(link);
|
431 |
linkContainer.appendChild(copyBtn);
|
432 |
|
433 |
+
if (fileName.toLowerCase().endsWith('.mp4')) {
|
434 |
const embedBtn = document.createElement('button');
|
435 |
embedBtn.textContent = 'Embed Video for Discord';
|
436 |
embedBtn.className = 'embed-btn';
|
|
|
493 |
range_header = request.headers.get('Range')
|
494 |
|
495 |
headers = {'Range': range_header} if range_header else {}
|
496 |
+
response = requests.get(original_url, headers=headers, stream=True)
|
|
|
497 |
|
498 |
def generate():
|
499 |
+
for chunk in response.iter_content(chunk_size=8192):
|
500 |
+
yield chunk
|
501 |
|
502 |
headers = dict(response.headers)
|
503 |
headers['Access-Control-Allow-Origin'] = '*'
|
|
|
550 |
return HTMLResponse(content=html)
|
551 |
|
552 |
async def get_cookies() -> Dict[str, str]:
|
553 |
+
try:
|
554 |
+
response = requests.get('https://replicate.com/levelsio/neon-tokyo', headers={
|
555 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
|
556 |
})
|
557 |
return dict(response.cookies)
|
558 |
+
except Exception as e:
|
559 |
+
print(f'Error fetching the page: {e}')
|
560 |
+
return {}
|
561 |
|
562 |
async def initiate_upload(cookies: Dict[str, str], filename: str, content_type: str) -> Dict:
|
563 |
url = f'https://replicate.com/api/upload/{filename}?content_type={content_type}'
|
564 |
+
try:
|
565 |
+
response = requests.post(url, cookies=cookies, headers={
|
566 |
'X-CSRFToken': cookies.get('csrftoken'),
|
567 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',
|
568 |
'Referer': 'https://replicate.com/levelsio/neon-tokyo',
|
|
|
580 |
print(f'Initiate upload response headers: {response.headers}')
|
581 |
print(f'Response body: {response.text}')
|
582 |
return response.json()
|
583 |
+
except Exception as e:
|
584 |
+
print(f'Error initiating upload: {e}')
|
585 |
+
raise
|
586 |
|
587 |
async def upload_file(upload_url: str, file_content: bytes, content_type: str) -> bool:
|
588 |
+
try:
|
589 |
+
response = requests.put(upload_url, data=file_content, headers={'Content-Type': content_type})
|
590 |
print(f'File upload response status: {response.status_code}')
|
591 |
print(f'File upload response headers: {response.headers}')
|
592 |
return response.status_code == 200
|
593 |
+
except Exception as e:
|
594 |
+
print(f'Error uploading file: {e}')
|
595 |
+
return False
|
596 |
|
597 |
async def retry_upload(upload_url: str, file_content: bytes, content_type: str, max_retries: int = 5, delay: int = 1) -> bool:
|
598 |
for attempt in range(1, max_retries + 1):
|
|
|
603 |
return True
|
604 |
if attempt < max_retries:
|
605 |
print(f'Upload failed, retrying in {delay}s...')
|
606 |
+
time.sleep(delay)
|
607 |
delay *= 2 # Exponential backoff
|
608 |
print('Upload failed after all retry attempts')
|
609 |
return False
|