File size: 2,489 Bytes
1d6eeba
402772f
1d6eeba
402772f
 
 
 
1d6eeba
e227091
310b5ef
2e174b1
8ddae8d
310b5ef
1d6eeba
402772f
 
 
 
 
 
 
 
1d6eeba
2e174b1
1d6eeba
 
 
 
 
 
 
 
 
402772f
 
 
 
 
 
 
 
1d6eeba
 
 
 
 
 
 
 
 
e227091
 
8ddae8d
e227091
 
 
 
 
 
402772f
 
e227091
2e174b1
e227091
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import gradio as gr
import openai
from langdetect import detect
from transformers import pipeline
import os

openai.api_key = os.getenv("OPENAI_API_KEY")  # Set this as a secret in your Space settings

summarizer = pipeline("summarization", model="facebook/bart-large-cnn")

def process_audio(audio_path):
    if not audio_path or not isinstance(audio_path, str):
        return "No audio file provided.", "", "", ""
    try:
        # Send audio to OpenAI Whisper API
        with open(audio_path, "rb") as audio_file:
            transcript_response = openai.audio.transcriptions.create(
                model="whisper-1",
                file=audio_file,
                response_format="text"
            )
        transcript = transcript_response
    except Exception as e:
        return f"Error in transcription: {e}", "", "", ""
    try:
        detected_lang = detect(transcript)
    except Exception:
        detected_lang = "unknown"
    lang_map = {'en': 'English', 'hi': 'Hindi', 'ta': 'Tamil'}
    lang_text = lang_map.get(detected_lang, detected_lang)
    transcript_en = transcript
    if detected_lang != "en":
        try:
            # Re-send with task=translate for translation to English
            with open(audio_path, "rb") as audio_file:
                translation_response = openai.audio.translations.create(
                    model="whisper-1",
                    file=audio_file,
                    response_format="text"
                )
            transcript_en = translation_response
        except Exception as e:
            transcript_en = f"Error translating: {e}"
    try:
        summary = summarizer(transcript_en, max_length=100, min_length=30, do_sample=False)
        summary_text = summary[0]["summary_text"]
    except Exception as e:
        summary_text = f"Error summarizing: {e}"
    return lang_text, transcript, transcript_en, summary_text

iface = gr.Interface(
    fn=process_audio,
    inputs=gr.Audio(type="filepath", label="Upload MP3/WAV Audio"),
    outputs=[
        gr.Textbox(label="Detected Language"),
        gr.Textbox(label="Original Transcript"),
        gr.Textbox(label="English Transcript (if translated)"),
        gr.Textbox(label="Summary"),
    ],
    title="Audio Transcript, Translation & Summary (via OpenAI Whisper API)",
    description="Upload your audio file (MP3/WAV). This app transcribes via OpenAI Whisper API, detects language, translates to English if needed, and summarizes."
)

iface.launch()