import gradio as gr
from faster_whisper import WhisperModel
import subprocess
import threading
import time
import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
# Load the Whisper model once
model_size = "large-v2"
model = WhisperModel(model_size, device="cuda", compute_type="float16") # or int8 for lower memory
def transcribe_audio(audio_file):
# Transcribe the audio file
segments, info = model.transcribe(audio_file, beam_size=5)
# Collect the transcription
transcription = ""
for segment in segments:
transcription += segment.text + " "
return transcription.strip()
def ollama_generate(prompt):
process = subprocess.Popen(['ollama', 'generate', '--model', 'llama3.1'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
output, error = process.communicate(prompt)
return output
def process_audio(audio_file):
transcription = transcribe_audio(audio_file)
# Now pass the transcription to Ollama to get a summary
summary_prompt = f"Please provide a summary of the following transcription:\n\n{transcription}"
summary = ollama_generate(summary_prompt)
return transcription, summary
def transcribe_live_audio(audio_file):
# Transcribe the audio file
transcription = transcribe_audio(audio_file)
return transcription
def assistant_action(audio_file):
# Transcribe the last minute of audio
# For simplicity, we use the entire transcription
transcription = transcribe_audio(audio_file)
prompt = f"I am in class right now and I want you to generate answers for the question the teacher asked or generate an overview of the current topic based on the following transcription:\n\n{transcription}"
response = ollama_generate(prompt)
return response
def chat_with_assistant(question, transcription):
prompt = f"{transcription}\n\nBased on the above transcription, please answer the following question:\n{question}"
response = ollama_generate(prompt)
return response
# Gradio interface
with gr.Blocks() as demo:
with gr.Tab("Record and Summarize"):
gr.Markdown("### Record Audio and Get Summary")
audio_input = gr.Audio(source="microphone", type="filepath", label="Record Audio")
process_button = gr.Button("Process Recording")
transcription_output = gr.Textbox(label="Transcription")
summary_output = gr.Textbox(label="Summary")
process_button.click(process_audio, inputs=audio_input, outputs=[transcription_output, summary_output])
with gr.Tab("Live Transcription and Assistant"):
gr.Markdown("### Live Transcription and Assistant")
live_audio_input = gr.Audio(source="microphone", type="filepath", label="Live Audio Recording", interactive=True)
live_transcription_output = gr.Textbox(label="Live Transcription")
assistant_button = gr.Button("Assist")
assistant_output = gr.Textbox(label="Assistant Response")
chatbox = gr.Textbox(label="Ask a Question")
chat_submit = gr.Button("Send")
chat_response = gr.Textbox(label="Assistant's Answer")
live_audio_input.change(transcribe_live_audio, inputs=live_audio_input, outputs=live_transcription_output)
assistant_button.click(assistant_action, inputs=live_audio_input, outputs=assistant_output)
chat_submit.click(chat_with_assistant, inputs=[chatbox, live_transcription_output], outputs=chat_response)
demo.launch()
This code creates a Gradio interface with two tabs:
First Tab: "Record and Summarize"
- Record Audio: Use the microphone to record audio.
- Process Recording: Button to process the recorded audio.
- Transcription: Displays the transcription of the audio using
faster_whisper.
- Summary: Displays a summary generated by passing the transcription to
ollama.
Second Tab: "Live Transcription and Assistant"
- Live Audio Recording: Use the microphone to record longer audio (simulate live recording).
- Live Transcription: Displays the transcription of the audio.
- Assist: Button that, when clicked, processes the last portion of the transcription and asks
ollama to generate answers or an overview based on a prompt.
- Assistant Response: Displays the response from
ollama.
- Ask a Question: Textbox to input a specific question.
- Send: Button to send the question.
- Assistant's Answer: Displays the answer generated by
ollama based on the transcription context.
Notes:
- Transcription: Uses the
transcribe_audio function, which utilizes faster_whisper to transcribe audio files.
- Ollama Integration: Uses the
ollama_generate function to interact with the ollama CLI tool (assuming it's installed and configured on your system).
- Live Transcription Simulation: Due to technical limitations, true live transcription (processing audio every 30 seconds without interruption) is simulated by transcribing the audio whenever it's updated.
Requirements:
gradio
faster_whisper
ollama installed and accessible via the command line.
- CUDA-compatible GPU for faster transcription with
faster_whisper (or adjust the code to run on CPU if necessary).
How to Run the Code:
- Install the required packages:
pip install gradio faster-whisper
- Ensure that
ollama is installed and configured on your machine.
- Run the script:
python your_script_name.py
- Access the Gradio interface in your web browser at the URL provided in the terminal.
Disclaimer:
- Implementing continuous live recording and transcription with periodic updates (every 30 seconds) is complex due to browser and Gradio limitations.
- This code simulates live transcription by updating the transcription whenever the audio input changes.
- For actual live transcription, you may need to develop a custom frontend or use specialized tools that support streaming audio and real-time transcription.