import os
import random
from functools import partial
import json
import base64
import gradio as gr
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
from utils import get_youtube
from model import summarize_video
# 環境変数から秘密鍵を取得
encoded_key = os.environ["FIREBASE_CREDENTIALS_BASE64"]
# Base64エンコードされた秘密鍵をデコード
decoded_key = base64.b64decode(encoded_key)
# デコードされた秘密鍵を使ってCredentialオブジェクトを作成
cred = credentials.Certificate(json.loads(decoded_key))
# cred = credentials.Certificate('/home/kazuki/casp/fastperson-538e9-firebase-adminsdk-285dh-fa3faea9d0.json')
# Firebase Admin SDKを初期化
firebase_admin.initialize_app(cred)
db = firestore.client()
def log_firestore(db, local_id="000000", action="test", user_id=""):
data = {
"local_id": local_id,
"timestamp": firestore.SERVER_TIMESTAMP,
"action": action,
"user_id": user_id
}
doc_ref = db.collection("exp.1").document()
doc_ref.set(data)
local_id = random.randint(0, 10000000)
def store_local_id(user_id, local_id, db):
log_firestore(db, local_id=str(local_id), action='ID', user_id=str(user_id))
# ---- Gradio Layout -----
youtube_url_in = gr.Textbox(label="Youtube url", lines=1, interactive=True)
video_in = gr.Video(label="Input Video", mirror_webcam=False, interactive=True)
video_out = gr.Video(label="Output Video")
summary_text = gr.Textbox(label="Video Transcription Summary")
transcription_text = gr.HTML(label="Full Transcription")
demo = gr.Blocks()
demo.encrypt = False
with demo:
with gr.Column():
gr.Markdown('''
Video Summarization
''')
with gr.Row():
gr.Markdown('''
### Summarize video
- Step 1. download a video from youtube (select one of the examples and press the Download button)
- Step 2: Select the summary rate and playback speed (select with slider)
- Step 3: Generate a summarized video (press the Summarize button)
''')
with gr.Row():
gr.Markdown('''
#### Output
- An original downloaded video is displayed on the left side of the screen, and a summarized video is displayed on the right side of the screen.
- A text summary of the video is displayed below it.
- A transcription of the original video is displayed further down (the part extracted from the summarized video is highlighted).
''')
with gr.Row():
gr.Markdown('''
### User ID
Fill in the user ID if the author of this system has instructed you to do so.
''')
with gr.Column():
user_id_input = gr.Textbox(placeholder="Enter your user ID", label="User ID")
store_local_id_wrapped = partial(store_local_id, local_id=local_id, db=db)
user_id_btn = gr.Button("Submit User ID")
user_id_btn.click(store_local_id_wrapped, inputs=[user_id_input])
with gr.Row():
gr.Markdown('''
### You can test by following examples:
''')
examples = gr.Examples(examples=
[ "https://www.youtube.com/watch?v=QghjaS0WQQU",
"https://www.youtube.com/watch?v=k_04qb_65l0",
"https://www.youtube.com/watch?v=yArB4rgA1hM",
"https://www.youtube.com/watch?v=SzAuB2FG79A",
"https://www.youtube.com/watch?v=f60dheI4ARg"
],
label="Examples", inputs=[youtube_url_in])
with gr.Column():
youtube_url_in.render()
download_youtube_btn = gr.Button("Download Youtube video")
get_youtube_wrapped = partial(get_youtube, local_id=local_id, db=db)
download_youtube_btn.click(get_youtube_wrapped, [youtube_url_in], [video_in])
with gr.Row():
sum_ratio = gr.Slider(label="Summarize Ratio", minimum=0.3, maximum=0.8, step=0.05, value=0.6)
playback_speed = gr.Slider(label="Playback Speed", minimum=0.5, maximum=2.0, step=0.25, value=1.0)
with gr.Row():
upload_output_video_btn = gr.Button("Summarize Video")
summarize_video_wrapped = partial(summarize_video, local_id=local_id, db=db)
upload_output_video_btn.click(summarize_video_wrapped, [video_in, sum_ratio, playback_speed], [video_out, summary_text, transcription_text])
with gr.Row():
video_in.render()
video_out.render()
with gr.Row():
summary_text.render()
with gr.Row():
transcription_text.render()
# demo.launch(debug=True, share=True)
demo.launch(debug=True)