| import os |
| import tempfile |
| import shutil |
| from groq import Groq |
| import gradio as gr |
| from pydub import AudioSegment |
| import re |
|
|
|
|
| |
| client = Groq(api_key=os.environ["keko"]) |
|
|
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
| |
|
|
| import gradio as gr |
| import os |
| from groq import Groq |
| import tempfile |
| import shutil |
|
|
| |
| client = Groq(api_key=os.environ["keko"]) |
|
|
| def check_password(password): |
| correct_password = "zoo" |
| 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 |
|
|
| if file_size <= max_size: |
| |
| 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: |
| |
| 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) |
|
|
| |
|
|
|
|
|
|
|
|
|
|
| |
| def parse_text_to_dict(text): |
| sections = re.split(r"\n#/\s*", text.strip()) |
| parsed_dict = {} |
|
|
| for section in sections: |
| if not section.strip(): |
| continue |
| lines = section.split("\n", 1) |
| title = lines[0].strip().lstrip("#/ ") |
| content = lines[1].strip() if len(lines) > 1 else "" |
| parsed_dict[title] = content |
|
|
| return parsed_dict |
|
|
| |
|
|
| 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 |
|
|
|
|
| |
| def transcribe_and_generate(audio_file, prompt_type): |
| |
| transcription = transcribe_audio(audio_file) |
| |
| |
| generated_text = generate_text(transcription, prompt_type) |
| |
| return transcription, generated_text |
|
|
| |
| def save_transcribe_fix(audio_file, prompt_type): |
| |
| saved_audio, save_status = save_audio(audio_file) |
| if not saved_audio: |
| return None, save_status, None |
|
|
| |
| transcription = transcribe_audio(saved_audio) |
| |
| |
| fixed_text = generate_text(transcription, prompt_type) |
| |
| return saved_audio, transcription, fixed_text |
|
|
|
|
| |
| |
| |
| |
| 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) |
|
|
| |
| 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 = gr.Textbox(label="Enter Text Manually", visible=False) |
|
|
| |
| input_toggle.change( |
| lambda use_text: ( |
| gr.update(visible=not use_text), |
| gr.update(visible=use_text), |
| gr.update(visible=not use_text) |
| ), |
| inputs=[input_toggle], |
| outputs=[audio_section, text_input, transcription_output] |
| ) |
|
|
| with gr.Row(): |
| prompt_type = gr.Dropdown( |
| choices=list(contentt.keys()), |
| label="Select Text Generation Type", |
| value=list(contentt.keys())[0] |
| ) |
| generate_btn = gr.Button("Generate Text") |
|
|
| generated_text_output = gr.Textbox(label="Generated Text", 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] |
| ) |
| |
| 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] |
| ) |
|
|
| |
| def determine_input(audio, text, prompt): |
| if text.strip(): |
| return generate_text(text, prompt) |
| elif audio: |
| 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), |
| main_interface: gr.update(visible=True), |
| 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) |