huu-ontocord's picture
Update app.py
ea3a352 verified
import gradio as gr
import json
from huggingface_hub import HfApi
import uuid
# Initialize the Hugging Face API
api = HfApi()
# Replace with your Hugging Face username and repository name
REPO_ID = "ontocord/prompt-share-storage"
import gradio as gr
import cv2
def process_video(input_video):
cap = cv2.VideoCapture(input_video)
output_path = "output.mp4"
fps = int(cap.get(cv2.CAP_PROP_FPS))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
video = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))
iterating, frame = cap.read()
while iterating:
# flip frame vertically
frame = cv2.flip(frame, 0)
display_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
video.write(frame)
yield display_frame, None
iterating, frame = cap.read()
video.release()
yield display_frame, output_path
def save_to_json(text1, text2, text3, video, agree):
"""Saves the input text and video with UUIDs to files and uploads to Hugging Face Hub."""
if agree:
# Generate a unique UUID
file_uuid = str(uuid.uuid4())
# Save the video with UUID prepended to the filename
video_filename = f"{file_uuid}_uploaded_video.mp4"
video.save(video_filename)
data = {
"text": text1,
"video_filename": video_filename
}
# Save the JSON with UUID prepended to the filename
json_filename = f"{file_uuid}_data.json"
with open(json_filename, "w") as f:
json.dump(data, f)
# Upload the files to Hugging Face Hub
api.upload_file(
path_or_fileobj=json_filename,
path_in_repo=json_filename,
repo_id=REPO_ID
)
api.upload_file(
path_or_fileobj=video_filename,
path_in_repo=video_filename,
repo_id=REPO_ID
)
return "Data saved and uploaded to Hugging Face Hub."
else:
return "Please agree to the terms before submitting."
with gr.Blocks() as demo:
with gr.Row():
input_video = gr.Video(label="input")
processed_frames = gr.Image(label="last frame")
output_video = gr.Video(label="output")
with gr.Row():
process_video_btn = gr.Button("process video")
process_video_btn.click(process_video, input_video, [processed_frames, output_video])
"""
iface = gr.Interface(
fn=save_to_json,
inputs=[
gr.Video(format="mp4"),
gr.TextArea(lines=5, placeholder="(Optional) Enter something that would help an AI understand this video, like a pretend conversation about the video (e.g., Q: Hi - what am I doing? A: Good, it looks like you are at a cafe.)\n-Or a detailed description about the video (e.g., This video shows a person at a cafe, with nosiy happy patrons, and a dynamic atmosphere)\n - or a question and answer that requires reasoning (e.g., Q: If two friends joined, how many coffees would be needed? A: Since you have one coffee in your hand, and two friends joined you, assuming they each like a coffee, two more coffees are needed, making a total of 3 coffees in this scene)"),
gr.Checkbox(label="I agree and have the rights to share these prompts under the CC-BY license.")
],
outputs="text",
title="Save Text and Video to Hugging Face",
description="Share a video and help create an open source AI training dataset. Just take a video and describe what you see, what you are doing, or have a conversation with someone. Make sure everyone has consented to be in the video. Optionally enter in some text that might help an AI understand the vidoe. Then click submit to save to ontocord/prompt-share-storage Dataset. DO NOT share personal information. Thank you!"
)
"""
demo.launch()