import gradio as gr import requests import json import os def send_req(title, audio_file_path): url = "https://dev-phonic-api.vuihoc.vn/api/v3/get_score_from_file" payload = {'title': title} files=[ ('audio',('temp.wav',open(audio_file_path,'rb'),'audio/wav')) ] headers = { 'accept': 'application/json', 'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ODg2MSwiZGV2aWNlSWQiOjg4NjEsImlhdCI6MTcxMTk2MTY2OSwiZXhwIjoxNzE3MTQ1NjY5fQ.8Wtzyx3mKVh7K9_GNzseWdK1NH-hycYdNh1uFsoVvEg' } response = requests.request("POST", url, headers=headers, data=payload, files=files) return response.json() def create_html_output(real_transcript, is_letter_correct_all_words): html_output = "

" for i, char in enumerate(real_transcript): if is_letter_correct_all_words[i] == '1': html_output += f"{char}" else: html_output += f"{char}" html_output += "

" return html_output def create_html_output_ipa(word_score_list): html_output = "

" for word_score in word_score_list: for phone_score in word_score["phone_score_list"]: if phone_score["quality_score"] == 100: html_output += f"{phone_score['phone_ipa']}" else: html_output += f"{phone_score['phone_ipa']}" html_output += " " html_output += "

" return html_output def download_audio_file(url, filename=None): """ Tải xuống tệp âm thanh từ một URL và lưu nó vào đĩa. Args: url (str): URL của tệp âm thanh cần tải xuống. filename (str, optional): Tên tệp để lưu (mặc định là tên tệp từ URL). """ response = requests.get(url) response.raise_for_status() # Kiểm tra lỗi HTTP # Nếu không cung cấp tên tệp, sử dụng tên tệp từ URL if not filename: filename = url.split("/")[-1] with open(filename, "wb") as f: f.write(response.content) return filename def pa_check(url_audio, microphone, file_upload, reference_text): if url_audio: file = download_audio_file(url_audio) else: if (microphone is not None) and (file_upload is not None): warn_output = ( "WARNING: You've uploaded an audio file and used the microphone. " "The recorded file from the microphone will be used and the uploaded audio will be discarded.\n" ) elif (microphone is None) and (file_upload is None): return "ERROR: You have to either use the microphone or upload an audio file" file = microphone if microphone is not None else file_upload result = send_req(reference_text, file) try: html_output = create_html_output(result["data"]["real_transcripts"], result["data"]["is_letter_correct_all_words"]) html_output_ipa = create_html_output_ipa(result["data"]["word_score_list"]) except Exception as e: print(e) print(result["data"]["real_transcripts"]) print(result["data"]["is_letter_correct_all_words"]) html_output = "ERROR: Something went wrong with the server response. Please try again later." return json.dumps(result, indent=4, ensure_ascii=False), html_output, html_output_ipa demo = gr.Interface( fn=pa_check, inputs=[ gr.Textbox(label="Url audio", type="text", placeholder="Download audio form url"), gr.Audio(sources="microphone", type="filepath"), gr.Audio(sources="upload", type="filepath"), gr.Textbox(label="Reference text", type="text", placeholder="How are you?|What is your name?"), ], outputs=[ gr.Textbox(label="Output"), "html", "html" ], theme="huggingface", title="Pronunciation Assessment", allow_flagging="never" ) demo.launch(auth=(os.environ['username'], os.environ['password']))