jaisun2004 commited on
Commit
402772f
·
verified ·
1 Parent(s): 895c36d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -9
app.py CHANGED
@@ -1,17 +1,25 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
  from langdetect import detect
 
 
 
 
4
 
5
- asr = pipeline("automatic-speech-recognition", model="openai/whisper-small")
6
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
7
 
8
  def process_audio(audio_path):
9
- # Accept only valid, non-empty file path (string)
10
  if not audio_path or not isinstance(audio_path, str):
11
  return "No audio file provided.", "", "", ""
12
  try:
13
- result = asr(audio_path)
14
- transcript = result["text"]
 
 
 
 
 
 
15
  except Exception as e:
16
  return f"Error in transcription: {e}", "", "", ""
17
  try:
@@ -23,8 +31,14 @@ def process_audio(audio_path):
23
  transcript_en = transcript
24
  if detected_lang != "en":
25
  try:
26
- result_translate = asr(audio_path, generate_kwargs={"task": "translate"})
27
- transcript_en = result_translate["text"]
 
 
 
 
 
 
28
  except Exception as e:
29
  transcript_en = f"Error translating: {e}"
30
  try:
@@ -43,8 +57,8 @@ iface = gr.Interface(
43
  gr.Textbox(label="English Transcript (if translated)"),
44
  gr.Textbox(label="Summary"),
45
  ],
46
- title="Audio Transcript, Translation & Summary",
47
- description="Upload your audio file (MP3/WAV). This app transcribes, detects language, translates to English if needed, and summarizes."
48
  )
49
 
50
  iface.launch()
 
1
  import gradio as gr
2
+ import openai
3
  from langdetect import detect
4
+ from transformers import pipeline
5
+ import os
6
+
7
+ openai.api_key = os.getenv("OPENAI_API_KEY") # Set this as a secret in your Space settings
8
 
 
9
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
10
 
11
  def process_audio(audio_path):
 
12
  if not audio_path or not isinstance(audio_path, str):
13
  return "No audio file provided.", "", "", ""
14
  try:
15
+ # Send audio to OpenAI Whisper API
16
+ with open(audio_path, "rb") as audio_file:
17
+ transcript_response = openai.audio.transcriptions.create(
18
+ model="whisper-1",
19
+ file=audio_file,
20
+ response_format="text"
21
+ )
22
+ transcript = transcript_response
23
  except Exception as e:
24
  return f"Error in transcription: {e}", "", "", ""
25
  try:
 
31
  transcript_en = transcript
32
  if detected_lang != "en":
33
  try:
34
+ # Re-send with task=translate for translation to English
35
+ with open(audio_path, "rb") as audio_file:
36
+ translation_response = openai.audio.translations.create(
37
+ model="whisper-1",
38
+ file=audio_file,
39
+ response_format="text"
40
+ )
41
+ transcript_en = translation_response
42
  except Exception as e:
43
  transcript_en = f"Error translating: {e}"
44
  try:
 
57
  gr.Textbox(label="English Transcript (if translated)"),
58
  gr.Textbox(label="Summary"),
59
  ],
60
+ title="Audio Transcript, Translation & Summary (via OpenAI Whisper API)",
61
+ description="Upload your audio file (MP3/WAV). This app transcribes via OpenAI Whisper API, detects language, translates to English if needed, and summarizes."
62
  )
63
 
64
  iface.launch()