Update app.py
Browse files
app.py
CHANGED
|
@@ -1,392 +1,273 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import torch
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
import spaces
|
| 5 |
from PIL import Image
|
| 6 |
-
|
| 7 |
-
import
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
# =========================================================
|
| 11 |
-
# إعدادات النموذج
|
| 12 |
-
# =========================================================
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
# تحميل
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
# تحميل tokenizer أولاً
|
| 36 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 37 |
-
MODEL_ID,
|
| 38 |
-
trust_remote_code=True,
|
| 39 |
-
use_fast=False
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
# تحميل النموذج مع trust_remote_code=True
|
| 43 |
-
model = AutoModel.from_pretrained(
|
| 44 |
-
MODEL_ID,
|
| 45 |
-
trust_remote_code=True,
|
| 46 |
-
torch_dtype=dtype,
|
| 47 |
-
low_cpu_mem_usage=True,
|
| 48 |
-
attn_implementation="eager",
|
| 49 |
-
).eval()
|
| 50 |
-
|
| 51 |
-
if torch.cuda.is_available():
|
| 52 |
-
model = model.cuda()
|
| 53 |
-
|
| 54 |
-
print("Model loaded successfully!")
|
| 55 |
-
|
| 56 |
-
except Exception as e:
|
| 57 |
-
print(f"Error with AutoModel, trying AutoModelForCausalLM: {e}")
|
| 58 |
-
|
| 59 |
-
# محاولة بديلة مع AutoModelForCausalLM
|
| 60 |
-
try:
|
| 61 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 62 |
-
MODEL_ID,
|
| 63 |
-
trust_remote_code=True, # مهم جداً!
|
| 64 |
-
torch_dtype=dtype,
|
| 65 |
-
low_cpu_mem_usage=True,
|
| 66 |
-
attn_implementation="eager"
|
| 67 |
-
).eval()
|
| 68 |
-
|
| 69 |
-
if torch.cuda.is_available():
|
| 70 |
-
model = model.cuda()
|
| 71 |
-
|
| 72 |
-
print("Model loaded successfully with AutoModelForCausalLM!")
|
| 73 |
-
|
| 74 |
-
except Exception as e2:
|
| 75 |
-
print(f"Failed to load model: {e2}")
|
| 76 |
-
raise RuntimeError(f"Could not load model: {e2}")
|
| 77 |
-
|
| 78 |
|
| 79 |
-
# =========================================================
|
| 80 |
-
# دالة معالجة الصور
|
| 81 |
-
# =========================================================
|
| 82 |
-
|
| 83 |
-
def process_image(image_input):
|
| 84 |
-
"""معالجة الصورة للنموذج"""
|
| 85 |
-
if image_input is None:
|
| 86 |
-
return None
|
| 87 |
-
|
| 88 |
-
if isinstance(image_input, str):
|
| 89 |
-
return Image.open(image_input).convert('RGB')
|
| 90 |
-
else:
|
| 91 |
-
return image_input.convert('RGB')
|
| 92 |
|
| 93 |
-
|
| 94 |
-
# =========================================================
|
| 95 |
-
# دالة الاستدلال مع ZeroGPU
|
| 96 |
-
# =========================================================
|
| 97 |
-
|
| 98 |
-
@spaces.GPU(duration=60)
|
| 99 |
def generate_response(
|
| 100 |
-
text_input,
|
| 101 |
-
image_input,
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
max_new_tokens
|
| 105 |
):
|
| 106 |
"""
|
| 107 |
-
|
| 108 |
"""
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
return "Please provide text or image input."
|
| 112 |
|
| 113 |
try:
|
| 114 |
-
|
| 115 |
-
|
| 116 |
|
| 117 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
if image_input is not None:
|
| 119 |
-
#
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
text_input = "What is shown in this image? Please describe in detail."
|
| 124 |
-
|
| 125 |
-
# التحقق من وجود دالة chat في النموذج
|
| 126 |
-
if hasattr(model, 'chat'):
|
| 127 |
-
try:
|
| 128 |
-
# استخدام دالة chat المخصصة
|
| 129 |
-
msgs = [{"role": "user", "content": [image, text_input]}]
|
| 130 |
-
|
| 131 |
-
with torch.no_grad():
|
| 132 |
-
response = model.chat(
|
| 133 |
-
image=image,
|
| 134 |
-
msgs=msgs,
|
| 135 |
-
tokenizer=tokenizer,
|
| 136 |
-
sampling=True,
|
| 137 |
-
temperature=temperature,
|
| 138 |
-
top_p=top_p,
|
| 139 |
-
max_new_tokens=max_new_tokens
|
| 140 |
-
)
|
| 141 |
-
|
| 142 |
-
return response
|
| 143 |
-
|
| 144 |
-
except Exception as e:
|
| 145 |
-
print(f"Chat method failed: {e}")
|
| 146 |
-
# السقوط إلى الطريقة العادية
|
| 147 |
-
|
| 148 |
-
# الطريقة البديلة للصور
|
| 149 |
-
# دمج النص مع وصف الصورة
|
| 150 |
-
prompt = f"Image: [Image will be processed]\n\nQuestion: {text_input}\n\nAnswer:"
|
| 151 |
-
|
| 152 |
-
else:
|
| 153 |
-
# نص فقط
|
| 154 |
-
prompt = text_input
|
| 155 |
|
| 156 |
-
#
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
)
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
"max_new_tokens": max_new_tokens,
|
| 171 |
-
"temperature": temperature if temperature > 0 else 1e-7,
|
| 172 |
-
"top_p": top_p,
|
| 173 |
-
"do_sample": temperature > 0,
|
| 174 |
-
"pad_token_id": tokenizer.pad_token_id if tokenizer.pad_token_id is not None else tokenizer.eos_token_id,
|
| 175 |
-
"eos_token_id": tokenizer.eos_token_id,
|
| 176 |
-
}
|
| 177 |
|
| 178 |
# التوليد
|
| 179 |
with torch.no_grad():
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
-
# فك
|
| 183 |
-
response =
|
| 184 |
-
|
| 185 |
skip_special_tokens=True
|
| 186 |
-
)
|
| 187 |
|
| 188 |
-
return response
|
| 189 |
|
| 190 |
except Exception as e:
|
| 191 |
-
|
| 192 |
-
traceback.print_exc()
|
| 193 |
-
return f"Error: {str(e)}"
|
| 194 |
|
| 195 |
|
| 196 |
-
#
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
def update_examples_visibility(show_examples):
|
| 206 |
-
"""تحديث رؤية الأمثلة"""
|
| 207 |
-
return gr.update(visible=show_examples)
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
# =========================================================
|
| 211 |
-
# واجهة Gradio
|
| 212 |
-
# =========================================================
|
| 213 |
-
|
| 214 |
-
def create_demo():
|
| 215 |
-
"""إنشاء واجهة Gradio البسيطة"""
|
| 216 |
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
gr.Markdown(
|
| 233 |
-
"""
|
| 234 |
-
# 🤖 MiniCPM-o-2.6 - Multimodal AI Assistant
|
| 235 |
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
"""
|
| 243 |
-
)
|
| 244 |
-
|
| 245 |
-
with gr.Row():
|
| 246 |
-
# العمود الرئيسي
|
| 247 |
-
with gr.Column(scale=2):
|
| 248 |
-
with gr.Group():
|
| 249 |
-
text_input = gr.Textbox(
|
| 250 |
-
label="💭 Text Input",
|
| 251 |
-
placeholder="Enter your question or prompt here...\nYou can ask about images, request text generation, or have a conversation.",
|
| 252 |
-
lines=4,
|
| 253 |
-
elem_id="text_input"
|
| 254 |
-
)
|
| 255 |
-
|
| 256 |
-
image_input = gr.Image(
|
| 257 |
-
label="📷 Image Input (Optional)",
|
| 258 |
-
type="pil",
|
| 259 |
-
elem_id="image_input"
|
| 260 |
-
)
|
| 261 |
-
|
| 262 |
-
with gr.Row():
|
| 263 |
-
submit_btn = gr.Button(
|
| 264 |
-
"🚀 Generate Response",
|
| 265 |
-
variant="primary",
|
| 266 |
-
scale=2
|
| 267 |
-
)
|
| 268 |
-
clear_btn = gr.Button(
|
| 269 |
-
"🗑️ Clear All",
|
| 270 |
-
variant="secondary",
|
| 271 |
-
scale=1
|
| 272 |
-
)
|
| 273 |
-
|
| 274 |
-
output = gr.Textbox(
|
| 275 |
-
label="🤖 AI Response",
|
| 276 |
-
lines=10,
|
| 277 |
-
interactive=False,
|
| 278 |
-
elem_id="output"
|
| 279 |
-
)
|
| 280 |
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
value=0.9,
|
| 300 |
-
step=0.05,
|
| 301 |
-
info="Controls diversity of output"
|
| 302 |
-
)
|
| 303 |
-
|
| 304 |
-
max_new_tokens = gr.Slider(
|
| 305 |
-
label="Max New Tokens",
|
| 306 |
-
minimum=50,
|
| 307 |
-
maximum=2048,
|
| 308 |
-
value=512,
|
| 309 |
-
step=50,
|
| 310 |
-
info="Maximum length of generated response"
|
| 311 |
-
)
|
| 312 |
|
| 313 |
-
gr.
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
- Request explanations
|
| 320 |
-
- Generate creative content
|
| 321 |
-
|
| 322 |
-
**Image Understanding:**
|
| 323 |
-
- Upload an image
|
| 324 |
-
- Ask about contents
|
| 325 |
-
- Request OCR/text extraction
|
| 326 |
-
- Get detailed descriptions
|
| 327 |
-
|
| 328 |
-
**Languages:**
|
| 329 |
-
- English, Chinese, Arabic
|
| 330 |
-
- And many more!
|
| 331 |
-
"""
|
| 332 |
)
|
|
|
|
|
|
|
|
|
|
| 333 |
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
gr.
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
["Create a healthy meal plan for one week.", None],
|
| 343 |
-
["Translate 'Hello, how are you?' to French, Spanish, and Arabic.", None],
|
| 344 |
-
],
|
| 345 |
-
inputs=[text_input, image_input],
|
| 346 |
-
outputs=output,
|
| 347 |
-
fn=lambda t, i: generate_response(t, i, 0.7, 0.9, 512),
|
| 348 |
-
cache_examples=False,
|
| 349 |
-
label="Click any example to try it"
|
| 350 |
)
|
| 351 |
-
|
| 352 |
-
# ربط الأحداث
|
| 353 |
-
submit_btn.click(
|
| 354 |
-
fn=generate_response,
|
| 355 |
-
inputs=[text_input, image_input, temperature, top_p, max_new_tokens],
|
| 356 |
-
outputs=output,
|
| 357 |
-
api_name="generate"
|
| 358 |
-
)
|
| 359 |
-
|
| 360 |
-
text_input.submit(
|
| 361 |
-
fn=generate_response,
|
| 362 |
-
inputs=[text_input, image_input, temperature, top_p, max_new_tokens],
|
| 363 |
-
outputs=output
|
| 364 |
-
)
|
| 365 |
-
|
| 366 |
-
clear_btn.click(
|
| 367 |
-
fn=clear_all,
|
| 368 |
-
inputs=[],
|
| 369 |
-
outputs=[text_input, image_input, output]
|
| 370 |
-
)
|
| 371 |
-
|
| 372 |
-
# رسالة ترحيبية عند التحميل
|
| 373 |
-
demo.load(
|
| 374 |
-
lambda: gr.Info("Model is loading... This may take a moment on first use."),
|
| 375 |
-
inputs=None,
|
| 376 |
-
outputs=None
|
| 377 |
-
)
|
| 378 |
|
| 379 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
|
| 381 |
|
| 382 |
-
# =========================================================
|
| 383 |
# تشغيل التطبيق
|
| 384 |
-
# =========================================================
|
| 385 |
-
|
| 386 |
if __name__ == "__main__":
|
| 387 |
-
demo
|
| 388 |
demo.launch(
|
| 389 |
-
|
| 390 |
show_error=True,
|
| 391 |
-
|
| 392 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
import spaces
|
| 4 |
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
import os
|
| 7 |
+
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# استيراد المكتبات الضرورية من Uni-MoE
|
| 10 |
+
try:
|
| 11 |
+
from uni_moe.model.processing_qwen2_vl import Qwen2VLProcessor
|
| 12 |
+
from uni_moe.model.modeling_out import GrinQwen2VLOutForConditionalGeneration
|
| 13 |
+
from uni_moe.qwen_vl_utils import process_mm_info
|
| 14 |
+
from uni_moe.model import deepspeed_moe_inference_utils
|
| 15 |
+
except ImportError:
|
| 16 |
+
print("⚠️ Warning: Uni-MoE libraries not fully imported. Some features may not work.")
|
| 17 |
|
| 18 |
+
# تحميل النموذج
|
| 19 |
+
MODEL_NAME = "HIT-TMG/Uni-MoE-2.0-Omni"
|
| 20 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 21 |
|
| 22 |
+
print(f"🚀 Loading model: {MODEL_NAME}")
|
| 23 |
+
print(f"📍 Device: {device}")
|
| 24 |
|
| 25 |
+
# تحميل المعالج والنموذج
|
| 26 |
+
try:
|
| 27 |
+
processor = Qwen2VLProcessor.from_pretrained(MODEL_NAME)
|
| 28 |
+
model = GrinQwen2VLOutForConditionalGeneration.from_pretrained(
|
| 29 |
+
MODEL_NAME,
|
| 30 |
+
torch_dtype=torch.bfloat16 if device == "cuda" else torch.float32,
|
| 31 |
+
device_map="auto"
|
| 32 |
+
)
|
| 33 |
+
if device == "cuda":
|
| 34 |
+
model = model.cuda()
|
| 35 |
|
| 36 |
+
# تعيين data_args
|
| 37 |
+
processor.data_args = model.config
|
| 38 |
+
print("✅ Model loaded successfully!")
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(f"❌ Error loading model: {str(e)}")
|
| 41 |
+
processor = None
|
| 42 |
+
model = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
@spaces.GPU(duration=120) # استخدام ZeroGPU لمدة 120 ثانية
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
def generate_response(
|
| 47 |
+
text_input: str,
|
| 48 |
+
image_input: Image.Image = None,
|
| 49 |
+
audio_input: str = None,
|
| 50 |
+
temperature: float = 1.0,
|
| 51 |
+
max_new_tokens: int = 512
|
| 52 |
):
|
| 53 |
"""
|
| 54 |
+
توليد استجابة من النموذج بناءً على المدخلات المختلفة
|
| 55 |
"""
|
| 56 |
+
if model is None or processor is None:
|
| 57 |
+
return "❌ النموذج غير متاح حالياً. يرجى المحاولة لاحقاً."
|
|
|
|
| 58 |
|
| 59 |
try:
|
| 60 |
+
# بناء رسالة المستخدم
|
| 61 |
+
content = []
|
| 62 |
|
| 63 |
+
# إضافة النص
|
| 64 |
+
if text_input:
|
| 65 |
+
content.append({"type": "text", "text": text_input})
|
| 66 |
+
|
| 67 |
+
# إضافة الصورة
|
| 68 |
if image_input is not None:
|
| 69 |
+
# حفظ الصورة مؤقتاً
|
| 70 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_img:
|
| 71 |
+
image_input.save(tmp_img.name)
|
| 72 |
+
content.append({"type": "image", "image": tmp_img.name})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
# إضافة الصوت
|
| 75 |
+
if audio_input is not None:
|
| 76 |
+
content.append({"type": "audio", "audio": audio_input})
|
| 77 |
+
|
| 78 |
+
if not content:
|
| 79 |
+
return "⚠️ يرجى إدخال نص أو صورة أو صوت."
|
| 80 |
+
|
| 81 |
+
# بناء الرسائل
|
| 82 |
+
messages = [{
|
| 83 |
+
"role": "user",
|
| 84 |
+
"content": content
|
| 85 |
+
}]
|
| 86 |
+
|
| 87 |
+
# معالجة الرسائل
|
| 88 |
+
texts = processor.apply_chat_template(
|
| 89 |
+
messages,
|
| 90 |
+
tokenize=False,
|
| 91 |
+
add_generation_prompt=True
|
| 92 |
)
|
| 93 |
|
| 94 |
+
# استبدال العلامات الخاصة
|
| 95 |
+
texts = texts.replace(
|
| 96 |
+
"<image>", "<|vision_start|><|image_pad|><|vision_end|>"
|
| 97 |
+
).replace(
|
| 98 |
+
"<audio>", "<|audio_start|><|audio_pad|><|audio_end|>"
|
| 99 |
+
).replace(
|
| 100 |
+
"<video>", "<|vision_start|><|video_pad|><|vision_end|>"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# معالجة الوسائط
|
| 104 |
+
image_inputs, video_inputs, audio_inputs = process_mm_info(messages)
|
| 105 |
+
|
| 106 |
+
# تجهيز المدخلات
|
| 107 |
+
inputs = processor(
|
| 108 |
+
text=texts,
|
| 109 |
+
images=image_inputs,
|
| 110 |
+
videos=video_inputs,
|
| 111 |
+
audios=audio_inputs,
|
| 112 |
+
padding=True,
|
| 113 |
+
return_tensors="pt",
|
| 114 |
+
)
|
| 115 |
|
| 116 |
+
inputs["input_ids"] = inputs["input_ids"].unsqueeze(0)
|
| 117 |
+
inputs = inputs.to(device=model.device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
# التوليد
|
| 120 |
with torch.no_grad():
|
| 121 |
+
output_ids = model.generate(
|
| 122 |
+
**inputs,
|
| 123 |
+
use_cache=True,
|
| 124 |
+
pad_token_id=processor.tokenizer.eos_token_id,
|
| 125 |
+
max_new_tokens=max_new_tokens,
|
| 126 |
+
temperature=temperature,
|
| 127 |
+
do_sample=True
|
| 128 |
+
)
|
| 129 |
|
| 130 |
+
# فك تشفير النتي��ة
|
| 131 |
+
response = processor.batch_decode(
|
| 132 |
+
output_ids[:, inputs["input_ids"].shape[-1]:],
|
| 133 |
skip_special_tokens=True
|
| 134 |
+
)[0]
|
| 135 |
|
| 136 |
+
return response
|
| 137 |
|
| 138 |
except Exception as e:
|
| 139 |
+
return f"❌ حدث خطأ: {str(e)}"
|
|
|
|
|
|
|
| 140 |
|
| 141 |
|
| 142 |
+
# إنشاء واجهة Gradio
|
| 143 |
+
with gr.Blocks(
|
| 144 |
+
title="Uni-MoE 2.0 Omni Demo",
|
| 145 |
+
theme=gr.themes.Soft(),
|
| 146 |
+
css="""
|
| 147 |
+
.rtl { direction: rtl; text-align: right; }
|
| 148 |
+
.main-header { text-align: center; margin-bottom: 2rem; }
|
| 149 |
+
"""
|
| 150 |
+
) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
+
gr.Markdown("""
|
| 153 |
+
<div class="main-header">
|
| 154 |
+
|
| 155 |
+
# 🚀 Uni-MoE 2.0 Omni Demo
|
| 156 |
+
|
| 157 |
+
نموذج متعدد الوسائط متقدم يدعم فهم وتوليد **النصوص والصور والصوت**
|
| 158 |
+
|
| 159 |
+
An advanced omnimodal model supporting understanding and generation of **text, images, and audio**
|
| 160 |
+
|
| 161 |
+
</div>
|
| 162 |
+
""")
|
| 163 |
+
|
| 164 |
+
with gr.Row():
|
| 165 |
+
with gr.Column(scale=1):
|
| 166 |
+
gr.Markdown("### 📝 المدخلات / Inputs")
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
+
text_input = gr.Textbox(
|
| 169 |
+
label="النص / Text",
|
| 170 |
+
placeholder="اكتب سؤالك أو وصفك هنا... / Enter your question or description here...",
|
| 171 |
+
lines=3,
|
| 172 |
+
rtl=True
|
| 173 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
+
image_input = gr.Image(
|
| 176 |
+
label="الصورة (اختياري) / Image (Optional)",
|
| 177 |
+
type="pil"
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
audio_input = gr.Audio(
|
| 181 |
+
label="الصوت (اختياري) / Audio (Optional)",
|
| 182 |
+
type="filepath"
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
with gr.Accordion("⚙️ إعدادات متقدمة / Advanced Settings", open=False):
|
| 186 |
+
temperature = gr.Slider(
|
| 187 |
+
minimum=0.1,
|
| 188 |
+
maximum=2.0,
|
| 189 |
+
value=1.0,
|
| 190 |
+
step=0.1,
|
| 191 |
+
label="Temperature"
|
| 192 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
+
max_tokens = gr.Slider(
|
| 195 |
+
minimum=64,
|
| 196 |
+
maximum=2048,
|
| 197 |
+
value=512,
|
| 198 |
+
step=64,
|
| 199 |
+
label="Max New Tokens"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
)
|
| 201 |
+
|
| 202 |
+
submit_btn = gr.Button("🎯 توليد / Generate", variant="primary")
|
| 203 |
+
clear_btn = gr.Button("🗑️ مسح / Clear")
|
| 204 |
|
| 205 |
+
with gr.Column(scale=1):
|
| 206 |
+
gr.Markdown("### 💬 النتيجة / Output")
|
| 207 |
+
|
| 208 |
+
output = gr.Textbox(
|
| 209 |
+
label="الاستجابة / Response",
|
| 210 |
+
lines=15,
|
| 211 |
+
show_copy_button=True,
|
| 212 |
+
rtl=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
+
# أمثلة
|
| 216 |
+
gr.Markdown("### 📚 أمثلة / Examples")
|
| 217 |
+
gr.Examples(
|
| 218 |
+
examples=[
|
| 219 |
+
["ما هي عاصمة مصر؟", None, None],
|
| 220 |
+
["صف هذه الصورة بالتفصيل", "https://picsum.photos/400/300", None],
|
| 221 |
+
["What is the capital of France?", None, None],
|
| 222 |
+
["Describe this image in detail", "https://picsum.photos/400/300", None],
|
| 223 |
+
],
|
| 224 |
+
inputs=[text_input, image_input, audio_input],
|
| 225 |
+
)
|
| 226 |
+
|
| 227 |
+
# معلومات إضافية
|
| 228 |
+
gr.Markdown("""
|
| 229 |
+
---
|
| 230 |
+
### ℹ️ معلومات / Information
|
| 231 |
+
|
| 232 |
+
**Uni-MoE 2.0 Omni** هو نموذج لغوي متعدد الوسائط (Omnimodal) مبني على معماريات:
|
| 233 |
+
- 🧠 **Mixture-of-Experts (MoE)** لكفاءة الحوسبة
|
| 234 |
+
- 🔄 **Qwen2.5-7B** كقاعدة أساسية
|
| 235 |
+
- 🎯 **Omni-Modality 3D RoPE** لمحاذاة متعددة الوسائط
|
| 236 |
+
|
| 237 |
+
**القدرات:**
|
| 238 |
+
- ✅ فهم النصوص والصور والصوت والفيديو
|
| 239 |
+
- ✅ توليد النصوص والصور والصوت
|
| 240 |
+
- ✅ استدلال متعدد الوسائط
|
| 241 |
+
|
| 242 |
+
📄 **ورقة بحثية:** [arXiv:2511.12609](https://arxiv.org/abs/2511.12609)
|
| 243 |
+
|
| 244 |
+
🔗 **GitHub:** [HITsz-TMG/Uni-MoE](https://github.com/HITsz-TMG/Uni-MoE)
|
| 245 |
+
|
| 246 |
+
---
|
| 247 |
+
<p style="text-align: center; color: #666;">
|
| 248 |
+
تم إنشاؤه باستخدام Gradio و ZeroGPU 🚀
|
| 249 |
+
</p>
|
| 250 |
+
""")
|
| 251 |
+
|
| 252 |
+
# ربط الأحداث
|
| 253 |
+
submit_btn.click(
|
| 254 |
+
fn=generate_response,
|
| 255 |
+
inputs=[text_input, image_input, audio_input, temperature, max_tokens],
|
| 256 |
+
outputs=output
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
clear_btn.click(
|
| 260 |
+
fn=lambda: (None, None, None, None),
|
| 261 |
+
outputs=[text_input, image_input, audio_input, output]
|
| 262 |
+
)
|
| 263 |
|
| 264 |
|
|
|
|
| 265 |
# تشغيل التطبيق
|
|
|
|
|
|
|
| 266 |
if __name__ == "__main__":
|
| 267 |
+
demo.queue(max_size=10)
|
| 268 |
demo.launch(
|
| 269 |
+
share=False,
|
| 270 |
show_error=True,
|
| 271 |
+
server_name="0.0.0.0",
|
| 272 |
+
server_port=7860
|
| 273 |
+
)
|