abinayn/Audio_Analysis
0
1import gradio as gr2import assemblyai as aai3import os4import google.generativeai as genai5import time6 7# ๐ API Keys8ASSEMBLYAI_API_KEY = os.getenv("ASSEMBLYAI_API_KEY")9GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")10 11# ๐ง Setup12aai.settings.api_key = ASSEMBLYAI_API_KEY13genai.configure(api_key=GEMINI_API_KEY)14model = genai.GenerativeModel("learnlm-2.0-flash-experimental")15 16# ๐ง Prompt Template17AGENT_PROMPT = """18You will receive a transcription generated from an uploaded audio file. Based on this transcription, perform the following tasks in order and return the output in the exact format below:19 20๐น Audio Transcription:21<Original Transcription Text>22 23๐น Transcription Summary:24- Point 125- Point 226...27 28๐น Grammar Correction:29 30Identify only the sentences that are grammatically incorrect, incomplete, or unclear in the transcription.31 32For each, include:33- One sentence before (if available)34- The problematic sentence, formatted in bold italics35- One sentence after (if available)36- A suggestion that explains the type of issue and proposes a clearer alternative37 38Suggestions should:39- Be helpful and context-aware40- Vary depending on the issue (e.g., tense, clarity, structure, redundancy)41- Use natural language, not repetitive phrasing42 43Format:44 45---------------------------------------46 47Context:48 <Sentence before> 49 **_Problematic sentence_** 50 <Sentence after>51 52Suggestion: 53 <Give a short, natural explanation of the issue and propose a better version>54 55---------------------------------------56 57Only include corrections. Ignore sentences that are already correct or clear.58 59 60 61๐น Sentiment Analysis:62Sentiment: <Positive / Neutral / Negative>63Explanation: <Short Reason>64"""65 66# ๐ Main Process with Live Status67def process_audio(file, status=gr.Textbox()):68 if not file:69 return None, "", "", gr.update(visible=False)70 71 start = time.time()72 status_msg = "<p>๐ Transcription in progress...</p>"73 yield None, "", status_msg # initial status74 75 transcriber = aai.Transcriber()76 transcript = transcriber.transcribe(file)77 raw_text = transcript.text78 79 status_msg = "<p>๐ง Analyzing with LearnLM...</p>"80 yield None, "", status_msg81 82 prompt = AGENT_PROMPT.replace("<Original Transcription Text>", raw_text)83 response = model.generate_content(prompt)84 85 elapsed = round(time.time() - start, 2)86 result_html = response.text.replace("\n", "<br>")87 time_info = f"<p><b>๐ Total processing time: {elapsed} seconds</b></p>"88 89 yield result_html, time_info, "<p>โ
Done! All steps completed.</p>"90 91# โ Clear Everything92def clear_all():93 return None, "", "", "", gr.update(visible=False)94 95# ๐๏ธ Gradio UI96with gr.Blocks() as demo:97 gr.Markdown("## ๐ง Audio Intelligence App ")98 99 audio = gr.Audio(type="filepath", label="๐๏ธ Upload Audio File")100 101 with gr.Row():102 submit = gr.Button("๐ Submit", visible=False, scale=1)103 clear = gr.Button("๐งน Clear", scale=1)104 105 status_box = gr.HTML(label="๐ Status Update")106 output = gr.HTML(label="๐ง LearnLM Agent Output")107 time_display = gr.HTML()108 109 def show_submit(file): return gr.update(visible=True) if file else gr.update(visible=False)110 111 audio.change(fn=show_submit, inputs=audio, outputs=submit)112 submit.click(fn=process_audio, inputs=[audio], outputs=[output, time_display, status_box])113 clear.click(fn=clear_all, inputs=None, outputs=[audio, output, time_display, status_box, submit])114 115demo.launch()116 117 