-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
159 lines (144 loc) · 4.72 KB
/
app.py
File metadata and controls
159 lines (144 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from flask import Flask, request
import os
import speech_recognition as sr
from pydub import AudioSegment
app = Flask(__name__)
@app.route('/')
def index():
return '''
<html>
<head>
<title>Audio to Text LWS25</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
height: 100vh;
background-color: #1e1e1e;
color: white;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
form {
text-align: center;
background-color: #2e2e2e;
padding: 20px;
border-radius: 10px;
width: 50%;
}
input, select, button {
margin: 10px;
padding: 10px;
width: 80%;
}
button {
background-color: #00adb5;
color: white;
border: none;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Audio to Text Transcriber - LWS25</h1>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="audio" required><br>
<select name="language">
<option value="en-US">English</option>
<option value="id-ID" selected>Indonesia</option>
<option value="ja-JP">Jepang</option>
<option value="ko-KR">Korea</option>
<option value="ar-SA">Arab</option>
</select><br>
<button type="submit">Upload & Transkrip</button>
</form>
</body>
</html>
'''
@app.route('/upload', methods=['POST'])
def upload_file():
if 'audio' not in request.files:
return 'No file uploaded', 400
audio_file = request.files['audio']
language = request.form['language']
if audio_file.filename == '':
return 'No selected file', 400
# Simpan audio sementara di RAM
audio_path = 'temp_audio_input.wav'
audio_file.save(audio_path)
text = audio_to_text(audio_path, language)
# Hapus audio setelah transkrip
os.remove(audio_path)
return f'''
<html>
<head>
<title>Transkrip Audio</title>
<style>
body {{
font-family: Arial, sans-serif;
margin: 20px;
height: 100vh;
background-color: #1e1e1e;
color: white;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}}
textarea {{
width: 80%;
height: 200px;
margin-bottom: 20px;
background-color: #2e2e2e;
color: white;
border: none;
padding: 10px;
border-radius: 10px;
}}
button {{
background-color: #00adb5;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
}}
button:hover {{
background-color: #007e8c;
}}
</style>
</head>
<body>
<h1>Transkrip Audio LWS25</h1>
<textarea id="transcript" readonly>{text}</textarea><br>
<button onclick="copyText()">Copy Transkrip</button>
<script>
function copyText() {{
var textArea = document.getElementById('transcript');
textArea.select();
document.execCommand('copy');
}}
</script>
</body>
</html>
'''
def audio_to_text(audio_path, language):
recognizer = sr.Recognizer()
# Convert semua format audio ke WAV
audio = AudioSegment.from_file(audio_path)
audio.export("temp.wav", format="wav")
with sr.AudioFile("temp.wav") as source:
audio_data = recognizer.record(source)
try:
text = recognizer.recognize_google(audio_data, language=language)
except sr.UnknownValueError:
text = "Gagal mengenali audio. Coba lagi dengan audio yang lebih jelas."
except sr.RequestError:
text = "Koneksi ke Google Speech API gagal. Cek koneksi internet."
finally:
os.remove("temp.wav")
return text
if __name__ == "__main__":
app.run()