Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -1186,6 +1186,31 @@ async def process_legacy_vc_job(client, chat_id, src_bytes, model_url, pitch, mo
|
|
| 1186 |
# ==============================================================================
|
| 1187 |
# 🟢 پارت 13: پردازش هوش مصنوعی متنی و تصویری (ترکیب کامل با Aya Expanse و Gemma)
|
| 1188 |
# ==============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1189 |
|
| 1190 |
async def helper_upload_to_aya(file_bytes, file_name, mime_type):
|
| 1191 |
"""آپلود تصویر در سرور بکاپ (Aya) در صورت قطعی گیتهاب"""
|
|
@@ -1515,6 +1540,9 @@ async def process_gemini(client, chat_id, prompt, file_bytes=None, file_name=Non
|
|
| 1515 |
await send_with_keyboard(client, chat_id, "❌ تمامی سرورها شلوغ هستند یا حجم فایل بالا بود. لطفاً بعداً امتحان کنید.", False)
|
| 1516 |
return
|
| 1517 |
|
|
|
|
|
|
|
|
|
|
| 1518 |
# ذخیره پاسخ در تاریخچه چت کاربر
|
| 1519 |
history.append({"role": "model", "parts": [{"text": final_answer}]})
|
| 1520 |
user_states[chat_id]["history"] = history
|
|
@@ -2009,6 +2037,8 @@ async def process_stt(client, chat_id, audio_bytes, file_name):
|
|
| 2009 |
except Exception: pass
|
| 2010 |
|
| 2011 |
if transcribed_text:
|
|
|
|
|
|
|
| 2012 |
sent = await send_with_keyboard(client, chat_id, f"📝 **متن استخراج شده:**\n\n{transcribed_text}", True)
|
| 2013 |
if sent and not creds.get("is_premium"):
|
| 2014 |
user_credits_db[str_chat_id]["stt"] -= 1
|
|
@@ -2045,6 +2075,8 @@ async def process_image_analysis(client, chat_id, file_bytes, file_name, prompt)
|
|
| 2045 |
except Exception: pass
|
| 2046 |
|
| 2047 |
if final_answer:
|
|
|
|
|
|
|
| 2048 |
sent = await send_with_keyboard(client, chat_id, f"💡 **نتیجه تحلیل تصویر:**\n\n{final_answer}", True)
|
| 2049 |
if sent and not creds.get("is_premium"):
|
| 2050 |
user_credits_db[str_chat_id]["file"] -= 1
|
|
|
|
| 1186 |
# ==============================================================================
|
| 1187 |
# 🟢 پارت 13: پردازش هوش مصنوعی متنی و تصویری (ترکیب کامل با Aya Expanse و Gemma)
|
| 1188 |
# ==============================================================================
|
| 1189 |
+
import re
|
| 1190 |
+
|
| 1191 |
+
def clean_math_formatting(text):
|
| 1192 |
+
"""پاکسازی و خوانا کردن فرمولهای ریاضی و علائم لاتک (LaTeX) برای پیامرسان روبیکا"""
|
| 1193 |
+
if not text: return text
|
| 1194 |
+
|
| 1195 |
+
# فاصله دادن بین عدد صحیح و کسر (مثلاً 3\frac تبدیل شود به 3 \frac)
|
| 1196 |
+
text = re.sub(r'(\d)\\frac', r'\1 \\frac', text)
|
| 1197 |
+
|
| 1198 |
+
# تبدیل کسرها به شکل خطی ساده (a/b)
|
| 1199 |
+
for _ in range(3):
|
| 1200 |
+
text = re.sub(r'\\frac\{([^{}]*)\}\{([^{}]*)\}', r'\1/\2', text)
|
| 1201 |
+
|
| 1202 |
+
replacements = {
|
| 1203 |
+
'\\times': '×', '\\div': '÷', '\\pm': '±', '\\cdot': '⋅',
|
| 1204 |
+
'\\left(': '(', '\\right)': ')', '\\left[': '[', '\\right]': ']',
|
| 1205 |
+
'\\left{': '{', '\\right}': '}', '\\sqrt': '√', '\\pi': 'π',
|
| 1206 |
+
'\\approx': '≈', '\\neq': '≠', '\\leq': '≤', '\\geq': '≥',
|
| 1207 |
+
'\\infty': '∞', '$$': '', '$': '', '\\[': '', '\\]': '', '\\(': '', '\\)': ''
|
| 1208 |
+
}
|
| 1209 |
+
|
| 1210 |
+
for old, new in replacements.items():
|
| 1211 |
+
text = text.replace(old, new)
|
| 1212 |
+
|
| 1213 |
+
return text.strip()
|
| 1214 |
|
| 1215 |
async def helper_upload_to_aya(file_bytes, file_name, mime_type):
|
| 1216 |
"""آپلود تصویر در سرور بکاپ (Aya) در صورت قطعی گیتهاب"""
|
|
|
|
| 1540 |
await send_with_keyboard(client, chat_id, "❌ تمامی سرورها شلوغ هستند یا حجم فایل بالا بود. لطفاً بعداً امتحان کنید.", False)
|
| 1541 |
return
|
| 1542 |
|
| 1543 |
+
# 🧹 اعمال فیلتر پاکسازی فرمولهای ریاضی و لاتک روی پاسخ نهایی
|
| 1544 |
+
final_answer = clean_math_formatting(final_answer)
|
| 1545 |
+
|
| 1546 |
# ذخیره پاسخ در تاریخچه چت کاربر
|
| 1547 |
history.append({"role": "model", "parts": [{"text": final_answer}]})
|
| 1548 |
user_states[chat_id]["history"] = history
|
|
|
|
| 2037 |
except Exception: pass
|
| 2038 |
|
| 2039 |
if transcribed_text:
|
| 2040 |
+
# 🧹 اعمال فیلتر پاکسازی فرمولهای ریاضی و لاتک
|
| 2041 |
+
transcribed_text = clean_math_formatting(transcribed_text)
|
| 2042 |
sent = await send_with_keyboard(client, chat_id, f"📝 **متن استخراج شده:**\n\n{transcribed_text}", True)
|
| 2043 |
if sent and not creds.get("is_premium"):
|
| 2044 |
user_credits_db[str_chat_id]["stt"] -= 1
|
|
|
|
| 2075 |
except Exception: pass
|
| 2076 |
|
| 2077 |
if final_answer:
|
| 2078 |
+
# 🧹 اعمال فیلتر پاکسازی فرمولهای ریاضی و لاتک
|
| 2079 |
+
final_answer = clean_math_formatting(final_answer)
|
| 2080 |
sent = await send_with_keyboard(client, chat_id, f"💡 **نتیجه تحلیل تصویر:**\n\n{final_answer}", True)
|
| 2081 |
if sent and not creds.get("is_premium"):
|
| 2082 |
user_credits_db[str_chat_id]["file"] -= 1
|