11import base64
22import os
33import time
4- import requests
54import torch
65import soundfile as sf
76import numpy as np
87from io import BytesIO
98from .utils .runwareUtils import (
109 RUNWARE_API_BASE_URL ,
11- generalRequestWrapper ,
1210 genRandUUID ,
13- getRunwareApiHeaders ,
11+ inferenecRequest ,
12+ pollVideoResult ,
1413 sanitize_for_logging ,
1514 safe_json_dumps ,
1615 sendMediaUUID ,
@@ -239,37 +238,8 @@ def _buildUploadConfig(self, taskUuid, mediaData):
239238
240239 def _makeUploadRequest (self , uploadConfig ):
241240 """Make upload request to Runware API"""
242- def recaller ():
243- print (f"[Debug] Making POST request to Runware API..." )
244- return requests .post (
245- RUNWARE_API_BASE_URL ,
246- headers = getRunwareApiHeaders (),
247- json = uploadConfig ,
248- timeout = self .REQUEST_TIMEOUT ,
249- allow_redirects = False ,
250- stream = True ,
251- )
252-
253- response = generalRequestWrapper (recaller )
254- return self ._parseJsonResponse (response )
255-
256- def _parseJsonResponse (self , response ):
257- """Parse JSON response from API"""
258- if response .status_code != 200 :
259- errorText = response .text [:500 ]
260- print (f"[Debug] Request failed with status { response .status_code } " )
261- print (f"[Debug] Response text: { errorText } " )
262- raise Exception (f"Request failed with status { response .status_code } : { response .text [:200 ]} " )
263-
264- if not response .text or not response .text .strip ():
265- raise Exception ("Request failed: Empty response from server" )
266-
267- try :
268- return response .json ()
269- except ValueError as e :
270- print (f"[Debug] Failed to parse JSON response: { str (e )} " )
271- print (f"[Debug] Response text: { response .text [:500 ]} " )
272- raise Exception (f"Request failed: Invalid JSON response from server: { str (e )} " )
241+ print (f"[Debug] Making upload request to Runware API..." )
242+ return inferenecRequest (uploadConfig )
273243
274244 def _validateUploadResponse (self , uploadResult ):
275245 """Validate upload response"""
@@ -281,26 +251,11 @@ def _validateUploadResponse(self, uploadResult):
281251
282252 def _pollForResult (self , taskUuid ):
283253 """Poll for media upload result"""
284- pollConfig = [{
285- "taskType" : "getResponse" ,
286- "taskUUID" : taskUuid ,
287- }]
288-
289254 for attempt in range (self .MAX_POLL_ATTEMPTS ):
290255 try :
291256 print (f"[Debug] Poll attempt { attempt + 1 } /{ self .MAX_POLL_ATTEMPTS } " )
292-
293- def recaller ():
294- return requests .post (
295- RUNWARE_API_BASE_URL ,
296- headers = getRunwareApiHeaders (),
297- json = pollConfig ,
298- timeout = self .REQUEST_TIMEOUT ,
299- allow_redirects = False ,
300- stream = True ,
301- )
302-
303- pollResult = generalRequestWrapper (recaller )
257+
258+ pollResult = pollVideoResult (taskUuid )
304259 pollData = self ._parsePollResponse (pollResult )
305260
306261 if pollData :
@@ -324,32 +279,18 @@ def recaller():
324279
325280 def _parsePollResponse (self , pollResult ):
326281 """Parse polling response"""
327- if pollResult .status_code != 200 :
328- print (f"[Debug] Poll failed with status { pollResult .status_code } " )
329- return None
330-
331- if not pollResult .text or not pollResult .text .strip ():
282+ if pollResult is None :
332283 print (f"[Debug] Poll returned empty response" )
333284 return None
334-
335- try :
336- pollResultJson = pollResult .json ()
337- except ValueError as e :
338- print (f"[Debug] Failed to parse JSON in poll: { str (e )} " )
339- return None
340-
341- print (f"[Debug] Poll response: { safe_json_dumps (pollResultJson , indent = 2 ) if isinstance (pollResultJson , (dict , list )) else pollResultJson } " )
342-
343- if "errors" in pollResultJson :
344- print (f"[Debug] Poll error: { safe_json_dumps (pollResultJson , indent = 2 ) if isinstance (pollResultJson , (dict , list )) else pollResultJson } " )
285+
286+ print (f"[Debug] Poll response: { safe_json_dumps (pollResult , indent = 2 ) if isinstance (pollResult , (dict , list )) else pollResult } " )
287+ if "errors" in pollResult :
288+ print (f"[Debug] Poll error: { safe_json_dumps (pollResult , indent = 2 ) if isinstance (pollResult , (dict , list )) else pollResult } " )
345289 return None
346-
347- if "data" in pollResultJson and len (pollResultJson ["data" ]) > 0 :
348- print (f"[Debug] Poll data: { sanitize_for_logging (pollResultJson ['data' ][0 ])} " )
349- return pollResultJson ["data" ][0 ]
350-
351- return None
352290
291+ if "data" in pollResult and len (pollResult ["data" ]) > 0 :
292+ print (f"[Debug] Poll data: { sanitize_for_logging (pollResult ['data' ][0 ])} " )
293+ return pollResult ["data" ][0 ]
353294
354- # Export the class directly
295+ return None
355296runwareMediaUpload = RunwareMediaUpload
0 commit comments