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etime
|
4 |
+
import json
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
import os
|
7 |
+
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
API_BASE_URL = os.getenv("API_BASE_URL","http://localhost:8000")
|
11 |
+
|
12 |
+
class AudioState:
|
13 |
+
def __init__(self):
|
14 |
+
self.current_audio_id = None
|
15 |
+
self.current_transcription = None
|
16 |
+
self.audio_data = None
|
17 |
+
|
18 |
+
state = AudioState()
|
19 |
+
|
20 |
+
def upload_audio(audio_path, language_code):
|
21 |
+
if audio_path is None:
|
22 |
+
return None, "Please record or upload an audio file.", None, None
|
23 |
+
|
24 |
+
try:
|
25 |
+
with open(audio_path, 'rb') as audio_file:
|
26 |
+
files = {
|
27 |
+
'file': ('audio.wav', audio_file, 'audio/wav')
|
28 |
+
}
|
29 |
+
data = {
|
30 |
+
'language_code': language_code
|
31 |
+
}
|
32 |
+
|
33 |
+
response = requests.post(f"{API_BASE_URL}/upload/", files=files, data=data)
|
34 |
+
response.raise_for_status()
|
35 |
+
audio_data = response.json()
|
36 |
+
|
37 |
+
# Store the audio data in state
|
38 |
+
state.current_audio_id = audio_data.get('id')
|
39 |
+
state.current_transcription = audio_data.get('original_transcribed_text', '')
|
40 |
+
state.audio_data = audio_data
|
41 |
+
|
42 |
+
status = f"""Upload successful!
|
43 |
+
Audio ID: {state.current_audio_id}
|
44 |
+
Transcription completed: Yes"""
|
45 |
+
|
46 |
+
return (
|
47 |
+
state.current_transcription,
|
48 |
+
status,
|
49 |
+
state.current_transcription,
|
50 |
+
f"Ready to edit transcription for Audio ID: {state.current_audio_id}"
|
51 |
+
)
|
52 |
+
|
53 |
+
except requests.exceptions.RequestException as e:
|
54 |
+
error_msg = f"Error uploading audio: {str(e)}"
|
55 |
+
return None, error_msg, None, "Upload failed"
|
56 |
+
|
57 |
+
def get_audio_by_id(audio_id):
|
58 |
+
try:
|
59 |
+
response = requests.get(f"{API_BASE_URL}/audio/{audio_id}/")
|
60 |
+
response.raise_for_status()
|
61 |
+
audio_data = response.json()
|
62 |
+
|
63 |
+
# Update state with fetched data
|
64 |
+
state.current_audio_id = audio_data['id']
|
65 |
+
state.current_transcription = audio_data.get('original_transcribed_text', '')
|
66 |
+
state.audio_data = audio_data
|
67 |
+
|
68 |
+
return (
|
69 |
+
audio_data.get('original_transcribed_text', ''),
|
70 |
+
audio_data.get('updated_transcribed_text', ''),
|
71 |
+
f"Loaded Audio ID: {audio_id}"
|
72 |
+
)
|
73 |
+
except requests.exceptions.RequestException as e:
|
74 |
+
return None, None, f"Error loading audio: {str(e)}"
|
75 |
+
|
76 |
+
def update_transcription(transcription, audio_id=None):
|
77 |
+
# Use either provided ID or stored ID
|
78 |
+
update_id = audio_id if audio_id else state.current_audio_id
|
79 |
+
|
80 |
+
if not update_id:
|
81 |
+
return "No audio ID provided or selected."
|
82 |
+
|
83 |
+
try:
|
84 |
+
response = requests.put(
|
85 |
+
f"{API_BASE_URL}/audio/{update_id}/",
|
86 |
+
json={"updated_transcribed_text": transcription}
|
87 |
+
)
|
88 |
+
response.raise_for_status()
|
89 |
+
updated_data = response.json()
|
90 |
+
return f"Transcription updated successfully for Audio ID: {update_id}!"
|
91 |
+
except requests.exceptions.RequestException as e:
|
92 |
+
return f"Error updating transcription: {str(e)}"
|
93 |
+
|
94 |
+
def list_audios():
|
95 |
+
try:
|
96 |
+
response = requests.get(f"{API_BASE_URL}/audio/")
|
97 |
+
response.raise_for_status()
|
98 |
+
audios = response.json()
|
99 |
+
|
100 |
+
if not audios:
|
101 |
+
return "No audio files found."
|
102 |
+
|
103 |
+
result = []
|
104 |
+
for audio in audios:
|
105 |
+
result.append(f"ID: {audio['id']}")
|
106 |
+
result.append(f"Original Transcription: {audio['original_transcribed_text']}")
|
107 |
+
if audio.get('updated_transcribed_text'):
|
108 |
+
result.append(f"Updated Transcription: {audio['updated_transcribed_text']}")
|
109 |
+
result.append("---")
|
110 |
+
|
111 |
+
return "\n".join(result)
|
112 |
+
except requests.exceptions.RequestException as e:
|
113 |
+
return f"Error listing audios: {str(e)}"
|
114 |
+
|
115 |
+
# Create the Gradio interface
|
116 |
+
with gr.Blocks(title="Audio Transcription System") as app:
|
117 |
+
gr.Markdown("# Audio Transcription System")
|
118 |
+
|
119 |
+
with gr.Tab("Record & Transcribe"):
|
120 |
+
with gr.Row():
|
121 |
+
with gr.Column():
|
122 |
+
audio_input = gr.Audio(
|
123 |
+
sources=["microphone", "upload"],
|
124 |
+
type="filepath",
|
125 |
+
label="Record or Upload Audio"
|
126 |
+
)
|
127 |
+
language_dropdown = gr.Dropdown(
|
128 |
+
choices=["rw", "en-US", "es-ES", "fr-FR", "de-DE"],
|
129 |
+
value="rw",
|
130 |
+
label="Language Code"
|
131 |
+
)
|
132 |
+
upload_button = gr.Button("Process Audio", variant="primary")
|
133 |
+
|
134 |
+
with gr.Column():
|
135 |
+
status_output = gr.Textbox(
|
136 |
+
label="Status",
|
137 |
+
interactive=False,
|
138 |
+
lines=5
|
139 |
+
)
|
140 |
+
transcription_output = gr.Textbox(
|
141 |
+
label="Original Transcription",
|
142 |
+
interactive=False,
|
143 |
+
lines=3
|
144 |
+
)
|
145 |
+
edit_transcription = gr.Textbox(
|
146 |
+
label="Edit Transcription",
|
147 |
+
lines=3,
|
148 |
+
interactive=True
|
149 |
+
)
|
150 |
+
edit_status = gr.Textbox(
|
151 |
+
label="Edit Status",
|
152 |
+
interactive=False
|
153 |
+
)
|
154 |
+
update_button = gr.Button("Update Transcription", variant="primary")
|
155 |
+
|
156 |
+
with gr.Tab("Edit by ID"):
|
157 |
+
with gr.Row():
|
158 |
+
with gr.Column():
|
159 |
+
audio_id_input = gr.Number(
|
160 |
+
label="Audio ID",
|
161 |
+
precision=0
|
162 |
+
)
|
163 |
+
load_button = gr.Button("Load Audio", variant="primary")
|
164 |
+
|
165 |
+
with gr.Column():
|
166 |
+
loaded_original = gr.Textbox(
|
167 |
+
label="Original Transcription",
|
168 |
+
interactive=False,
|
169 |
+
lines=3
|
170 |
+
)
|
171 |
+
loaded_current = gr.Textbox(
|
172 |
+
label="Current Transcription",
|
173 |
+
interactive=False,
|
174 |
+
lines=3
|
175 |
+
)
|
176 |
+
new_transcription = gr.Textbox(
|
177 |
+
label="New Transcription",
|
178 |
+
lines=3,
|
179 |
+
interactive=True
|
180 |
+
)
|
181 |
+
update_status = gr.Textbox(
|
182 |
+
label="Update Status",
|
183 |
+
interactive=False
|
184 |
+
)
|
185 |
+
update_by_id_button = gr.Button("Update Transcription", variant="primary")
|
186 |
+
|
187 |
+
with gr.Tab("View All Recordings"):
|
188 |
+
refresh_button = gr.Button("Refresh Audio List")
|
189 |
+
audio_list = gr.Textbox(
|
190 |
+
label="Audio Files",
|
191 |
+
lines=15,
|
192 |
+
interactive=False
|
193 |
+
)
|
194 |
+
|
195 |
+
# Event handlers
|
196 |
+
upload_button.click(
|
197 |
+
fn=upload_audio,
|
198 |
+
inputs=[audio_input, language_dropdown],
|
199 |
+
outputs=[transcription_output, status_output, edit_transcription, edit_status]
|
200 |
+
)
|
201 |
+
|
202 |
+
update_button.click(
|
203 |
+
fn=update_transcription,
|
204 |
+
inputs=[edit_transcription],
|
205 |
+
outputs=edit_status
|
206 |
+
)
|
207 |
+
|
208 |
+
load_button.click(
|
209 |
+
fn=get_audio_by_id,
|
210 |
+
inputs=[audio_id_input],
|
211 |
+
outputs=[loaded_original, loaded_current, update_status]
|
212 |
+
)
|
213 |
+
|
214 |
+
update_by_id_button.click(
|
215 |
+
fn=update_transcription,
|
216 |
+
inputs=[new_transcription, audio_id_input],
|
217 |
+
outputs=update_status
|
218 |
+
)
|
219 |
+
|
220 |
+
refresh_button.click(
|
221 |
+
fn=list_audios,
|
222 |
+
inputs=[],
|
223 |
+
outputs=audio_list
|
224 |
+
)
|
225 |
+
|
226 |
+
if __name__ == "__main__":
|
227 |
+
app.launch()
|