Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import re
|
| 4 |
+
import time
|
| 5 |
+
from datetime import datetime, timezone
|
| 6 |
+
|
| 7 |
+
# Constants
|
| 8 |
+
RUNPOD_API_KEY = "rpa_0PN9A3W28RFTK3LDY6M3LRO5HT9SWI441HQV2V6A1jt5ih"
|
| 9 |
+
RUNPOD_ENDPOINT = "https://api.runpod.ai/v2/vz476hgvqvzojy"
|
| 10 |
+
USER_LOGIN = "Ammariii08"
|
| 11 |
+
|
| 12 |
+
HEADERS = {
|
| 13 |
+
"Content-Type": "application/json",
|
| 14 |
+
"Authorization": f"Bearer {RUNPOD_API_KEY}"
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
def extract_file_id(url):
|
| 18 |
+
m = re.search(r'/d/([a-zA-Z0-9_-]+)', url) or re.search(r'id=([a-zA-Z0-9_-]+)', url)
|
| 19 |
+
return m.group(1) if m else None
|
| 20 |
+
|
| 21 |
+
def to_direct_download(link):
|
| 22 |
+
fid = extract_file_id(link)
|
| 23 |
+
return f"https://drive.google.com/uc?export=download&id={fid}" if fid else None
|
| 24 |
+
|
| 25 |
+
def make_api_request(fid):
|
| 26 |
+
payload = {"input": {"video_url": f"https://drive.google.com/uc?export=download&id={fid}"}}
|
| 27 |
+
resp = requests.post(f"{RUNPOD_ENDPOINT}/run", headers=HEADERS, json=payload, timeout=1800)
|
| 28 |
+
resp.raise_for_status()
|
| 29 |
+
return resp.json()
|
| 30 |
+
|
| 31 |
+
def process_video(drive_link, progress=gr.Progress()):
|
| 32 |
+
fid = extract_file_id(drive_link)
|
| 33 |
+
if not fid:
|
| 34 |
+
progress(1.0, desc="Invalid URL")
|
| 35 |
+
return "❌ Invalid Drive URL", "", "–", ""
|
| 36 |
+
progress(0.05, desc="Initializing…")
|
| 37 |
+
t0 = time.time()
|
| 38 |
+
try:
|
| 39 |
+
job = make_api_request(fid)
|
| 40 |
+
except Exception as e:
|
| 41 |
+
progress(1.0, desc="Submission Failed")
|
| 42 |
+
return f"❌ Submission error: {e}", "", "–", ""
|
| 43 |
+
progress(0.2, desc="Job Submitted")
|
| 44 |
+
t1 = time.time()
|
| 45 |
+
job_id = job["id"]
|
| 46 |
+
|
| 47 |
+
prog = 0.2
|
| 48 |
+
while True:
|
| 49 |
+
st = requests.get(f"{RUNPOD_ENDPOINT}/status/{job_id}", headers=HEADERS).json()
|
| 50 |
+
status = st.get("status", "UNKNOWN")
|
| 51 |
+
if status == "COMPLETED":
|
| 52 |
+
t2 = time.time()
|
| 53 |
+
exec_time = t2 - t1
|
| 54 |
+
out = st["output"]
|
| 55 |
+
dl_link = to_direct_download(out["drive_link"]) or out["drive_link"]
|
| 56 |
+
break
|
| 57 |
+
if status == "FAILED":
|
| 58 |
+
progress(1.0, desc="Job Failed")
|
| 59 |
+
return "❌ Job failed", "", "–", ""
|
| 60 |
+
prog = min(prog + 0.1, 0.9)
|
| 61 |
+
progress(prog, desc=f"Waiting ({status})…")
|
| 62 |
+
time.sleep(5)
|
| 63 |
+
|
| 64 |
+
progress(1.0, desc="Completed")
|
| 65 |
+
delay = t1 - t0
|
| 66 |
+
|
| 67 |
+
details_md = f"""
|
| 68 |
+
**Drive Link:**
|
| 69 |
+
`{dl_link}`
|
| 70 |
+
|
| 71 |
+
**Job ID:** `{job_id}`
|
| 72 |
+
**Status:** `{status}`
|
| 73 |
+
|
| 74 |
+
**Submission Delay:** `{delay:.1f}s`
|
| 75 |
+
**Execution Time:** `{exec_time:.1f}s`
|
| 76 |
+
"""
|
| 77 |
+
|
| 78 |
+
# Render a styled HTML link inside Markdown
|
| 79 |
+
button_md = f'<a href="{dl_link}" target="_blank" style="display:inline-block;padding:8px 16px;background:#4CAF50;color:white;border-radius:4px;text-decoration:none;">Open Download</a>'
|
| 80 |
+
|
| 81 |
+
return "✅ Completed!", dl_link, details_md, button_md
|
| 82 |
+
|
| 83 |
+
def create_ui():
|
| 84 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 85 |
+
gr.Markdown(f"""
|
| 86 |
+
# AXION 🎾
|
| 87 |
+
## RUNPOD Deployment Testing
|
| 88 |
+
|
| 89 |
+
""")
|
| 90 |
+
link_in = gr.Textbox(label="Google Drive Link", placeholder="https://drive.google.com/file/d/…")
|
| 91 |
+
btn = gr.Button("Process Video")
|
| 92 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 93 |
+
dl_out = gr.Textbox(label="Download Link", interactive=False)
|
| 94 |
+
details = gr.Markdown(label="Details")
|
| 95 |
+
open_btn = gr.Markdown() # <-- always present
|
| 96 |
+
|
| 97 |
+
btn.click(
|
| 98 |
+
fn=process_video,
|
| 99 |
+
inputs=[link_in],
|
| 100 |
+
outputs=[status, dl_out, details, open_btn]
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
return app
|
| 104 |
+
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
create_ui().launch(share=True, server_name="0.0.0.0", server_port=7860)
|