Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,86 +1,37 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
from fastapi import FastAPI, Request
|
| 6 |
-
from fastapi.responses import StreamingResponse, HTMLResponse
|
| 7 |
-
from gradio_client import Client
|
| 8 |
|
| 9 |
-
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
if system_prompt:
|
| 31 |
-
system_prompt += "\n" + content
|
| 32 |
-
else:
|
| 33 |
-
system_prompt = content
|
| 34 |
-
else:
|
| 35 |
-
filtered_chat.append(msg)
|
| 36 |
-
|
| 37 |
-
# 2. تحديد رسالة المستخدم الأخيرة وتاريخ المحادثة وقبلهما
|
| 38 |
-
user_message = ""
|
| 39 |
-
history_msgs = []
|
| 40 |
-
if filtered_chat:
|
| 41 |
-
user_message = filtered_chat[-1].get("content", "")
|
| 42 |
-
history_msgs = filtered_chat[:-1]
|
| 43 |
-
|
| 44 |
-
# 3. بناء الـ history بشكل أزواج متتالية [ [user, assistant], [user, assistant], ... ]
|
| 45 |
-
history = []
|
| 46 |
-
i = 0
|
| 47 |
-
while i < len(history_msgs):
|
| 48 |
-
if history_msgs[i].get("role") == "user":
|
| 49 |
-
u_content = history_msgs[i].get("content", "")
|
| 50 |
-
a_content = ""
|
| 51 |
-
if i + 1 < len(history_msgs) and history_msgs[i+1].get("role") == "assistant":
|
| 52 |
-
a_content = history_msgs[i+1].get("content", "")
|
| 53 |
-
i += 1
|
| 54 |
-
history.append([u_content, a_content])
|
| 55 |
-
i += 1
|
| 56 |
|
| 57 |
-
|
| 58 |
-
try:
|
| 59 |
-
def run_client():
|
| 60 |
-
# [تعديل] تمرير المعاملات بالترتيب (Positional) حصراً لأن السبيس لا يدعم الـ keyword arguments
|
| 61 |
-
return client.predict(
|
| 62 |
-
user_message,
|
| 63 |
-
history,
|
| 64 |
-
system_prompt
|
| 65 |
-
)
|
| 66 |
-
|
| 67 |
-
response_text = await asyncio.to_thread(run_client)
|
| 68 |
-
|
| 69 |
-
# إرسال النص الناتج على دفعات متتالية لتوافق الـ StreamingResponse
|
| 70 |
-
for char in str(response_text):
|
| 71 |
-
yield char
|
| 72 |
-
await asyncio.sleep(0.01)
|
| 73 |
-
|
| 74 |
-
except Exception as e:
|
| 75 |
-
yield f" بعتذر منك بس فيك تشغل التطبيق بكرة لانو المودل الاساسي قيد التطوير "
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
"Connection": "keep-alive",
|
| 81 |
-
"X-Accel-Buffering": "no"
|
| 82 |
-
}
|
| 83 |
-
return StreamingResponse(generate_chunks(), headers=headers)
|
| 84 |
-
|
| 85 |
-
if __name__ == "__main__":
|
| 86 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import torch
|
| 3 |
+
import kagglehub
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
model_path = kagglehub.model_download("ruicompany/onyx-code-figma/keras/default")
|
| 7 |
|
| 8 |
+
# إعداد CPU Offload مع 8-bit
|
| 9 |
+
quantization_config = BitsAndBytesConfig(
|
| 10 |
+
load_in_8bit=True,
|
| 11 |
+
llm_int8_enable_fp32_cpu_offload=True, # ⚡ السر: تفريغ على CPU
|
| 12 |
+
)
|
| 13 |
|
| 14 |
+
# تحديد كم الذاكرة لكل جهاز
|
| 15 |
+
max_memory = {
|
| 16 |
+
0: "0GB", # لا تستخدم GPU نهائياً
|
| 17 |
+
"cpu": "6GB", # خصص 6GB من RAM للنموذج
|
| 18 |
+
}
|
| 19 |
|
| 20 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
+
model_path,
|
| 22 |
+
quantization_config=quantization_config,
|
| 23 |
+
device_map="auto",
|
| 24 |
+
max_memory=max_memory,
|
| 25 |
+
offload_buffers=True,
|
| 26 |
+
offload_folder="offload", # مجلد التفريغ المؤقت
|
| 27 |
+
offload_state_dict=True,
|
| 28 |
+
torch_dtype=torch.float32, # CPU يحتاج float32
|
| 29 |
+
trust_remote_code=True,
|
| 30 |
+
)
|
| 31 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
print("✅ النموذج يعمل على CPU + 8-bit Quantization")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
sys.path.insert(0, model_path)
|
| 36 |
+
from onyx_code import launch_designer
|
| 37 |
+
launch_designer()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|