import torch from transformers import pipeline from transformers.pipelines.audio_utils import ffmpeg_read import gradio as gr MODEL_NAME = "seiching/whisper-small-seiching" #MODEL_NAME = "openai/whisper-small" 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, ) from openai import OpenAI from concurrent.futures import ThreadPoolExecutor import tiktoken def call_openai_api(openaiobj,transcription): response = openaiobj.chat.completions.create( model="gpt-3.5-turbo", temperature=0, messages=[ { "role": "system", "content": "你是專業的會議紀錄製作員,請根據由語音辨識軟體將會議錄音所轉錄的逐字稿,也請注意逐字稿可能有錯,請以條列式,列出討論事項及結論,討論內容細節請略過,要用比較正式及容易閱讀的寫法,避免口語化" }, { "role": "user", "content": transcription } ] ) return response.choices[0].message.content def split_into_chunks(text, tokens=500): encoding = tiktoken.encoding_for_model('gpt-3.5-turbo') words = encoding.encode(text) chunks = [] for i in range(0, len(words), tokens): chunks.append(' '.join(encoding.decode(words[i:i + tokens]))) return chunks def process_chunks(openaikeystr,inputtext): # openaiobj = OpenAI( # # This is the default and can be omitted # api_key=openaikeystr, # ) #openaiojb =OpenAI(base_url="http://localhost:1234/v1", api_key="not-needed") openaiojb =OpenAI( api_key=openaikeystr) text = inputtext #openaikey.set_key(openaikeystr) #print('process_chunk',openaikey.get_key()) chunks = split_into_chunks(text) response='' for chunk in chunks: response=response+call_openai_api(openaiobj,chunk) return response # # Processes chunks in parallel # with ThreadPoolExecutor() as executor: # responses = list(executor.map(call_openai_api, [openaiobj,chunks])) # return responses import torch from transformers import pipeline from transformers.pipelines.audio_utils import ffmpeg_read import gradio as gr MODEL_NAME = "seiching/whisper-small-seiching" BATCH_SIZE = 8 transcribe_text="this is a test" device = 0 if torch.cuda.is_available() else "cpu" pipe = pipeline( task="automatic-speech-recognition", model=MODEL_NAME, chunk_length_s=30, device=device, ) # Copied from https://github.com/openai/whisper/blob/c09a7ae299c4c34c5839a76380ae407e7d785914/whisper/utils.py#L50 def format_timestamp(seconds: float, always_include_hours: bool = False, decimal_marker: str = "."): if seconds is not None: milliseconds = round(seconds * 1000.0) hours = milliseconds // 3_600_000 milliseconds -= hours * 3_600_000 minutes = milliseconds // 60_000 milliseconds -= minutes * 60_000 seconds = milliseconds // 1_000 milliseconds -= seconds * 1_000 hours_marker = f"{hours:02d}:" if always_include_hours or hours > 0 else "" return f"{hours_marker}{minutes:02d}:{seconds:02d}{decimal_marker}{milliseconds:03d}" else: # we have a malformed timestamp so just return it as is return seconds def transcribe(file, task, return_timestamps): outputs = pipe(file, batch_size=BATCH_SIZE, generate_kwargs={"task": task,"language": "chinese",}, return_timestamps=return_timestamps) text = outputs["text"] if return_timestamps: timestamps = outputs["chunks"] timestamps = [ f"[{format_timestamp(chunk['timestamp'][0])} -> {format_timestamp(chunk['timestamp'][1])}] {chunk['text']}" for chunk in timestamps ] text = "\n".join(str(feature) for feature in timestamps) global transcribe_text transcribe_text=text # with open('asr_resul.txt', 'w') as f: # f.write(text) # ainotes=process_chunks(text) # with open("ainotes_result.txt", "a") as f: # f.write(ainotes) return text demo = gr.Blocks() mic_transcribe = gr.Interface( fn=transcribe, inputs=[ gr.inputs.Audio(source="microphone", type="filepath", optional=True), gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"), gr.inputs.Checkbox(default=False, label="Return timestamps"), ], outputs="text", layout="horizontal", theme="huggingface", title="會議紀錄小幫手AINotes", description=( "可由麥克風錄音或上傳語音檔" f" 使用這個模型 [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) 先做語音辨識再做會議紀錄摘要" " 長度沒有限制" ), allow_flagging="never", ) file_transcribe = gr.Interface( fn=transcribe, inputs=[ gr.inputs.Audio(source="upload", optional=True, label="Audio file", type="filepath"), gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"), gr.inputs.Checkbox(default=False, label="Return timestamps"), ], outputs="text", layout="horizontal", theme="huggingface", title="會議紀錄小幫手AINotes", description=( "可由麥克風錄音或上傳語音檔" f" 使用這個模型 [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) 先做語音辨識再做會議紀錄摘要" " 長度沒有限制" ), # examples=[ # ["./example.flac", "transcribe", False], # ["./example.flac", "transcribe", True], # ], cache_examples=True, allow_flagging="never", ) def writenotes( apikeystr,inputscript): #text=transcribe_text #openaikey.set_key(inputkey) #openaikey = OpenAIKeyClass(inputkey) print('ok') if len(inputscript)>10: transcribe_text=inputscript ainotestext=process_chunks(apikeystr,transcribe_text) # ainotestext=inputscript #ainotestext="" # with open('asr_resul.txt', 'w') as f: # #print(transcribe_text) # # f.write(inputkey) # f.write(transcribe_text) # with open('ainotes.txt','w') as f: # f.write(ainotestext) return ainotestext ainotes = gr.Interface( fn=writenotes, inputs=[gr.Textbox(label="OPEN AI API KEY",placeholder="請輸入sk..."),gr.Textbox(label="逐字稿",placeholder="請輸入逐字稿")], outputs="text", layout="horizontal", theme="huggingface", title="會議紀錄小幫手AINotes", description=( "可由麥克風錄音或上傳語音檔" f" 使用這個模型 [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) 先做語音辨識再做會議紀錄摘要" " 長度沒有限制" ), # examples=[ # ["./example.flac", "transcribe", False], # ["./example.flac", "transcribe", True], # ], cache_examples=True, allow_flagging="never", ) with demo: gr.TabbedInterface([file_transcribe,mic_transcribe,ainotes], ["語音檔辨識","麥克風語音檔辨識","產生會議紀錄" ]) demo.launch(enable_queue=True)