Illia56 commited on
Commit
083fb9e
1 Parent(s): 9f6a51f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -39
app.py CHANGED
@@ -1,60 +1,47 @@
1
  import gradio as gr
 
2
  import os
3
- import requests
4
  from gradio_client import Client
5
 
6
- # Define the Hugging Face API URL and headers with your token
7
- API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
8
- HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN") # Replace with your actual token
9
- headers = {"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}"}
10
-
11
- def query(payload):
12
- response = requests.post(API_URL, headers=headers, json=payload)
13
- return response.json()
14
-
15
- def transcribe_and_summarize(youtube_url: str, task: str = "transcribe", return_timestamps: bool = False, summarize: bool = False, api_name: str = "/predict_2") -> dict:
 
16
  client = Client("https://sanchit-gandhi-whisper-jax.hf.space/")
17
- result = list(client.predict(youtube_url, task, return_timestamps, fn_index=7))
18
-
19
- # If the "summarize" checkbox is selected, summarize the transcription
20
- if summarize:
21
- transcription = result[1]
22
- summary_result = query({"inputs": transcription})
23
- try:
24
- result[2] = summary_result[0]['summary_text']
25
- except:
26
- result[2] = 'Model is overloaded.'
27
-
28
- else:
29
- result[2] = ''
30
-
31
  return result
32
 
 
 
33
  MODEL_NAME = "openai/whisper-large-v2"
34
 
 
35
  demo = gr.Blocks()
36
 
37
  EXAMPLES = [
38
- ["https://www.youtube.com/watch?v=HyBw3wcZ124", "transcribe", False],
39
  ]
40
 
41
- # Define the Gradio interface with the "Summarize" checkbox and "Summary" output
42
  yt_transcribe = gr.Interface(
43
- fn=transcribe_and_summarize,
44
  inputs=[
45
  gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
46
  gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
47
- gr.inputs.Checkbox(label="Return timestamps"),
48
- gr.inputs.Checkbox(label="Summarize") # Added "Summarize" checkbox
49
- ],
50
- outputs=[
51
- gr.outputs.HTML(label="Video"),
52
- gr.outputs.Textbox(label="Transcription").style(show_copy_button=True),
53
- gr.outputs.Textbox(label="Summary").style(show_copy_button=True) # Added "Summary" output
54
  ],
 
 
55
  layout="horizontal",
56
  theme=gr.themes.Base(),
57
- title="Whisper Large V2: Transcribe YouTube with Summarization",
58
  description=(
59
  "Transcribe long-form YouTube videos with the click of a button! Demo uses the checkpoint"
60
  f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe video files of"
@@ -65,8 +52,8 @@ yt_transcribe = gr.Interface(
65
  cache_examples=False
66
  )
67
 
68
- with demo():
69
  gr.DuplicateButton()
70
- gr.TabbedInterface([yt_transcribe], ["YouTube"])
71
 
72
- demo.launch(enable_queue=True)
 
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.
9
+ Parameters:
10
+ - youtube_url (str): The YouTube URL to transcribe.
11
+ - task (str, optional): The task to perform. Default is "transcribe".
12
+ - return_timestamps (bool, optional): Whether to return timestamps. Default is True.
13
+ - api_name (str, optional): The API endpoint to use. Default is "/predict_2".
14
+ Returns:
15
+ - dict: The transcription result.
16
+ """
17
  client = Client("https://sanchit-gandhi-whisper-jax.hf.space/")
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)],
42
  layout="horizontal",
43
  theme=gr.themes.Base(),
44
+ title="Whisper Large V2: Transcribe YouTube",
45
  description=(
46
  "Transcribe long-form YouTube videos with the click of a button! Demo uses the checkpoint"
47
  f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe video files of"
 
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)