tiennguyenbnbk commited on
Commit
030ab41
·
verified ·
1 Parent(s): 1fdf480

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -0
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+ import os
5
+
6
+ def send_req(title, audio_file_path):
7
+ url = "http://https://dev-phonic-api.vuihoc.vn/api/v3/get_score_from_file"
8
+
9
+ payload = {'title': title}
10
+ files=[
11
+ ('audio',('temp.wav',open(audio_file_path,'rb'),'audio/wav'))
12
+ ]
13
+ headers = {
14
+ 'accept': 'application/json',
15
+ 'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ODg2MSwiZGV2aWNlSWQiOjg4NjEsImlhdCI6MTcxMTk2MTY2OSwiZXhwIjoxNzE3MTQ1NjY5fQ.8Wtzyx3mKVh7K9_GNzseWdK1NH-hycYdNh1uFsoVvEg'
16
+ }
17
+
18
+ response = requests.request("POST", url, headers=headers, data=payload, files=files)
19
+
20
+ return response.json()
21
+
22
+ def create_html_output(real_transcript, is_letter_correct_all_words):
23
+ html_output = "<center><h1>"
24
+ for i, char in enumerate(real_transcript):
25
+ if is_letter_correct_all_words[i] == '1':
26
+ html_output += f"<span style='color:green;'>{char}</span>"
27
+ else:
28
+ html_output += f"<span style='color:red;'>{char}</span>"
29
+ html_output += "</h1></center>"
30
+ return html_output
31
+
32
+ def create_html_output_ipa(word_score_list):
33
+ html_output = "<center><h1>"
34
+ for word_score in word_score_list:
35
+ for phone_score in word_score["phone_score_list"]:
36
+ if phone_score["quality_score"] == 100:
37
+ html_output += f"<span style='color:green;'>{phone_score['phone_ipa']}</span>"
38
+ else:
39
+ html_output += f"<span style='color:red;'>{phone_score['phone_ipa']}</span>"
40
+ html_output += "<span style='color:red;'> </span>"
41
+ html_output += "</h1></center>"
42
+ return html_output
43
+
44
+ def download_audio_file(url, filename=None):
45
+ """
46
+ Tải xuống tệp âm thanh từ một URL và lưu nó vào đĩa.
47
+
48
+ Args:
49
+ url (str): URL của tệp âm thanh cần tải xuống.
50
+ filename (str, optional): Tên tệp để lưu (mặc định là tên tệp từ URL).
51
+ """
52
+
53
+
54
+ response = requests.get(url)
55
+ response.raise_for_status() # Kiểm tra lỗi HTTP
56
+
57
+ # Nếu không cung cấp tên tệp, sử dụng tên tệp từ URL
58
+ if not filename:
59
+ filename = url.split("/")[-1]
60
+
61
+ with open(filename, "wb") as f:
62
+ f.write(response.content)
63
+
64
+
65
+ def pa_check(microphone, file_upload, reference_text):
66
+ if (microphone is not None) and (file_upload is not None):
67
+ warn_output = (
68
+ "WARNING: You've uploaded an audio file and used the microphone. "
69
+ "The recorded file from the microphone will be used and the uploaded audio will be discarded.\n"
70
+ )
71
+
72
+ elif (microphone is None) and (file_upload is None):
73
+ return "ERROR: You have to either use the microphone or upload an audio file"
74
+
75
+ file = microphone if microphone is not None else file_upload
76
+
77
+ result = send_req(reference_text, file)
78
+ try:
79
+
80
+ html_output = create_html_output(result["data"]["real_transcripts"], result["data"]["is_letter_correct_all_words"])
81
+ html_output_ipa = create_html_output_ipa(result["data"]["word_score_list"])
82
+ except Exception as e:
83
+ print(e)
84
+ print(result["data"]["real_transcripts"])
85
+ print(result["data"]["is_letter_correct_all_words"])
86
+ html_output = "ERROR: Something went wrong with the server response. Please try again later."
87
+
88
+
89
+ return json.dumps(result, indent=4, ensure_ascii=False), html_output, html_output_ipa
90
+
91
+
92
+ demo = gr.Interface(
93
+ fn=pa_check,
94
+ inputs=[
95
+ # gr.Textbox(label="Url audio", type="text", placeholder="Download audio form url"),
96
+ gr.Audio(sources="microphone", type="filepath"),
97
+ gr.Audio(sources="upload", type="filepath"),
98
+ gr.Textbox(label="Reference text", type="text", placeholder="How are you?|What is your name?"),
99
+ ],
100
+ outputs=[
101
+ gr.Textbox(label="Output"),
102
+ "html",
103
+ "html"
104
+ ],
105
+ theme="huggingface",
106
+ title="Pronunciation Assessment",
107
+ allow_flagging="never",
108
+ examples=[[None, None, "How are you?|What is your name?"], [None, None, "It's at quarter past nine.|It's at nine fifteen."]]
109
+ )
110
+
111
+ demo.launch(auth=(os.environ['username'], os.environ['password']))
112
+
113
+