Skills_Lecture / app.py
gopallikehack's picture
Update app.py
0e76cc0 verified
Raw History Blame Contribute Delete
1.46 kB
from flask import Flask, render_template, request, Response
from huggingface_hub import HfApi
import requests
app = Flask(__name__)
DATASET = "gopallikehack/Skills_Lecture"
api = HfApi()
def get_videos():
files = api.list_repo_files(
repo_id=DATASET,
repo_type="dataset"
)
return [
x for x in files
if x.lower().endswith(".mp4")
]
@app.route("/")
def home():
videos = get_videos()
return render_template(
"index.html",
videos=videos
)
@app.route("/watch/<path:name>")
def watch(name):
return render_template(
"player.html",
title=name,
video=name
)
@app.route("/stream/<path:name>")
def stream(name):
url = (
f"https://huggingface.co/datasets/"
f"{DATASET}/resolve/main/{name}"
)
headers={}
if "Range" in request.headers:
headers["Range"] = request.headers["Range"]
r = requests.get(
url,
headers=headers,
stream=True
)
response = Response(
r.iter_content(
chunk_size=1024*1024
),
status=r.status_code,
content_type=r.headers.get(
"content-type"
)
)
if "Content-Range" in r.headers:
response.headers["Content-Range"] = r.headers["Content-Range"]
if "Accept-Ranges" in r.headers:
response.headers["Accept-Ranges"] = r.headers["Accept-Ranges"]
return response