antfraia commited on
Commit
161393d
·
1 Parent(s): 2cd9f21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -21
app.py CHANGED
@@ -1,34 +1,31 @@
1
  import gradio as gr
2
- from transformers import BartTokenizer, BartForConditionalGeneration
3
- import whisper
4
 
5
- # Initialize the BART model and tokenizer
6
- MODEL_NAME = "facebook/bart-large-cnn"
7
- model = BartForConditionalGeneration.from_pretrained(MODEL_NAME)
8
- tokenizer = BartTokenizer.from_pretrained(MODEL_NAME)
9
 
10
- def convert_and_summarize(audio_path: str) -> str:
11
- # Convert audio to text
12
- whisper_model = whisper.load_model("base")
13
- result = whisper_model.transcribe(audio_path)
14
- transcribed_text = result["text"]
15
 
16
- # Summarize the transcribed text
17
- inputs = tokenizer([transcribed_text], max_length=1024, truncation=True, return_tensors='pt')
18
- summary_ids = model.generate(inputs['input_ids'])
19
- summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
20
 
21
- return summary
22
 
23
  audio_input = gr.inputs.Audio(type="filepath")
 
24
 
25
- # Interface for Gradio
26
  iface = gr.Interface(
27
- fn=convert_and_summarize,
28
  inputs=audio_input,
29
- outputs="text",
30
- title="Audio-to-Summarized-Text",
31
- description="Upload an audio here and get a bullet-point summary of its content.",
32
  theme="Monochrome",
33
  live=True,
34
  capture_session=True,
 
1
  import gradio as gr
2
+ import requests
 
3
 
4
+ API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large-v2/whisper"
5
+ API_KEY = "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"
6
+ HEADERS = {"Authorization": f"Bearer {API_KEY}"}
 
7
 
8
+ def transcribe_audio(audio_path: str) -> str:
9
+ # Read audio file
10
+ with open(audio_path, "rb") as f:
11
+ audio_data = f.read()
 
12
 
13
+ # Make API request to OpenAI Whisper v2 API
14
+ response = requests.post(API_URL, headers=HEADERS, data=audio_data)
15
+ result = response.json()
16
+ transcribed_text = result["text"]
17
 
18
+ return transcribed_text
19
 
20
  audio_input = gr.inputs.Audio(type="filepath")
21
+ text_output = gr.outputs.Textbox()
22
 
 
23
  iface = gr.Interface(
24
+ fn=transcribe_audio,
25
  inputs=audio_input,
26
+ outputs=text_output,
27
+ title="Speech-to-Text using Whisper v2",
28
+ description="Upload an audio file to transcribe it to text.",
29
  theme="Monochrome",
30
  live=True,
31
  capture_session=True,