|
|
| import pickle |
| import random |
| import re |
| import requests |
| |
| import os |
| import time |
| from datetime import datetime |
| import random |
| import json |
| import inspect |
| from datetime import datetime |
| import re |
|
|
|
|
| import os |
| |
| import asyncio |
|
|
| from litellm import completion |
| from litellm.exceptions import RateLimitError |
| import gradio as gr |
| |
|
|
| import tempfile |
|
|
| |
|
|
|
|
| |
| with open('functions.py') as f: |
| exec(f.read()) |
|
|
| with open('prompts.py') as f: |
| exec(f.read()) |
|
|
| with open('cleys.py') as f: |
| exec(f.read()) |
|
|
|
|
|
|
|
|
| def make_download_file(task, transcript, evaluation): |
| content = ( |
| "=== Generated Task =======================================================================================\n" |
| f"{task}\n\n\n\n" |
| "=== Transcribed Audio =======================================================================================\n\n" |
| f"{transcript}\n\n\n\n\n\n" |
| "=== Evaluation =================================================================================\n" |
| f"{evaluation}\n" |
| ) |
| tmp_path = tempfile.NamedTemporaryFile(delete=False, suffix=".txt").name |
| with open(tmp_path, "w", encoding="utf-8") as f: |
| f.write(content) |
| return tmp_path |
|
|
|
|
| def on_start(): |
| task_number = f'Now create a practice task number {random.randint(1, 4)}' |
| generated_task = generate_text( |
| task_number, |
| model="gemini/gemini-2.5-flash", |
| system_prompt=task_generator_prompt, |
| temperature=0.7 |
| ) |
| |
| return ( |
| generated_task, |
| gr.update(visible=True), |
| "", |
| "", |
| gr.update(visible=False), |
| gr.update(visible=False), |
| gr.update(visible=False) |
| ) |
|
|
|
|
| def on_audio(audio, current_task): |
| if not audio: |
| return "No audio received.", "", gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), None |
|
|
| if not DEEPGRAM_API_KEY: |
| return "Deepgram API key not found.", "", gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), None |
|
|
| try: |
| |
| result = transcribe_with_deepgram(DEEPGRAM_API_KEY, audio) |
| transcript = ( |
| result.get("results", {}) |
| .get("channels", [{}])[0] |
| .get("alternatives", [{}])[0] |
| .get("transcript", "") |
| ) |
| if not transcript: |
| return "No transcript found.", "", gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), None |
|
|
| |
| answer_to_eval = ( |
| f"Here is the task:\n{current_task}\n\n" |
| f"Here is the answer:\n{transcript}\n\n" |
| "Now evaluate this answer." |
| ) |
|
|
| |
| evaluation = generate_text( |
| answer_to_eval, |
| model="gemini/gemini-2.5-pro", |
| system_prompt=evaluator_prompt, |
| temperature=0.3 |
| ) |
|
|
| |
| if isinstance(evaluation, dict): |
| evaluation_text_val = ( |
| evaluation.get("choices", [{}])[0] |
| .get("message", {}) |
| .get("content", "") |
| ) |
| else: |
| evaluation_text_val = str(evaluation) |
|
|
| |
| file_path = make_download_file(current_task, transcript, evaluation_text_val) |
|
|
| |
| return ( |
| transcript, |
| evaluation_text_val, |
| gr.update(visible=False), |
| gr.update(visible=True), |
| gr.update(visible=False), |
| gr.update(value=file_path, visible=True) |
| ) |
|
|
| except Exception as e: |
| return f"Transcription error: {e}", "", gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), None |
|
|
|
|
| def on_reset(): |
| return ( |
| "", |
| gr.update(visible=False), |
| "", |
| "", |
| gr.update(visible=True), |
| gr.update(visible=False), |
| gr.update(visible=False) |
| ) |
|
|
|
|
| with gr.Blocks() as demo: |
| |
| output_text = gr.Markdown(value="### 📘 Generated Task\n") |
| transcript_text = gr.Textbox(label="Transcribed Audio", interactive=False) |
| evaluation_text = gr.Markdown(value="### ✅ Evaluation\n") |
|
|
| audio_input = gr.Audio( |
| sources=["microphone", "upload"], |
| type="filepath", |
| label="Record or Upload Audio", |
| visible=False |
| ) |
|
|
| start_btn = gr.Button("Start", visible=True) |
| reset_btn = gr.Button("Reset", visible=False) |
|
|
| download_file = gr.File( |
| label="Download Your Results", |
| visible=False, |
| file_types=[".txt"] |
| ) |
|
|
| |
| start_btn.click( |
| fn=on_start, |
| inputs=[], |
| outputs=[output_text, audio_input, transcript_text, evaluation_text, start_btn, reset_btn, download_file], |
| ) |
|
|
| |
| audio_input.change( |
| fn=on_audio, |
| inputs=[audio_input, output_text], |
| outputs=[transcript_text, evaluation_text, audio_input, reset_btn, start_btn, download_file] |
| ) |
|
|
| |
| reset_btn.click( |
| fn=on_reset, |
| inputs=[], |
| outputs=[output_text, audio_input, transcript_text, evaluation_text, start_btn, reset_btn, download_file] |
| ) |
|
|
| demo.launch() |