fastperson / app.py
kwmr's picture
upload
0614214
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('''
<div style="text-align: center">
<h1 style='text-align: center'>Video Summarization</h1>
</div>
''')
with gr.Row():
gr.Markdown('''
### Summarize video
<ul>
<li>Step 1. download a video from youtube (select one of the examples and press the Download button)</li>
<li>Step 2: Select the summary rate and playback speed (select with slider)</li>
<li>Step 3: Generate a summarized video (press the Summarize button)</li>
</ul>
''')
with gr.Row():
gr.Markdown('''
#### Output
<ul>
<li>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.</li>
<li>A text summary of the video is displayed below it.</li>
<li>A transcription of the original video is displayed further down (the part extracted from the summarized video is highlighted).</li>
</ul>
''')
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)