Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import random
|
| 4 |
+
import re
|
| 5 |
+
import sys
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from typing import List, Optional
|
| 9 |
+
from uuid import uuid4
|
| 10 |
+
import pandas as pd
|
| 11 |
+
import gradio as gr
|
| 12 |
+
import numpy as np
|
| 13 |
+
import requests
|
| 14 |
+
from datasets import load_dataset
|
| 15 |
+
from huggingface_hub import (
|
| 16 |
+
CommitScheduler,
|
| 17 |
+
HfApi,
|
| 18 |
+
InferenceClient,
|
| 19 |
+
login,
|
| 20 |
+
snapshot_download,
|
| 21 |
+
)
|
| 22 |
+
from PIL import Image
|
| 23 |
+
from glob import glob
|
| 24 |
+
import requests
|
| 25 |
+
import re
|
| 26 |
+
import time
|
| 27 |
+
|
| 28 |
+
cached_latest_posts_df = None
|
| 29 |
+
last_fetched = None
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def get_latest_pots():
|
| 33 |
+
global cached_latest_posts_df
|
| 34 |
+
global last_fetched
|
| 35 |
+
|
| 36 |
+
# make sure we don't fetch data too often, limit to 1 request per 10 minutes
|
| 37 |
+
now_time = datetime.now()
|
| 38 |
+
if last_fetched is not None and (now_time - last_fetched).seconds < 600:
|
| 39 |
+
print("Using cached data")
|
| 40 |
+
return cached_latest_posts_df
|
| 41 |
+
|
| 42 |
+
last_fetched = now_time
|
| 43 |
+
url = "https://www.reddit.com/r/GamePhysics/.json"
|
| 44 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
| 45 |
+
|
| 46 |
+
response = requests.get(url, headers=headers)
|
| 47 |
+
if response.status_code != 200:
|
| 48 |
+
return []
|
| 49 |
+
|
| 50 |
+
data = response.json()
|
| 51 |
+
|
| 52 |
+
# Extract posts from the data
|
| 53 |
+
posts = data["data"]["children"]
|
| 54 |
+
|
| 55 |
+
for post in posts:
|
| 56 |
+
title = post["data"]["title"]
|
| 57 |
+
post_id = post["data"]["id"]
|
| 58 |
+
print(f"ID: {post_id}, Title: {title}")
|
| 59 |
+
|
| 60 |
+
# create [post_id, title] list
|
| 61 |
+
examples = [[post["data"]["id"], post["data"]["title"]] for post in posts]
|
| 62 |
+
# make a dataframe
|
| 63 |
+
examples = pd.DataFrame(examples, columns=["post_id", "title"])
|
| 64 |
+
cached_latest_posts_df = examples
|
| 65 |
+
return examples
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def row_selected(evt: gr.SelectData):
|
| 69 |
+
global cached_latest_posts_df
|
| 70 |
+
row = evt.index[0]
|
| 71 |
+
post_id = cached_latest_posts_df.iloc[row]["post_id"]
|
| 72 |
+
return post_id
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def load_video(url):
|
| 76 |
+
# Regular expression pattern for r/GamePhysics URLs and IDs
|
| 77 |
+
pattern = r"https://www\.reddit\.com/r/GamePhysics/comments/([0-9a-zA-Z]+).*|([0-9a-zA-Z]+)"
|
| 78 |
+
|
| 79 |
+
# Match the URL or ID against the pattern
|
| 80 |
+
match = re.match(pattern, url)
|
| 81 |
+
|
| 82 |
+
if match:
|
| 83 |
+
# Extract the post ID from the URL
|
| 84 |
+
post_id = match.group(1) or match.group(2)
|
| 85 |
+
print(f"Valid GamePhysics post ID: {post_id}")
|
| 86 |
+
else:
|
| 87 |
+
post_id = url
|
| 88 |
+
|
| 89 |
+
video_url = f"https://huggingface.co/datasets/asgaardlab/GamePhysicsDailyDump/resolve/main/data/videos/{post_id}.mp4?download=true"
|
| 90 |
+
|
| 91 |
+
# make sure file exists before returning, make a request without downloading the file
|
| 92 |
+
r = requests.head(video_url)
|
| 93 |
+
if r.status_code != 200 and r.status_code != 302:
|
| 94 |
+
raise gr.Error(
|
| 95 |
+
f"Video is not in the repo, please try another post. - {r.status_code }"
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
return video_url
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
with gr.Blocks() as demo:
|
| 102 |
+
gr.Markdown("## Preview GamePhysics")
|
| 103 |
+
|
| 104 |
+
with gr.Row():
|
| 105 |
+
reddit_id = gr.Textbox(lines=1, placeholder="Post url or id here", label="URL")
|
| 106 |
+
dummt_title = gr.Textbox(visible=False)
|
| 107 |
+
load_btn = gr.Button("Load")
|
| 108 |
+
|
| 109 |
+
video_player = gr.Video()
|
| 110 |
+
|
| 111 |
+
gr.Markdown("## Latest Posts")
|
| 112 |
+
|
| 113 |
+
with gr.Column():
|
| 114 |
+
get_latest_pots_btn = gr.Button("Show Latest Posts")
|
| 115 |
+
latest_post_dataframe = gr.Dataframe()
|
| 116 |
+
|
| 117 |
+
load_btn.click(load_video, inputs=[reddit_id], outputs=[video_player])
|
| 118 |
+
|
| 119 |
+
get_latest_pots_btn.click(get_latest_pots, outputs=[latest_post_dataframe])
|
| 120 |
+
# demo.load(get_latest_pots, outputs=[app_examples])
|
| 121 |
+
|
| 122 |
+
latest_post_dataframe.select(fn=row_selected, outputs=[reddit_id])
|
| 123 |
+
|
| 124 |
+
demo.launch()
|