99app = Flask (__name__ )
1010model = whisper .load_model ("base" )
1111
12+
1213# Create jobs database if it doesn't exist
1314def init_db ():
14- conn = sqlite3 .connect (' jobs.db' )
15+ conn = sqlite3 .connect (" jobs.db" )
1516 c = conn .cursor ()
16- c .execute ('''
17+ c .execute ("""
1718 CREATE TABLE IF NOT EXISTS jobs (
1819 id TEXT PRIMARY KEY,
1920 status TEXT NOT NULL,
@@ -23,149 +24,169 @@ def init_db():
2324 result TEXT,
2425 error TEXT
2526 )
26- ''' )
27+ """ )
2728 conn .commit ()
2829 conn .close ()
2930
31+
3032init_db ()
3133
34+
3235# Background worker to process jobs
3336def process_job (job_id ):
3437 # Create app context for the thread
3538 with app .app_context ():
36- conn = sqlite3 .connect (' jobs.db' )
39+ conn = sqlite3 .connect (" jobs.db" )
3740 c = conn .cursor ()
38-
41+
3942 # Get job details
40- c .execute (' SELECT audio_path FROM jobs WHERE id = ?' , (job_id ,))
43+ c .execute (" SELECT audio_path FROM jobs WHERE id = ?" , (job_id ,))
4144 result = c .fetchone ()
4245 if not result :
4346 conn .close ()
4447 return
45-
48+
4649 audio_path = result [0 ]
47-
50+
4851 try :
4952 # Update status to processing
50- c .execute ('''
53+ c .execute (
54+ """
5155 UPDATE jobs
5256 SET status = 'processing',
5357 updated_at = ?
5458 WHERE id = ?
55- ''' , (datetime .now (), job_id ))
59+ """ ,
60+ (datetime .now (), job_id ),
61+ )
5662 conn .commit ()
57-
63+
5864 # Process transcription
5965 result = model .transcribe (audio_path , verbose = False )
6066 transcription = result ["text" ]
6167 segments = result ["segments" ]
62-
68+
6369 # Update job with results
64- c .execute ('''
70+ c .execute (
71+ """
6572 UPDATE jobs
6673 SET status = 'completed',
6774 result = ?,
6875 updated_at = ?
6976 WHERE id = ?
70- ''' , (
71- json .dumps ({
72- 'transcription' : transcription ,
73- 'language' : result ["language" ],
74- 'segments' : [
77+ """ ,
78+ (
79+ json .dumps (
7580 {
76- 'text' : segment ['text' ],
77- 'start' : segment ['start' ],
78- 'end' : segment ['end' ]
81+ "transcription" : transcription ,
82+ "language" : result ["language" ],
83+ "segments" : [
84+ {
85+ "text" : segment ["text" ],
86+ "start" : segment ["start" ],
87+ "end" : segment ["end" ],
88+ }
89+ for segment in segments
90+ ],
7991 }
80- for segment in segments
81- ]
82- }),
83- datetime .now (),
84- job_id
85- ))
92+ ),
93+ datetime .now (),
94+ job_id ,
95+ ),
96+ )
8697 conn .commit ()
87-
98+
8899 except Exception as e :
89100 # Update job with error
90- c .execute ('''
101+ c .execute (
102+ """
91103 UPDATE jobs
92104 SET status = 'failed',
93105 error = ?,
94106 updated_at = ?
95107 WHERE id = ?
96- ''' , (str (e ), datetime .now (), job_id ))
108+ """ ,
109+ (str (e ), datetime .now (), job_id ),
110+ )
97111 conn .commit ()
98112 finally :
99113 conn .close ()
100114
101- @app .route ('/start' , methods = ['POST' ])
115+
116+ @app .route ("/start" , methods = ["POST" ])
102117def start_transcription ():
103- if ' audio' not in request .files :
104- return jsonify ({' error' : ' No audio file provided' }), 400
105-
106- audio_file = request .files [' audio' ]
107- if audio_file .filename == '' :
108- return jsonify ({' error' : ' No selected file' }), 400
109-
118+ if " audio" not in request .files :
119+ return jsonify ({" error" : " No audio file provided" }), 400
120+
121+ audio_file = request .files [" audio" ]
122+ if audio_file .filename == "" :
123+ return jsonify ({" error" : " No selected file" }), 400
124+
110125 # Generate unique job ID
111126 job_id = f"job_{ datetime .now ().strftime ('%Y%m%d_%H%M%S' )} "
112-
127+
113128 # Save audio file
114- with tempfile .NamedTemporaryFile (suffix = ' .wav' , delete = False ) as temp_file :
129+ with tempfile .NamedTemporaryFile (suffix = " .wav" , delete = False ) as temp_file :
115130 audio_file .save (temp_file .name )
116131 audio_path = temp_file .name
117-
132+
118133 # Create job record
119- conn = sqlite3 .connect (' jobs.db' )
134+ conn = sqlite3 .connect (" jobs.db" )
120135 c = conn .cursor ()
121- c .execute ('''
136+ c .execute (
137+ """
122138 INSERT INTO jobs (id, status, created_at, updated_at, audio_path)
123139 VALUES (?, 'queued', ?, ?, ?)
124- ''' , (job_id , datetime .now (), datetime .now (), audio_path ))
140+ """ ,
141+ (job_id , datetime .now (), datetime .now (), audio_path ),
142+ )
125143 conn .commit ()
126144 conn .close ()
127-
145+
128146 # Start processing in background
129147 Thread (target = process_job , args = (job_id ,)).start ()
130-
131- return jsonify ({
132- 'job_id' : job_id ,
133- 'status' : 'queued' ,
134- 'created_at' : datetime .now ().isoformat ()
135- })
136-
137- @app .route ('/status/<job_id>' , methods = ['GET' ])
148+
149+ return jsonify (
150+ {"job_id" : job_id , "status" : "queued" , "created_at" : datetime .now ().isoformat ()}
151+ )
152+
153+
154+ @app .route ("/status/<job_id>" , methods = ["GET" ])
138155def get_job_status (job_id ):
139- conn = sqlite3 .connect (' jobs.db' )
156+ conn = sqlite3 .connect (" jobs.db" )
140157 c = conn .cursor ()
141-
142- c .execute ('''
158+
159+ c .execute (
160+ """
143161 SELECT status, result, error, created_at, updated_at
144162 FROM jobs
145163 WHERE id = ?
146- ''' , (job_id ,))
147-
164+ """ ,
165+ (job_id ,),
166+ )
167+
148168 result = c .fetchone ()
149169 conn .close ()
150-
170+
151171 if not result :
152- return jsonify ({' error' : ' Job not found' }), 404
153-
172+ return jsonify ({" error" : " Job not found" }), 404
173+
154174 status , result_json , error , created_at , updated_at = result
155-
175+
156176 response = {
157- ' job_id' : job_id ,
158- ' status' : status ,
159- ' created_at' : created_at ,
160- ' updated_at' : updated_at
177+ " job_id" : job_id ,
178+ " status" : status ,
179+ " created_at" : created_at ,
180+ " updated_at" : updated_at ,
161181 }
162-
182+
163183 if result_json :
164- response [' result' ] = json .loads (result_json )
184+ response [" result" ] = json .loads (result_json )
165185 if error :
166- response [' error' ] = error
167-
186+ response [" error" ] = error
187+
168188 return jsonify (response )
169189
170- if __name__ == '__main__' :
171- app .run (debug = True , port = 5000 )
190+
191+ if __name__ == "__main__" :
192+ app .run (host = "0.0.0.0" , debug = True , port = 8001 )
0 commit comments