import os import spaces import torch import gradio as gr from transformers import pipeline MODEL_NAME = "openai/whisper-large-v3" BATCH_SIZE = 8 FILE_LIMIT_MB = 1000 device = 0 if torch.cuda.is_available() else "cpu" pipe = pipeline( task="automatic-speech-recognition", model=MODEL_NAME, chunk_length_s=30, device=device, ) def respond_to_question_llama(transcript, question): from huggingface_hub import InferenceClient client = InferenceClient( "meta-llama/Meta-Llama-3.1-70B-Instruct", token=os.environ["HUGGINGFACEHUB_API_TOKEN"], ) response = client.chat_completion( messages=[{"role": "user", "content": f"Transcript: {transcript}\n\nUser: {question}"}], max_tokens=4096, ).choices[0].message.content return response @spaces.GPU def audio_transcribe(inputs): status=True text="Arquivo de audio nao carregado!" status=False if inputs is None: raise gr.Error("No audio file submitted! Please upload an audio file before submitting your request.") else: text = pipe(inputs, batch_size=BATCH_SIZE, return_timestamps=True)['text'] status = True return [text, gr.Textbox(visible=status),gr.Textbox(visible=status),gr.Textbox(visible=status)] def hidden_ask_question(): return [gr.Textbox(visible=False),gr.Textbox(visible=False),gr.Textbox(visible=False)] with gr.Blocks() as transcriberUI: gr.Markdown( """ # Ola! Clique no botao abaixo para selecionar o Audio que deseja conversar! Ambiente disponivel 24x7. Running on ZeroGPU with openai/whisper-large-v3 """ ) inp = gr.Audio(sources="upload", type="filepath", label="Audio file") transcribe = gr.Textbox(label="Transcricao", show_label=True, show_copy_button=True) ask_question = gr.Textbox(label="Ask a question", visible=False) response_output = gr.Textbox(label="Response", visible=False) submit_question = gr.Button("Submit question", visible=False) submit_button = gr.Button("Transcribe to Chat", variant='primary', size='sm') clear_button = gr.ClearButton([transcribe,response_output,inp, ask_question]) def ask_question_callback(transcription,question): if ask_question: response = respond_to_question_llama(transcription, question) else: response = "No question asked" return response #inp.upload(audio_transcribe, inputs=inp, outputs=[transcribe,ask_question,submit_question, response_output]) submit_button.click(audio_transcribe, inputs=inp, outputs=[transcribe,ask_question,submit_question, response_output]) submit_question.click(ask_question_callback, outputs=[response_output], inputs=[transcribe, ask_question]) clear_button.click(hidden_ask_question,outputs=[ask_question,response_output,submit_question]) transcriberUI.queue().launch()