Update yml/chat-gemma.py
Browse files- yml/chat-gemma.py +15 -9
yml/chat-gemma.py
CHANGED
|
@@ -4,6 +4,7 @@ from gradio_client import Client
|
|
| 4 |
|
| 5 |
# =========================================================
|
| 6 |
# پچ جلوگیری از قطعی ارتباط (Time Out) برای پردازشهای طولانی
|
|
|
|
| 7 |
original_client_init = httpx.Client.__init__
|
| 8 |
def patched_client_init(self, *args, **kwargs):
|
| 9 |
kwargs['timeout'] = httpx.Timeout(300.0)
|
|
@@ -25,7 +26,7 @@ gh_run_id = os.environ.get('GITHUB_RUN_ID')
|
|
| 25 |
|
| 26 |
files_to_send = []
|
| 27 |
|
| 28 |
-
# ۱. دانلود
|
| 29 |
if file_url and file_url.strip():
|
| 30 |
print(f'Downloading attachment for Base64 conversion: {file_url}')
|
| 31 |
try:
|
|
@@ -36,31 +37,35 @@ if file_url and file_url.strip():
|
|
| 36 |
with open(local_filename, 'wb') as f:
|
| 37 |
f.write(req.content)
|
| 38 |
|
| 39 |
-
#
|
| 40 |
with open(local_filename, "rb") as img_file:
|
| 41 |
encoded_string = base64.b64encode(img_file.read()).decode('utf-8')
|
| 42 |
|
| 43 |
-
# تشخیص نوع فایل برای
|
| 44 |
mime_type = "application/octet-stream"
|
| 45 |
if ext.lower() in ['png', 'jpg', 'jpeg', 'webp']:
|
| 46 |
mime_type = f"image/{ext.lower()}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
base64_data_uri = f"data:{mime_type};base64,{encoded_string}"
|
| 49 |
|
| 50 |
-
# ارسال مستقیم رشته
|
| 51 |
files_to_send.append(base64_data_uri)
|
| 52 |
-
print(' -> Base64
|
| 53 |
except Exception as e:
|
| 54 |
-
print(f'Failed to process file
|
| 55 |
|
| 56 |
print('Connecting to Gemma-4 Space...')
|
| 57 |
bot_reply = ''
|
| 58 |
|
| 59 |
try:
|
| 60 |
-
# ۲. اتصال به اسپیس
|
| 61 |
client = Client('opera8/gemma-4-e4b-it')
|
| 62 |
|
| 63 |
-
# ۳. ار
|
| 64 |
result = client.predict(
|
| 65 |
message={'text': prompt, 'files': files_to_send},
|
| 66 |
thinking=False,
|
|
@@ -70,7 +75,7 @@ try:
|
|
| 70 |
api_name='/generate'
|
| 71 |
)
|
| 72 |
|
| 73 |
-
# ۴.
|
| 74 |
bot_reply = result[0] if isinstance(result, (list, tuple)) else result
|
| 75 |
if isinstance(bot_reply, dict):
|
| 76 |
bot_reply = bot_reply.get('text', str(bot_reply))
|
|
@@ -83,6 +88,7 @@ except Exception as e:
|
|
| 83 |
else:
|
| 84 |
bot_reply = f'⚠️ خطای سرور پردازش: {err_str}'
|
| 85 |
|
|
|
|
| 86 |
if not bot_reply or str(bot_reply).strip() == '':
|
| 87 |
bot_reply = "⚠️ پاسخی از سمت سرور دریافت نشد."
|
| 88 |
else:
|
|
|
|
| 4 |
|
| 5 |
# =========================================================
|
| 6 |
# پچ جلوگیری از قطعی ارتباط (Time Out) برای پردازشهای طولانی
|
| 7 |
+
# زمان انتظار روی ۵ دقیقه تنظیم شده است
|
| 8 |
original_client_init = httpx.Client.__init__
|
| 9 |
def patched_client_init(self, *args, **kwargs):
|
| 10 |
kwargs['timeout'] = httpx.Timeout(300.0)
|
|
|
|
| 26 |
|
| 27 |
files_to_send = []
|
| 28 |
|
| 29 |
+
# ۱. دانلود تصویر و تبدیل قطعی آن به Base64 Data URI (دقیقاً مشابه روش Qwen)
|
| 30 |
if file_url and file_url.strip():
|
| 31 |
print(f'Downloading attachment for Base64 conversion: {file_url}')
|
| 32 |
try:
|
|
|
|
| 37 |
with open(local_filename, 'wb') as f:
|
| 38 |
f.write(req.content)
|
| 39 |
|
| 40 |
+
# تبدیل فایل به رشته Base64
|
| 41 |
with open(local_filename, "rb") as img_file:
|
| 42 |
encoded_string = base64.b64encode(img_file.read()).decode('utf-8')
|
| 43 |
|
| 44 |
+
# تشخیص حدودی نوع فایل برای هدر
|
| 45 |
mime_type = "application/octet-stream"
|
| 46 |
if ext.lower() in ['png', 'jpg', 'jpeg', 'webp']:
|
| 47 |
mime_type = f"image/{ext.lower()}"
|
| 48 |
+
elif ext.lower() in ['mp4', 'mov', 'avi', 'webm']:
|
| 49 |
+
mime_type = f"video/{ext.lower()}"
|
| 50 |
+
elif ext.lower() in ['wav', 'mp3', 'ogg', 'flac']:
|
| 51 |
+
mime_type = f"audio/{ext.lower()}"
|
| 52 |
|
| 53 |
base64_data_uri = f"data:{mime_type};base64,{encoded_string}"
|
| 54 |
|
| 55 |
+
# ارسال مستقیم رشته متنی بیس۶۴ به عنوان فایل (بدون استفاده از آبجکت دیکشنری)
|
| 56 |
files_to_send.append(base64_data_uri)
|
| 57 |
+
print(' -> Base64 payload generated successfully.')
|
| 58 |
except Exception as e:
|
| 59 |
+
print(f'Failed to process file: {e}')
|
| 60 |
|
| 61 |
print('Connecting to Gemma-4 Space...')
|
| 62 |
bot_reply = ''
|
| 63 |
|
| 64 |
try:
|
| 65 |
+
# ۲. اتصال به اسپیس هدف شما
|
| 66 |
client = Client('opera8/gemma-4-e4b-it')
|
| 67 |
|
| 68 |
+
# ۳. اجرای متد Predict (ارسال پیام به همراه آرایه متنی تصاویر)
|
| 69 |
result = client.predict(
|
| 70 |
message={'text': prompt, 'files': files_to_send},
|
| 71 |
thinking=False,
|
|
|
|
| 75 |
api_name='/generate'
|
| 76 |
)
|
| 77 |
|
| 78 |
+
# ۴. استخراج پاسخ متنی
|
| 79 |
bot_reply = result[0] if isinstance(result, (list, tuple)) else result
|
| 80 |
if isinstance(bot_reply, dict):
|
| 81 |
bot_reply = bot_reply.get('text', str(bot_reply))
|
|
|
|
| 88 |
else:
|
| 89 |
bot_reply = f'⚠️ خطای سرور پردازش: {err_str}'
|
| 90 |
|
| 91 |
+
# بررسی نهایی برای جلوگیری از کرش سیستم در صورت خالی بودن پاسخ
|
| 92 |
if not bot_reply or str(bot_reply).strip() == '':
|
| 93 |
bot_reply = "⚠️ پاسخی از سمت سرور دریافت نشد."
|
| 94 |
else:
|