Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,48 @@
|
|
1 |
-
|
2 |
import gradio as gr
|
3 |
-
|
4 |
import os
|
5 |
-
from gradio_client import Client
|
6 |
-
|
7 |
-
def transcribe_audio(youtube_url: str, task: str = "transcribe", return_timestamps: bool = False, api_name: str = "/predict_2") -> dict:
|
8 |
-
"""
|
9 |
-
Transcribe audio from a given YouTube URL using a specified model.
|
10 |
|
11 |
-
|
12 |
-
- youtube_url (str): The YouTube URL to transcribe.
|
13 |
-
- task (str, optional): The task to perform. Default is "transcribe".
|
14 |
-
- return_timestamps (bool, optional): Whether to return timestamps. Default is True.
|
15 |
-
- api_name (str, optional): The API endpoint to use. Default is "/predict_2".
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
"""
|
20 |
-
client = Client("https://sanchit-gandhi-whisper-jax.hf.space/")
|
21 |
-
result = client.predict(youtube_url, task, return_timestamps, fn_index=7)
|
22 |
-
return result
|
23 |
|
|
|
|
|
|
|
24 |
|
|
|
|
|
|
|
25 |
|
26 |
-
|
|
|
|
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
|
31 |
EXAMPLES = [
|
32 |
-
["https://www.youtube.com/watch?v=H1YoNlz2LxA", "translate",False],
|
33 |
]
|
34 |
|
35 |
-
|
36 |
yt_transcribe = gr.Interface(
|
37 |
fn=transcribe_audio,
|
38 |
inputs=[
|
39 |
gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
|
40 |
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
41 |
-
gr.inputs.Checkbox(label="Return timestamps")
|
42 |
],
|
43 |
outputs=[gr.outputs.HTML(label="Video"),
|
44 |
gr.outputs.Textbox(label="Transcription").style(show_copy_button=True)],
|
@@ -55,9 +59,34 @@ yt_transcribe = gr.Interface(
|
|
55 |
cache_examples=False
|
56 |
)
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
-
|
|
|
63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Define the BART summarization API URL
|
15 |
+
API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
|
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, summary_length: int = 150) -> 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"][:summary_length]
|
29 |
+
})
|
30 |
+
|
31 |
+
return summary_result
|
32 |
|
33 |
+
MODEL_NAME = "openai/whisper-large-v2"
|
34 |
|
35 |
EXAMPLES = [
|
36 |
+
["https://www.youtube.com/watch?v=H1YoNlz2LxA", "translate", False],
|
37 |
]
|
38 |
|
39 |
+
# Define the Gradio interface for transcription
|
40 |
yt_transcribe = gr.Interface(
|
41 |
fn=transcribe_audio,
|
42 |
inputs=[
|
43 |
gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
|
44 |
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
45 |
+
gr.inputs.Checkbox(label="Return timestamps"),
|
46 |
],
|
47 |
outputs=[gr.outputs.HTML(label="Video"),
|
48 |
gr.outputs.Textbox(label="Transcription").style(show_copy_button=True)],
|
|
|
59 |
cache_examples=False
|
60 |
)
|
61 |
|
62 |
+
# Define the Gradio interface for summarization
|
63 |
+
yt_summarize = gr.Interface(
|
64 |
+
fn=summarize_video,
|
65 |
+
inputs=[
|
66 |
+
gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
|
67 |
+
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
68 |
+
gr.inputs.Checkbox(label="Return timestamps"),
|
69 |
+
gr.inputs.Number(default=150, label="Summary Length", min=1, max=500),
|
70 |
+
],
|
71 |
+
outputs=[gr.outputs.HTML(label="Video"),
|
72 |
+
gr.outputs.Textbox(label="Summary").style(show_copy_button=True)],
|
73 |
+
layout="horizontal",
|
74 |
+
theme=gr.themes.Base(),
|
75 |
+
title="Whisper Large V2: Summarize YouTube",
|
76 |
+
description=(
|
77 |
+
"Summarize long-form YouTube videos with the click of a button! This tab uses the Whisper ASR model for transcription"
|
78 |
+
" and BART for summarization."
|
79 |
+
),
|
80 |
+
allow_flagging="never",
|
81 |
+
examples=EXAMPLES,
|
82 |
+
cache_examples=False
|
83 |
+
)
|
84 |
|
85 |
+
# Add the "Summarize" tab to the Gradio interface
|
86 |
+
yt_transcribe.tabs["Summarize"] = yt_summarize
|
87 |
|
88 |
+
# Launch the Gradio interface
|
89 |
+
with yt_transcribe:
|
90 |
+
gr.DuplicateButton()
|
91 |
+
gr.TabbedInterface([yt_transcribe], ["YouTube"])
|
92 |
+
yt_transcribe.launch(enable_queue=True)
|