auda / app.py
bai4578's picture
Update app.py
e50b582 verified
Raw
History Blame Contribute Delete
14.7 kB
import os
import tempfile
import shutil
from groq import Groq
import gradio as gr
from pydub import AudioSegment
import re
# Initialize the Groq client
client = Groq(api_key=os.environ["keko"])
# def process_audio(audio_file):
# if audio_file is None:
# return None, None, "No audio file provided."
# # Convert to MP3 if not already in MP3 format
# audio = AudioSegment.from_file(audio_file)
# mp3_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
# audio.export(mp3_file.name, format="mp3")
# return mp3_file.name, mp3_file.name, "Audio processed successfully."
# def transcribe_audio(audio_file):
# if audio_file is None:
# return "No audio file provided."
# # Create a transcription of the audio file
# with open(audio_file, "rb") as file:
# transcription = client.audio.transcriptions.create(
# file=(audio_file, file.read()),
# model="whisper-large-v3",
# prompt="fix this dictated Text contains gastroenterology terms. remove words such as commas, newline, periods and replace with appropriate punctuations. apply corrections as specified by the user. keep format, minimal necessary changes only",
# response_format="json",
# temperature=0.0
# )
# return transcription.text
# # Create the Gradio interface using Blocks
# with gr.Blocks(title="Audio Recorder and Transcriber") as demo:
# gr.Markdown("# Audio Recorder and Transcriber")
# gr.Markdown("Record audio or upload a file. You can download the audio or transcribe it.")
# audio_state = gr.State(None)
# with gr.Row():
# audio_input = gr.Audio(sources=["microphone", "upload"], type="filepath", label="Record or Upload Audio")
# with gr.Row():
# process_btn = gr.Button("Process Audio")
# transcribe_btn = gr.Button("Transcribe Audio")
# with gr.Row():
# audio_output = gr.Audio(label="Processed Audio (MP3)", format="mp3")
# process_msg = gr.Textbox(label="Process Status")
# transcription_output = gr.Textbox(label="Transcription", show_copy_button=True)
# def update_audio_state(audio):
# return audio if audio else None
# audio_input.change(
# update_audio_state,
# inputs=[audio_input],
# outputs=[audio_state]
# )
# process_btn.click(
# process_audio,
# inputs=[audio_state],
# outputs=[audio_state, audio_output, process_msg]
# )
# transcribe_btn.click(
# transcribe_audio,
# inputs=[audio_state],
# outputs=[transcription_output]
# )
# # Launch the interface
# demo.launch()
### original: #########################
# def save_audio(audio_file):
# if audio_file is None:
# return None, "No audio file provided."
# # Save the audio file as MP3
# mp3_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
# shutil.copy2(audio_file, mp3_file.name)
# return mp3_file.name, "Audio saved successfully."
# def transcribe_audio(audio_file):
# if audio_file is None:
# return "No audio file provided."
# # Create a transcription of the audio file
# with open(audio_file, "rb") as file:
# transcription = client.audio.transcriptions.create(
# file=(audio_file, file.read()),
# model="whisper-large-v3",
# prompt="text may contain medical gastroenterology terms",
# response_format="json",
# temperature=0.0
# )
# return transcription.text
# # Create the Gradio interface using Blocks
# with gr.Blocks(title="Audio Recorder and Transcriber") as demo:
# gr.Markdown("# Audio Recorder and Transcriber")
# gr.Markdown("Record audio or upload a file. You can download the audio or transcribe it.")
# with gr.Row():
# audio_input = gr.Audio(sources=["microphone", "upload"], type="filepath", label="Record or Upload Audio")
# with gr.Row():
# save_btn = gr.Button("Save Audio")
# transcribe_btn = gr.Button("Transcribe Audio")
# with gr.Row():
# audio_output = gr.Audio(label="Saved Audio (MP3)", format="mp3")
# save_msg = gr.Textbox(label="Save Status")
# transcription_output = gr.Textbox(label="Transcription", show_copy_button=True)
# save_btn.click(
# save_audio,
# inputs=[audio_input],
# outputs=[audio_output, save_msg]
# )
# transcribe_btn.click(
# transcribe_audio,
# inputs=[audio_input],
# outputs=[transcription_output]
# )
# # Launch the interface
# demo.launch()
#### trial send to LLM
#########################
import gradio as gr
import os
from groq import Groq
import tempfile
import shutil
# Initialize the Groq client
client = Groq(api_key=os.environ["keko"])
def check_password(password):
correct_password = "zoo" # Set your desired password here
return password == correct_password
def save_audio(audio_file):
if audio_file is None:
return None, "No audio file provided."
mp3_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
shutil.copy2(audio_file, mp3_file.name)
return mp3_file.name, "Audio saved successfully."
def split_audio(audio_file, chunk_size=25*1024*1024, overlap=10000):
audio = AudioSegment.from_file(audio_file)
duration = len(audio)
chunks = []
start = 0
while start < duration:
end = start + chunk_size
if end > duration:
end = duration
chunk = audio[start:end]
if len(chunk.raw_data) > chunk_size:
end = start + (chunk_size // chunk.frame_width) * chunk.frame_width
chunk = audio[start:end]
chunks.append(chunk)
start = end - overlap
return chunks
def transcribe_audio(audio_file):
if audio_file is None:
return "No audio file provided."
file_size = os.path.getsize(audio_file)
max_size = 25 * 1024 * 1024 # 25 MB in bytes
if file_size <= max_size:
# If the file is small enough, process it directly
with open(audio_file, "rb") as file:
transcription = client.audio.transcriptions.create(
file=(audio_file, file.read()),
model="whisper-large-v3",
prompt="fix this dictated Text contains gastroenterology terms. remove words such as commas, newline, periods and replace with appropriate punctuations. apply corrections as specified by the user. keep format, minimal necessary changes only",
response_format="json",
temperature=0.0
)
return transcription.text
else:
# If the file is too large, split it and process chunks
chunks = split_audio(audio_file)
transcriptions = []
for i, chunk in enumerate(chunks):
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
chunk.export(temp_file.name, format="mp3")
with open(temp_file.name, "rb") as file:
transcription = client.audio.transcriptions.create(
file=(f"chunk_{i}.mp3", file.read()),
model="whisper-large-v3",
prompt="fix this dictated Text contains gastroenterology terms. ",
response_format="json",
temperature=0.0
)
transcriptions.append(transcription.text)
os.unlink(temp_file.name)
return " ".join(transcriptions)
## function to parse text into dictionary of prompts
def parse_text_to_dict(text):
sections = re.split(r"\n#/\s*", text.strip()) # Split only at "#/"
parsed_dict = {}
for section in sections:
if not section.strip(): # Skip empty sections
continue
lines = section.split("\n", 1) # Split into title and content
title = lines[0].strip().lstrip("#/ ") # Ensure "#/" is removed
content = lines[1].strip() if len(lines) > 1 else "" # Remaining text is the value
parsed_dict[title] = content
return parsed_dict
# load the prompts:
with open("primpts.txt", "r", encoding="utf-8") as file:
contentt = file.read()
contentt = parse_text_to_dict(contentt)
def generate_text(text, prompt_type):
if not text:
return "No text provided."
prompts = contentt
selected_prompt = prompts.get(prompt_type, "You are a helpful assistant. Process the following text.")
response = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[
{"role": "system", "content": selected_prompt},
{"role": "user", "content": text}
],
max_tokens=4000,
temperature=0.1
)
return response.choices[0].message.content
# Define the new function for "Transcribe & Fix"
def transcribe_and_generate(audio_file, prompt_type):
# Transcribe the audio
transcription = transcribe_audio(audio_file)
# Generate the text based on the transcription and selected prompt
generated_text = generate_text(transcription, prompt_type)
return transcription, generated_text
# Define a new function that combines save, transcribe, and fix
def save_transcribe_fix(audio_file, prompt_type):
# Step 1: Save the audio
saved_audio, save_status = save_audio(audio_file)
if not saved_audio:
return None, save_status, None # Return error message if saving fails
# Step 2: Transcribe the audio
transcription = transcribe_audio(saved_audio)
# Step 3: Generate the fixed text
fixed_text = generate_text(transcription, prompt_type)
return saved_audio, transcription, fixed_text
# ------------------------------------------------
# Create the Gradio interface using Blocks
# -------------------------------------------------
# Update Gradio UI with a new button
with gr.Blocks(title="Audio Recorder, Transcriber, and Text Generator") as demo:
gr.Markdown("# Audio Recorder, Transcriber, and Text Generator")
gr.Markdown("Enter the correct password to access the application.")
with gr.Group() as login_container:
password_input = gr.Textbox(type="password", label="Enter Password")
login_button = gr.Button("Login")
login_message = gr.Markdown()
with gr.Column(visible=False) as main_interface:
gr.Markdown("Record audio, transcribe it, or enter text manually, and optionally generate text based on the input.")
input_toggle = gr.Checkbox(label="Use Manual Text Input Instead of Audio", value=False)
# Audio section including transcription output
with gr.Column() as audio_section:
audio_input = gr.Audio(sources=["microphone", "upload"], type="filepath", label="Record or Upload Audio")
with gr.Row():
save_btn = gr.Button("Save Audio")
transcribe_btn = gr.Button("Transcribe Audio")
transcribe_fix_btn = gr.Button("Transcribe & Fix")
save_transcribe_fix_btn = gr.Button("Save, Transcribe & Fix")
with gr.Row():
audio_output = gr.Audio(label="Saved Audio (MP3)", format="mp3")
save_msg = gr.Textbox(label="Save Status")
transcription_output = gr.Textbox(label="Transcription", show_copy_button=True)
# Text input section (starts hidden)
text_input = gr.Textbox(label="Enter Text Manually", visible=False)
# Toggle input sources (audio or text)
input_toggle.change(
lambda use_text: (
gr.update(visible=not use_text), # Hide audio section if using text input
gr.update(visible=use_text), # Show text input when checked
gr.update(visible=not use_text) # Hide transcription output when using text input
),
inputs=[input_toggle],
outputs=[audio_section, text_input, transcription_output]
)
with gr.Row():
prompt_type = gr.Dropdown(
choices=list(contentt.keys()), # Get dictionary keys as choices
label="Select Text Generation Type",
value=list(contentt.keys())[0] # Default to the first key
)
generate_btn = gr.Button("Generate Text")
generated_text_output = gr.Textbox(label="Generated Text", show_copy_button=True)
# Actions for audio-related inputs
save_btn.click(
save_audio,
inputs=[audio_input],
outputs=[audio_output, save_msg]
)
transcribe_btn.click(
transcribe_audio,
inputs=[audio_input],
outputs=[transcription_output]
)
transcribe_fix_btn.click(
transcribe_and_generate,
inputs=[audio_input, prompt_type],
outputs=[transcription_output, generated_text_output]
)
save_transcribe_fix_btn.click(
save_transcribe_fix,
inputs=[audio_input, prompt_type],
outputs=[audio_output, transcription_output, generated_text_output]
)
# Automatically detect if text or audio should be used
def determine_input(audio, text, prompt):
if text.strip(): # If text is entered, use it
return generate_text(text, prompt)
elif audio: # Otherwise, check if audio exists and use it
return generate_text(transcription_output.value, prompt)
else:
return "Please provide either text or audio."
generate_btn.click(
determine_input,
inputs=[audio_input, text_input, prompt_type],
outputs=[generated_text_output]
)
def login(password):
if check_password(password):
return {
login_container: gr.update(visible=False), # Hide login UI
main_interface: gr.update(visible=True), # Show main interface
login_message: gr.update(value="Login successful.", visible=True)
}
else:
return {
login_message: gr.update(value="Incorrect password. Please try again.", visible=True)
}
login_button.click(
login,
inputs=[password_input],
outputs=[login_container, main_interface, login_message]
)
demo.launch(share=True)