Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,95 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
# 1. تحميل التوكنايزر
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
trust_remote_code=True # مهم إذا كان هناك كود مخصص للنموذج
|
| 14 |
-
)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
|
|
|
| 24 |
def medical_chat(message, history):
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
|
|
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
fn=medical_chat,
|
| 48 |
-
title="Medical Master
|
| 49 |
-
description="
|
| 50 |
)
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
+
import json
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
from model import MedicalMasterAI # استيراد المعمارية الخاصة بك
|
| 6 |
|
| 7 |
+
# تحديد الجهاز
|
| 8 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 9 |
|
| 10 |
+
# 1. تحميل التوكنايزر المخصص الخاص بك
|
| 11 |
+
with open("tokenizer_config.json", "r", encoding="utf-8") as f:
|
| 12 |
+
vocab = json.load(f)
|
| 13 |
+
|
| 14 |
+
stoi = vocab["stoi"]
|
| 15 |
+
itos = vocab["itos"]
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# دوال التشفير وفك التشفير
|
| 18 |
+
def encode(text):
|
| 19 |
+
# إذا لم يجد الحرف، يستبدله بمسافة (0)
|
| 20 |
+
return [stoi.get(c, 0) for c in text]
|
| 21 |
+
|
| 22 |
+
def decode(ids):
|
| 23 |
+
return "".join([itos.get(str(i), "") for i in ids])
|
| 24 |
+
|
| 25 |
+
# 2. بناء وتحميل النموذج
|
| 26 |
+
try:
|
| 27 |
+
# تهيئة النموذج بنفس الأبعاد الموجودة في كودك
|
| 28 |
+
model = MedicalMasterAI(vocab_size=115, n_layer=48, n_head=8, n_embd=768)
|
| 29 |
+
|
| 30 |
+
# تحميل الأوزان (تأكد من أن اسم الملف في المساحة هو pytorch_model.bin)
|
| 31 |
+
# استخدام weights_only=True لدواعي أمنية ولمنع التحذيرات
|
| 32 |
+
state_dict = torch.load("pytorch_model.bin", map_location=device, weights_only=True)
|
| 33 |
+
model.load_state_dict(state_dict)
|
| 34 |
+
|
| 35 |
+
model.to(device)
|
| 36 |
+
model.eval() # وضع التقييم
|
| 37 |
+
model_loaded = True
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"Error loading model: {e}")
|
| 40 |
+
model_loaded = False
|
| 41 |
|
| 42 |
+
# 3. دالة المحادثة (التوليد التلقائي)
|
| 43 |
def medical_chat(message, history):
|
| 44 |
+
if not model_loaded:
|
| 45 |
+
return "حدث خطأ أثناء تحميل أوزان النموذج. تأكد من وجود ملف pytorch_model.bin"
|
| 46 |
+
|
| 47 |
+
# ملاحظة: تم إزالة \n لأنها غير موجودة في قاموسك (stoi)
|
| 48 |
+
prompt = f"Question: {message} Answer:"
|
| 49 |
+
|
| 50 |
+
# تحويل النص إلى أرقام (Tensors)
|
| 51 |
+
idx = torch.tensor([encode(prompt)], dtype=torch.long).to(device)
|
| 52 |
+
|
| 53 |
+
# عدد الأحرف التي سيولدها النموذج (بما أنه Character-level)
|
| 54 |
+
max_new_chars = 200
|
| 55 |
+
|
| 56 |
+
generated_ids = []
|
| 57 |
+
|
| 58 |
+
with torch.no_grad():
|
| 59 |
+
for _ in range(max_new_chars):
|
| 60 |
+
# قص السياق إذا تجاوز 1024 (الحد الأقصى للـ Position Embedding في كودك)
|
| 61 |
+
idx_cond = idx[:, -1024:]
|
| 62 |
+
|
| 63 |
+
# تمرير البيانات للنموذج
|
| 64 |
+
logits = model(idx_cond)
|
| 65 |
+
|
| 66 |
+
# التركيز على الحرف الأخير فقط
|
| 67 |
+
logits = logits[:, -1, :]
|
| 68 |
+
|
| 69 |
+
# تطبيق الحرارة (Temperature) لتنويع الإجابات
|
| 70 |
+
temperature = 0.8
|
| 71 |
+
logits = logits / temperature
|
| 72 |
+
|
| 73 |
+
# تحويل القيم إلى احتمالات
|
| 74 |
+
probs = F.softmax(logits, dim=-1)
|
| 75 |
+
|
| 76 |
+
# اختيار الحرف التالي
|
| 77 |
+
idx_next = torch.multinomial(probs, num_samples=1)
|
| 78 |
+
|
| 79 |
+
# إضافة الحرف الجديد للسياق
|
| 80 |
+
idx = torch.cat((idx, idx_next), dim=1)
|
| 81 |
+
generated_ids.append(idx_next.item())
|
| 82 |
+
|
| 83 |
+
# فك تشفير الأحرف المولدة فقط
|
| 84 |
+
answer = decode(generated_ids)
|
| 85 |
+
|
| 86 |
+
return answer
|
| 87 |
|
| 88 |
+
# 4. بناء واجهة Gradio
|
| 89 |
demo = gr.ChatInterface(
|
| 90 |
fn=medical_chat,
|
| 91 |
+
title="Medical Master (Custom PyTorch AI)",
|
| 92 |
+
description="نموذج مبني من الصفر (Character-Level) للإجابة على الاستفسارات.",
|
| 93 |
)
|
| 94 |
|
| 95 |
if __name__ == "__main__":
|