Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -322,79 +322,79 @@ import requests
|
|
322 |
# else:
|
323 |
# return {"error": response.json()}
|
324 |
|
325 |
-
# def process_audio(audio):
|
326 |
-
# audio_file = open(audio, "rb")
|
327 |
-
# try:
|
328 |
-
# audio = AudioSegment.from_file(audio_file)
|
329 |
-
# except CouldntDecodeError:
|
330 |
-
# os.remove(audio)
|
331 |
-
# return "Unsupported audio format"
|
332 |
-
|
333 |
-
# # بررسی مدت زمان فایل صوتی
|
334 |
-
# duration_seconds = len(audio_file) / 1000.0 # مدت زمان به ثانیه
|
335 |
-
# if duration_seconds > 900:
|
336 |
-
# os.remove(audio)
|
337 |
-
# return "Audio file is too long"
|
338 |
-
|
339 |
-
# project_root = os.path.dirname(os.path.dirname(__file__))
|
340 |
-
# voice_id = str(uuid.uuid4())
|
341 |
-
# # تبدیل به فرمت WAV سازگار با Whisper
|
342 |
-
# whisper_path = f'{voice_id}.wav'
|
343 |
-
# whisper_voice_path = os.path.join(project_root, whisper_path)
|
344 |
-
|
345 |
-
# audio.export(whisper_voice_path, format='wav')
|
346 |
-
|
347 |
-
# # ارسال درخواست به API Hugging Face
|
348 |
-
# output = query(whisper_voice_path)
|
349 |
-
|
350 |
-
# # حذف فایل موقت
|
351 |
-
# os.remove(whisper_voice_path)
|
352 |
-
|
353 |
-
# if "error" in output:
|
354 |
-
# print("Error:", output["error"])
|
355 |
-
# return "Transcription failed"
|
356 |
-
# else:
|
357 |
-
# text_question = output.get("text", "")
|
358 |
-
# print("text_question =", text_question)
|
359 |
-
# return text_question
|
360 |
-
|
361 |
def process_audio(audio):
|
362 |
-
|
363 |
-
audio_file = open(audio, "rb")
|
364 |
-
|
365 |
-
# Load and convert the audio file
|
366 |
try:
|
367 |
audio = AudioSegment.from_file(audio_file)
|
368 |
-
|
369 |
except CouldntDecodeError:
|
370 |
-
os.remove(
|
371 |
return "Unsupported audio format"
|
372 |
|
373 |
-
#
|
374 |
-
duration_seconds = len(
|
375 |
if duration_seconds > 900:
|
376 |
-
os.remove(
|
377 |
return "Audio file is too long"
|
378 |
|
379 |
project_root = os.path.dirname(os.path.dirname(__file__))
|
380 |
voice_id = str(uuid.uuid4())
|
381 |
-
#
|
382 |
whisper_path = f'{voice_id}.wav'
|
383 |
whisper_voice_path = os.path.join(project_root, whisper_path)
|
384 |
|
385 |
audio.export(whisper_voice_path, format='wav')
|
386 |
|
387 |
-
|
|
|
388 |
|
389 |
-
with open(whisper_voice_path, 'rb') as audio_file:
|
390 |
-
text_question = client.audio.transcriptions.create(model="whisper-1",
|
391 |
-
file=audio_file,
|
392 |
-
response_format="text",
|
393 |
-
language="fa")
|
394 |
# حذف فایل موقت
|
395 |
os.remove(whisper_voice_path)
|
396 |
-
|
397 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
398 |
|
399 |
|
400 |
|
|
|
322 |
# else:
|
323 |
# return {"error": response.json()}
|
324 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
325 |
def process_audio(audio):
|
326 |
+
audio_file = open(audio, "rb")
|
|
|
|
|
|
|
327 |
try:
|
328 |
audio = AudioSegment.from_file(audio_file)
|
|
|
329 |
except CouldntDecodeError:
|
330 |
+
os.remove(audio)
|
331 |
return "Unsupported audio format"
|
332 |
|
333 |
+
# بررسی مدت زمان فایل صوتی
|
334 |
+
duration_seconds = len(audio_file) / 1000.0 # مدت زمان به ثانیه
|
335 |
if duration_seconds > 900:
|
336 |
+
os.remove(audio)
|
337 |
return "Audio file is too long"
|
338 |
|
339 |
project_root = os.path.dirname(os.path.dirname(__file__))
|
340 |
voice_id = str(uuid.uuid4())
|
341 |
+
# تبدیل به فرمت WAV سازگار با Whisper
|
342 |
whisper_path = f'{voice_id}.wav'
|
343 |
whisper_voice_path = os.path.join(project_root, whisper_path)
|
344 |
|
345 |
audio.export(whisper_voice_path, format='wav')
|
346 |
|
347 |
+
# ارسال درخواست به API Hugging Face
|
348 |
+
output = query(whisper_voice_path)
|
349 |
|
|
|
|
|
|
|
|
|
|
|
350 |
# حذف فایل موقت
|
351 |
os.remove(whisper_voice_path)
|
352 |
+
|
353 |
+
if "error" in output:
|
354 |
+
print("Error:", output["error"])
|
355 |
+
return "Transcription failed"
|
356 |
+
else:
|
357 |
+
text_question = output.get("text", "")
|
358 |
+
print("text_question =", text_question)
|
359 |
+
return text_question
|
360 |
+
|
361 |
+
# def process_audio(audio):
|
362 |
+
# # باز کردن فایل صوتی ضبطشده
|
363 |
+
# audio_file = open(audio, "rb")
|
364 |
+
|
365 |
+
# # Load and convert the audio file
|
366 |
+
# try:
|
367 |
+
# audio = AudioSegment.from_file(audio_file)
|
368 |
+
|
369 |
+
# except CouldntDecodeError:
|
370 |
+
# os.remove(audio_file)
|
371 |
+
# return "Unsupported audio format"
|
372 |
+
|
373 |
+
# # Check duration
|
374 |
+
# duration_seconds = len(audio) / 1000.0 # Duration in seconds
|
375 |
+
# if duration_seconds > 900:
|
376 |
+
# os.remove(whisper_voice_path)
|
377 |
+
# return "Audio file is too long"
|
378 |
+
|
379 |
+
# project_root = os.path.dirname(os.path.dirname(__file__))
|
380 |
+
# voice_id = str(uuid.uuid4())
|
381 |
+
# # Convert to WAV format compatible with Whisper
|
382 |
+
# whisper_path = f'{voice_id}.wav'
|
383 |
+
# whisper_voice_path = os.path.join(project_root, whisper_path)
|
384 |
+
|
385 |
+
# audio.export(whisper_voice_path, format='wav')
|
386 |
+
|
387 |
+
# client = OpenAI(api_key=OPENAI_API_KEY)
|
388 |
+
|
389 |
+
# with open(whisper_voice_path, 'rb') as audio_file:
|
390 |
+
# text_question = client.audio.transcriptions.create(model="whisper-1",
|
391 |
+
# file=audio_file,
|
392 |
+
# response_format="text",
|
393 |
+
# language="fa")
|
394 |
+
# # حذف فایل موقت
|
395 |
+
# os.remove(whisper_voice_path)
|
396 |
+
# print("text_question=", text_question)
|
397 |
+
# return text_question
|
398 |
|
399 |
|
400 |
|