Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,8 @@
|
|
1 |
import gradio as gr
|
2 |
-
import requests
|
3 |
-
import os
|
4 |
-
|
5 |
-
# Define the Whisper ASR function (transcribe_audio) here
|
6 |
-
|
7 |
-
# Retrieve the API token from the environment variable
|
8 |
-
API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
9 |
-
|
10 |
-
# Check if the API token is available
|
11 |
-
if not API_TOKEN:
|
12 |
-
raise ValueError("HUGGINGFACE_API_TOKEN environment variable is not set.")
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}
|
17 |
-
|
18 |
-
def query(payload):
|
19 |
-
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
20 |
-
return response.json()
|
21 |
|
22 |
-
def summarize_video(youtube_url: str, task: str = "transcribe", return_timestamps: bool = False) -> dict:
|
23 |
-
# Call your transcribe_audio function to get the transcription
|
24 |
-
transcription_result = transcribe_audio(youtube_url, task, return_timestamps)
|
25 |
-
|
26 |
-
# Summarize the transcription
|
27 |
-
summary_result = query({
|
28 |
-
"inputs": transcription_result["transcription"]
|
29 |
-
})
|
30 |
-
|
31 |
-
return summary_result[0]['summary_text']
|
32 |
-
|
33 |
def transcribe_audio(youtube_url: str, task: str = "transcribe", return_timestamps: bool = False, api_name: str = "/predict_2") -> dict:
|
34 |
"""
|
35 |
Transcribe audio from a given YouTube URL using a specified model.
|
@@ -45,19 +18,24 @@ def transcribe_audio(youtube_url: str, task: str = "transcribe", return_timestam
|
|
45 |
result = client.predict(youtube_url, task, return_timestamps, fn_index=7)
|
46 |
return result
|
47 |
|
|
|
|
|
48 |
MODEL_NAME = "openai/whisper-large-v2"
|
49 |
|
|
|
|
|
|
|
50 |
EXAMPLES = [
|
51 |
-
["https://www.youtube.com/watch?v=H1YoNlz2LxA", "translate",
|
52 |
]
|
53 |
|
54 |
-
|
55 |
yt_transcribe = gr.Interface(
|
56 |
fn=transcribe_audio,
|
57 |
inputs=[
|
58 |
gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
|
59 |
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
60 |
-
gr.inputs.Checkbox(label="Return timestamps")
|
61 |
],
|
62 |
outputs=[gr.outputs.HTML(label="Video"),
|
63 |
gr.outputs.Textbox(label="Transcription").style(show_copy_button=True)],
|
@@ -74,32 +52,8 @@ yt_transcribe = gr.Interface(
|
|
74 |
cache_examples=False
|
75 |
)
|
76 |
|
77 |
-
|
78 |
-
yt_summarize = gr.Interface(
|
79 |
-
fn=summarize_video,
|
80 |
-
inputs=[
|
81 |
-
gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
|
82 |
-
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
83 |
-
gr.inputs.Checkbox(label="Return timestamps")
|
84 |
-
],
|
85 |
-
outputs=[gr.outputs.HTML(label="Video"),
|
86 |
-
gr.outputs.Textbox(label="Summary").style(show_copy_button=True)],
|
87 |
-
layout="horizontal",
|
88 |
-
theme=gr.themes.Base(),
|
89 |
-
title="Whisper Large V2: Summarize YouTube",
|
90 |
-
description=(
|
91 |
-
"Summarize long-form YouTube videos with the click of a button! This tab uses the Whisper ASR model for transcription"
|
92 |
-
" and BART for summarization."
|
93 |
-
),
|
94 |
-
allow_flagging="never",
|
95 |
-
examples=EXAMPLES,
|
96 |
-
cache_examples=False
|
97 |
-
)
|
98 |
-
|
99 |
-
# Add the "Summarize" tab to the Gradio interface
|
100 |
-
|
101 |
-
# Launch the Gradio interface
|
102 |
-
with yt_transcribe:
|
103 |
gr.DuplicateButton()
|
104 |
-
gr.TabbedInterface([yt_transcribe
|
105 |
-
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
import os
|
4 |
+
from gradio_client import Client
|
|
|
|
|
|
|
|
|
|
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
def transcribe_audio(youtube_url: str, task: str = "transcribe", return_timestamps: bool = False, api_name: str = "/predict_2") -> dict:
|
7 |
"""
|
8 |
Transcribe audio from a given YouTube URL using a specified model.
|
|
|
18 |
result = client.predict(youtube_url, task, return_timestamps, fn_index=7)
|
19 |
return result
|
20 |
|
21 |
+
|
22 |
+
|
23 |
MODEL_NAME = "openai/whisper-large-v2"
|
24 |
|
25 |
+
|
26 |
+
demo = gr.Blocks()
|
27 |
+
|
28 |
EXAMPLES = [
|
29 |
+
["https://www.youtube.com/watch?v=H1YoNlz2LxA", "translate",False],
|
30 |
]
|
31 |
|
32 |
+
|
33 |
yt_transcribe = gr.Interface(
|
34 |
fn=transcribe_audio,
|
35 |
inputs=[
|
36 |
gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
|
37 |
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
38 |
+
gr.inputs.Checkbox(label="Return timestamps")
|
39 |
],
|
40 |
outputs=[gr.outputs.HTML(label="Video"),
|
41 |
gr.outputs.Textbox(label="Transcription").style(show_copy_button=True)],
|
|
|
52 |
cache_examples=False
|
53 |
)
|
54 |
|
55 |
+
with demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
gr.DuplicateButton()
|
57 |
+
gr.TabbedInterface([yt_transcribe], [ "YouTube"])
|
58 |
+
|
59 |
+
demo.launch(enable_queue=True)
|