Update yml/matnbesada.py
Browse files- yml/matnbesada.py +101 -37
yml/matnbesada.py
CHANGED
|
@@ -1,52 +1,116 @@
|
|
| 1 |
-
import os, sys, requests
|
| 2 |
from gradio_client import Client, handle_file
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
space_url = os.environ.get('SPACE_URL')
|
| 9 |
-
github_run_id = os.environ.get('GITHUB_RUN_ID')
|
| 10 |
-
hf_token = os.environ.get('HF_TOKEN', '')
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
| 14 |
try:
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
result = client.predict(
|
| 26 |
-
handle_file(
|
| 27 |
-
|
| 28 |
-
|
| 29 |
api_name="/transcribe"
|
| 30 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
with open("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
|
| 49 |
-
print("عملیات با موفقیت پایان یافت.")
|
| 50 |
except Exception as e:
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
sys.exit(1)
|
|
|
|
| 1 |
+
import os, sys, requests, time, json
|
| 2 |
from gradio_client import Client, handle_file
|
| 3 |
|
| 4 |
+
raw_prompt = os.environ.get('PROMPT', '')
|
| 5 |
+
run_id = os.environ.get('RUN_ID', '')
|
| 6 |
+
space_url = os.environ.get('SPACE_URL', '')
|
| 7 |
+
github_run_id = os.environ.get('GITHUB_RUN_ID', '')
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
def report_failure(error_msg):
|
| 10 |
+
try:
|
| 11 |
+
requests.post(
|
| 12 |
+
f"{space_url}/api/webhook/fail",
|
| 13 |
+
json={
|
| 14 |
+
"run_id": run_id,
|
| 15 |
+
"error": error_msg,
|
| 16 |
+
"event_type": "matnbesada",
|
| 17 |
+
"client_payload": {
|
| 18 |
+
"prompt": raw_prompt,
|
| 19 |
+
"run_id": run_id,
|
| 20 |
+
"space_url": space_url
|
| 21 |
+
},
|
| 22 |
+
"github_run_id": github_run_id
|
| 23 |
+
},
|
| 24 |
+
timeout=15
|
| 25 |
+
)
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"Failed to report failure: {e}")
|
| 28 |
|
| 29 |
+
print('1. Parsing parameters (Masquerade handling)...')
|
| 30 |
try:
|
| 31 |
+
# استخراج دیتای جاسازی شده در پرامپت
|
| 32 |
+
parts = raw_prompt.split("___")
|
| 33 |
+
actual_run_id = parts[0]
|
| 34 |
+
lang_val = parts[1] if len(parts) > 1 else "Auto-Detect"
|
| 35 |
+
opt_val = parts[2].lower() == 'true' if len(parts) > 2 else True
|
| 36 |
+
except Exception as e:
|
| 37 |
+
actual_run_id = run_id
|
| 38 |
+
lang_val = "Auto-Detect"
|
| 39 |
+
opt_val = True
|
| 40 |
+
|
| 41 |
+
audio_download_url = f"{space_url}/static/images/{actual_run_id}.audio"
|
| 42 |
+
local_audio_path = f"input_audio_{actual_run_id}.tmp"
|
| 43 |
+
|
| 44 |
+
print(f'2. Downloading audio source from Docker: {audio_download_url}')
|
| 45 |
+
try:
|
| 46 |
+
req = requests.get(audio_download_url, timeout=120)
|
| 47 |
+
if req.status_code != 200:
|
| 48 |
+
raise Exception(f"Audio not found. HTTP Status: {req.status_code}")
|
| 49 |
|
| 50 |
+
with open(local_audio_path, 'wb') as f:
|
| 51 |
+
f.write(req.content)
|
| 52 |
+
print(" -> Audio downloaded successfully.")
|
| 53 |
+
except Exception as e:
|
| 54 |
+
err_str = f"Download failed: {str(e)}"
|
| 55 |
+
print(err_str)
|
| 56 |
+
report_failure(err_str)
|
| 57 |
+
sys.exit(1)
|
| 58 |
+
|
| 59 |
+
print('3. Connecting to WhisperX AI Space (Opera8/sadabematn)...')
|
| 60 |
+
try:
|
| 61 |
+
client = Client("Opera8/sadabematn")
|
| 62 |
+
|
| 63 |
+
print(f'4. Transcribing audio... (Lang: {lang_val}, Optimize: {opt_val})')
|
| 64 |
result = client.predict(
|
| 65 |
+
handle_file(local_audio_path),
|
| 66 |
+
lang_val,
|
| 67 |
+
opt_val,
|
| 68 |
api_name="/transcribe"
|
| 69 |
)
|
| 70 |
+
|
| 71 |
+
# تفکیک خروجیهای برگشتی
|
| 72 |
+
full_text = result[0] if isinstance(result, (list, tuple)) else result
|
| 73 |
+
srt_content = result[1] if isinstance(result, (list, tuple)) and len(result) > 1 else ""
|
| 74 |
+
word_json = result[2] if isinstance(result, (list, tuple)) and len(result) > 2 else "{}"
|
| 75 |
+
|
| 76 |
+
if isinstance(full_text, dict):
|
| 77 |
+
full_text = str(full_text)
|
| 78 |
|
| 79 |
+
print('5. Uploading Multi-format results back to Docker Space...')
|
| 80 |
+
|
| 81 |
+
# آپلود فایل SRT
|
| 82 |
+
with open("result.srt", "w", encoding="utf-8") as f:
|
| 83 |
+
f.write(str(srt_content))
|
| 84 |
+
with open("result.srt", 'rb') as f:
|
| 85 |
+
requests.post(f'{space_url}/api/webhook/upload',
|
| 86 |
+
data={'run_id': f"{actual_run_id}_srt", 'github_run_id': '', 'ext': 'srt'},
|
| 87 |
+
files={'file': f})
|
| 88 |
+
|
| 89 |
+
# آپلود فایل JSON خام زمانبندی
|
| 90 |
+
with open("result.json", "w", encoding="utf-8") as f:
|
| 91 |
+
f.write(str(word_json))
|
| 92 |
+
with open("result.json", 'rb') as f:
|
| 93 |
+
requests.post(f'{space_url}/api/webhook/upload',
|
| 94 |
+
data={'run_id': f"{actual_run_id}_json", 'github_run_id': '', 'ext': 'json'},
|
| 95 |
+
files={'file': f})
|
| 96 |
|
| 97 |
+
# آپلود متن کامل (این مرحله سیستم بررسی وضعیت (Status Check) را فعال میکند)
|
| 98 |
+
with open("result.txt", "w", encoding="utf-8") as f:
|
| 99 |
+
f.write(str(full_text))
|
| 100 |
+
with open("result.txt", 'rb') as f:
|
| 101 |
+
res = requests.post(f'{space_url}/api/webhook/upload',
|
| 102 |
+
data={'run_id': actual_run_id, 'github_run_id': github_run_id, 'ext': 'txt'},
|
| 103 |
+
files={'file': f})
|
| 104 |
|
| 105 |
+
if res.status_code == 200:
|
| 106 |
+
print('6. SUCCESS! Lifecycle completed.')
|
| 107 |
+
else:
|
| 108 |
+
sys.exit(1)
|
| 109 |
|
|
|
|
| 110 |
except Exception as e:
|
| 111 |
+
err_str = str(e)
|
| 112 |
+
print(f'CRITICAL GENERATION ERROR: {err_str}')
|
| 113 |
+
if 'quota' in err_str.lower() or 'zerogpu' in err_str.lower():
|
| 114 |
+
err_str = "سهمیه سرور گیتهاب اکشن یا هاگینگفیس پر شده است. لطفاً کمی بعد تلاش کنید."
|
| 115 |
+
report_failure(err_str)
|
| 116 |
sys.exit(1)
|