Update main.py
Browse files
main.py
CHANGED
|
@@ -10,6 +10,7 @@ import mimetypes
|
|
| 10 |
import io
|
| 11 |
import json
|
| 12 |
import datetime
|
|
|
|
| 13 |
from flask import Flask
|
| 14 |
from rubpy.bot import BotClient, filters
|
| 15 |
|
|
@@ -573,6 +574,41 @@ async def process_image(client, chat_id, prompt):
|
|
| 573 |
if os.path.exists(file_name): os.remove(file_name)
|
| 574 |
except Exception as e: await send_with_keyboard(client, chat_id, f"❌ خطا در ذخیره عکس:\n{str(e)[:150]}", True)
|
| 575 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 576 |
|
| 577 |
# --- ۲.۵. پردازش ویرایش عکس با FLUX.2-dev ---
|
| 578 |
async def process_image_edit(client, chat_id, image_bytes, prompt):
|
|
@@ -584,8 +620,13 @@ async def process_image_edit(client, chat_id, image_bytes, prompt):
|
|
| 584 |
if not HF_TOKENS:
|
| 585 |
return await send_with_keyboard(client, chat_id, "❌ توکنهای هاگینگ فیس تنظیم نشدهاند.", False)
|
| 586 |
|
| 587 |
-
proc_msg = await send_with_keyboard(client, chat_id, "🪄 در حال
|
| 588 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 589 |
keys_to_try = HF_TOKENS.copy()
|
| 590 |
random.shuffle(keys_to_try)
|
| 591 |
generated_image = None
|
|
@@ -596,7 +637,7 @@ async def process_image_edit(client, chat_id, image_bytes, prompt):
|
|
| 596 |
hf_client = AsyncInferenceClient(provider="fal-ai", api_key=token)
|
| 597 |
generated_image = await hf_client.image_to_image(
|
| 598 |
image_bytes,
|
| 599 |
-
prompt=
|
| 600 |
model="black-forest-labs/FLUX.2-dev"
|
| 601 |
)
|
| 602 |
break
|
|
@@ -623,7 +664,7 @@ async def process_image_edit(client, chat_id, image_bytes, prompt):
|
|
| 623 |
rgb_im = generated_image.convert('RGB')
|
| 624 |
rgb_im.save(file_name, format="JPEG", quality=100)
|
| 625 |
await asyncio.sleep(1)
|
| 626 |
-
caption_text = f"🪄 ویرایش عکس با هوش مصنوعی Flux.2 انجام شد!\n\n✨ تغییرات خواسته شده: {prompt}"
|
| 627 |
upload_result = await helper_upload_file(client, chat_id, file_name, "Image", caption_text)
|
| 628 |
if upload_result is not True:
|
| 629 |
await send_with_keyboard(client, chat_id, f"❌ عکس ساخته شد اما خطا در ارسال به روبیکا رخ داد:\n`{str(upload_result)[:800]}`", True)
|
|
|
|
| 10 |
import io
|
| 11 |
import json
|
| 12 |
import datetime
|
| 13 |
+
import string
|
| 14 |
from flask import Flask
|
| 15 |
from rubpy.bot import BotClient, filters
|
| 16 |
|
|
|
|
| 574 |
if os.path.exists(file_name): os.remove(file_name)
|
| 575 |
except Exception as e: await send_with_keyboard(client, chat_id, f"❌ خطا در ذخیره عکس:\n{str(e)[:150]}", True)
|
| 576 |
|
| 577 |
+
# --- تابع جدید برای ارسال متن به اسپیس مترجم ---
|
| 578 |
+
async def translate_text_aloha(prompt_text):
|
| 579 |
+
session_hash = ''.join(random.choices(string.ascii_lowercase + string.digits, k=11))
|
| 580 |
+
join_url = "https://hamed744-translate-tts-aloha.hf.space/gradio_api/queue/join"
|
| 581 |
+
payload = {
|
| 582 |
+
"data": [prompt_text, "انگلیسی (آمریکا) - جنی (زن)", 0, 0, 0],
|
| 583 |
+
"fn_index": 1,
|
| 584 |
+
"session_hash": session_hash
|
| 585 |
+
}
|
| 586 |
+
|
| 587 |
+
try:
|
| 588 |
+
async with aiohttp.ClientSession() as session:
|
| 589 |
+
# پیوستن به صف ترجمه
|
| 590 |
+
async with session.post(join_url, json=payload, timeout=20) as resp:
|
| 591 |
+
if resp.status != 200:
|
| 592 |
+
return prompt_text
|
| 593 |
+
|
| 594 |
+
# دریافت دادهها به صورت جریانی (Stream)
|
| 595 |
+
data_url = f"https://hamed744-translate-tts-aloha.hf.space/gradio_api/queue/data?session_hash={session_hash}"
|
| 596 |
+
async with session.get(data_url, timeout=60) as resp:
|
| 597 |
+
async for line_bytes in resp.content:
|
| 598 |
+
line = line_bytes.decode('utf-8').strip()
|
| 599 |
+
if line.startswith("data: "):
|
| 600 |
+
try:
|
| 601 |
+
json_data = json.loads(line[6:])
|
| 602 |
+
if json_data.get("msg") == "process_completed":
|
| 603 |
+
if json_data.get("success"):
|
| 604 |
+
return json_data["output"]["data"][0]
|
| 605 |
+
break
|
| 606 |
+
except Exception:
|
| 607 |
+
pass
|
| 608 |
+
except Exception:
|
| 609 |
+
pass
|
| 610 |
+
|
| 611 |
+
return prompt_text
|
| 612 |
|
| 613 |
# --- ۲.۵. پردازش ویرایش عکس با FLUX.2-dev ---
|
| 614 |
async def process_image_edit(client, chat_id, image_bytes, prompt):
|
|
|
|
| 620 |
if not HF_TOKENS:
|
| 621 |
return await send_with_keyboard(client, chat_id, "❌ توکنهای هاگینگ فیس تنظیم نشدهاند.", False)
|
| 622 |
|
| 623 |
+
proc_msg = await send_with_keyboard(client, chat_id, "🪄 در حال ترجمه دستور شما توسط اسپیس و اعمال جادوی FLUX.2...\n(این فرآیند ممکن است کمی طول بکشد)", False)
|
| 624 |
|
| 625 |
+
# گرفتن ترجمه از اسپیس درخواستی
|
| 626 |
+
translated_prompt = await translate_text_aloha(prompt)
|
| 627 |
+
if not translated_prompt or translated_prompt.strip() == "":
|
| 628 |
+
translated_prompt = prompt
|
| 629 |
+
|
| 630 |
keys_to_try = HF_TOKENS.copy()
|
| 631 |
random.shuffle(keys_to_try)
|
| 632 |
generated_image = None
|
|
|
|
| 637 |
hf_client = AsyncInferenceClient(provider="fal-ai", api_key=token)
|
| 638 |
generated_image = await hf_client.image_to_image(
|
| 639 |
image_bytes,
|
| 640 |
+
prompt=translated_prompt,
|
| 641 |
model="black-forest-labs/FLUX.2-dev"
|
| 642 |
)
|
| 643 |
break
|
|
|
|
| 664 |
rgb_im = generated_image.convert('RGB')
|
| 665 |
rgb_im.save(file_name, format="JPEG", quality=100)
|
| 666 |
await asyncio.sleep(1)
|
| 667 |
+
caption_text = f"🪄 ویرایش عکس با هوش مصنوعی Flux.2 انجام شد!\n\n✨ تغییرات خواسته شده: {prompt}\n🔤 متن ارسال شده به هوش: {translated_prompt}"
|
| 668 |
upload_result = await helper_upload_file(client, chat_id, file_name, "Image", caption_text)
|
| 669 |
if upload_result is not True:
|
| 670 |
await send_with_keyboard(client, chat_id, f"❌ عکس ساخته شد اما خطا در ارسال به روبیکا رخ داد:\n`{str(upload_result)[:800]}`", True)
|