File size: 3,245 Bytes
adb964a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import torch
import gradio as gr
from transformers import pipeline
import os
import pandas as pd  # Importa o Pandas
import json  # Importa o módulo json para ler o arquivo de palavras-chave


MODEL_NAME = "openai/whisper-large-v2"
BATCH_SIZE = 8

device = 0 if torch.cuda.is_available() else "cpu"

pipe = pipeline(
    task="automatic-speech-recognition",
    model=MODEL_NAME,
    chunk_length_s=30,
    device=device,
)

# Função para carregar palavras-chave do arquivo JSON
def load_keywords(json_path):
    with open(json_path, 'r') as file:
        data = json.load(file)
    keywords = data.get('keywords', []) + data.get('key_words_Telus', [])
    return keywords

# Caminho para o arquivo JSON de palavras-chave
keywords_path = os.path.join(os.path.dirname(__file__), "keywords.json")
keywords = load_keywords(keywords_path)

def transcribe(inputs, task):
    if inputs is None:
        raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
    
    result = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)
    text = result["text"]
    
    # Identifica as palavras-chave na transcrição
    found_keywords = [keyword for keyword in keywords if keyword.lower() in text.lower()]
    
    # Cria um DataFrame para armazenar os dados
    df = pd.DataFrame({'Transcription': [text], 'Found Keywords': [", ".join(found_keywords)]})
    
    # Define o caminho onde o arquivo CSV será salvo
    csv_save_path = os.path.join(os.path.dirname(__file__), "transcription.csv")
    # Salva o DataFrame como um arquivo CSV
    df.to_csv(csv_save_path, index=False)
    
    # Retorna o texto da transcrição, as palavras-chave encontradas e o caminho do arquivo CSV
    return text, ", ".join(found_keywords), csv_save_path

# Ajusta as interfaces Gradio para incluir a saída das palavras-chave encontradas
mf_transcribe = gr.Interface(
    fn=transcribe,
    inputs=[
        gr.Audio(type="filepath"),
        gr.Radio(choices=["transcribe", "translate"], label="Task"),
    ],
    outputs=[
        gr.Textbox(label="Transcription Text"),
        gr.Textbox(label="Found Keywords", visible=False),  # Oculta para a aba de microfone
        gr.File(label="Download CSV File", visible=False),  # Oculta o download do CSV para a aba de microfone
    ],
    title="GexelView.AI - Transcriber",
    description="Transcribe or translate audio inputs with a single click.",
    allow_flagging="never",
)

file_transcribe = gr.Interface(
    fn=transcribe,
    inputs=[
        gr.Audio(type="filepath", label="Audio file"),
        gr.Radio(choices=["transcribe", "translate"], label="Task"),
    ],
    outputs=[
        gr.Textbox(label="Transcription Text"),
        gr.Textbox(label="Found Keywords"),  # Exibe as palavras-chave encontradas
        gr.File(label="Download CSV File"),  # Torna o download do arquivo CSV visível para a aba de arquivo
    ],
    title="GexelView.AI - Transcriber",
    description="Transcribe or translate audio files with a single click.",
    allow_flagging="never",
)

demo = gr.Blocks()

with demo:
    gr.TabbedInterface([mf_transcribe, file_transcribe], ["Microphone", "Audio file"])

demo.launch()