Hermit11 commited on
Commit
7fedbc9
·
verified ·
1 Parent(s): 1bdfe7b

Upload 5 files

Browse files
Files changed (5) hide show
  1. app.py +80 -0
  2. check_versions.py +20 -0
  3. main.py +49 -0
  4. requirements.txt +8 -0
  5. testAPIDeployment.py +13 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ import cv2
4
+ import numpy as np
5
+ import requests
6
+ import tempfile
7
+ import os
8
+
9
+ app = FastAPI()
10
+
11
+ class VideoURLs(BaseModel):
12
+ urls: list[str]
13
+
14
+ def download_video(url):
15
+ try:
16
+ response = requests.get(url, verify=False) # Added verify=False
17
+ response.raise_for_status()
18
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_file:
19
+ tmp_file.write(response.content)
20
+ return tmp_file.name
21
+ except requests.RequestException as e:
22
+ raise Exception(f"Failed to download video from {url}: {str(e)}")
23
+
24
+ def combine_videos(urls):
25
+ if not urls:
26
+ return None
27
+
28
+ temp_files = []
29
+ for url in urls:
30
+ if url.strip():
31
+ try:
32
+ temp_files.append(download_video(url.strip()))
33
+ except Exception as e:
34
+ raise HTTPException(status_code=400, detail=str(e))
35
+
36
+ if not temp_files:
37
+ raise HTTPException(status_code=400, detail="No valid videos to combine")
38
+
39
+ captures = [cv2.VideoCapture(file) for file in temp_files]
40
+
41
+ fps = captures[0].get(cv2.CAP_PROP_FPS)
42
+ frame_size = (int(captures[0].get(cv2.CAP_PROP_FRAME_WIDTH)),
43
+ int(captures[0].get(cv2.CAP_PROP_FRAME_HEIGHT)))
44
+
45
+ output_path = 'combined_video.mp4'
46
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
47
+ out = cv2.VideoWriter(output_path, fourcc, fps, frame_size)
48
+
49
+ for cap in captures:
50
+ while True:
51
+ ret, frame = cap.read()
52
+ if not ret:
53
+ break
54
+ out.write(frame)
55
+
56
+ for cap in captures:
57
+ cap.release()
58
+ out.release()
59
+
60
+ for file in temp_files:
61
+ os.remove(file)
62
+
63
+ return output_path
64
+
65
+ @app.post("/combine_videos")
66
+ async def process_urls(video_urls: VideoURLs):
67
+ try:
68
+ combined_video = combine_videos(video_urls.urls)
69
+ if combined_video:
70
+ return {"video_path": combined_video}
71
+ else:
72
+ raise HTTPException(status_code=400, detail="Failed to combine videos")
73
+ except HTTPException as he:
74
+ raise he
75
+ except Exception as e:
76
+ raise HTTPException(status_code=500, detail=str(e))
77
+
78
+ if __name__ == "__main__":
79
+ import uvicorn
80
+ uvicorn.run(app, host="0.0.0.0", port=8000)
check_versions.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import importlib
2
+ import subprocess
3
+
4
+ def get_package_version(package_name):
5
+ try:
6
+ return importlib.import_module(package_name).__version__
7
+ except ImportError:
8
+ return "Not installed"
9
+ except AttributeError:
10
+ # Some packages might not have __version__ attribute
11
+ try:
12
+ return subprocess.check_output([package_name, "--version"]).decode().strip()
13
+ except:
14
+ return "Version not found"
15
+
16
+ packages = ["numpy", "requests", "cv2", "gradio", "vimeo"]
17
+
18
+ for package in packages:
19
+ version = get_package_version(package)
20
+ print(f"{package}: {version}")
main.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+
4
+ # FastAPI backend URL
5
+ BACKEND_URL = "http://localhost:8000/combine_videos"
6
+
7
+ def add_url(url_list, new_url):
8
+ if new_url.strip():
9
+ url_list.append(new_url.strip())
10
+ return "\n".join(url_list), ""
11
+
12
+ def combine_videos(url_list):
13
+ urls = url_list.split("\n")
14
+ try:
15
+ response = requests.post(BACKEND_URL, json={"urls": urls})
16
+ response.raise_for_status()
17
+ video_path = response.json()["video_path"]
18
+ return video_path, gr.update(interactive=True)
19
+ except requests.RequestException as e:
20
+ error_message = f"Error: {str(e)}"
21
+ if response.status_code != 200:
22
+ error_message += f"\nServer response: {response.text}"
23
+ return error_message, gr.update(interactive=False)
24
+
25
+ def download_video(video_path):
26
+ return video_path if video_path and not video_path.startswith("Error:") else None
27
+
28
+ with gr.Blocks() as demo:
29
+ gr.Markdown("# KSL Video Combiner")
30
+
31
+ with gr.Row():
32
+ url_input = gr.Textbox(label="Enter a video URL")
33
+ add_button = gr.Button("Add URL")
34
+
35
+ url_list = gr.Textbox(label="Added URLs", lines=5)
36
+ combine_button = gr.Button("Combine Videos")
37
+
38
+ video_output = gr.Video(label="Combined Video")
39
+ download_button = gr.Button("Download Video", interactive=False)
40
+
41
+ url_list_state = gr.State([])
42
+
43
+ add_button.click(add_url, inputs=[url_list_state, url_input], outputs=[url_list, url_input])
44
+ combine_button.click(combine_videos, inputs=[url_list], outputs=[video_output, download_button])
45
+
46
+ download_button.click(download_video, inputs=[video_output], outputs=[gr.File()])
47
+
48
+ if __name__ == "__main__":
49
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ numpy
2
+ requests
3
+ opencv-python-headless
4
+ gradio
5
+ PyVimeo
6
+ fastapi
7
+ pydantic
8
+ uvicorn
testAPIDeployment.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @app.get("/", response_class=HTMLResponse)
2
+ async def index(request: Request):
3
+ print('Request for index page received')
4
+ return templates.TemplateResponse('index.html', {"request": request})
5
+
6
+ @app.post('/hello', response_class=HTMLResponse)
7
+ async def hello(request: Request, name: str = Form(...)):
8
+ if name:
9
+ print('Request for hello page received with name=%s' % name)
10
+ return templates.TemplateResponse('hello.html', {"request": request, 'name':name})
11
+ else:
12
+ print('Request for hello page received with no name or blank name -- redirecting')
13
+ return RedirectResponse(request.url_for("index"), status_code=status.HTTP_302_FOUND)