gijl commited on
Commit
024a042
·
verified ·
1 Parent(s): 4ce8159

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -34
app.py CHANGED
@@ -1,55 +1,52 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
  import torch
4
 
5
- # تحديد الجهاز (GPU إذا توفر، وإلا CPU)
6
- device = 0 if torch.cuda.is_available() else -1
7
-
8
- # تحميل النموذج
9
  model_id = "gijl/Medical-Master-1.5B"
10
 
11
- try:
12
- pipe = pipeline(
13
- "text-generation",
14
- model=model_id,
15
- device=device,
16
- # استخدام float32 لتجنب مشاكل التوافق على CPU المساحات المجانية
17
- model_kwargs={"torch_dtype": torch.float32}
18
- )
19
- except Exception as e:
20
- print(f"Error loading model: {e}")
21
- pipe = None
 
 
 
 
 
22
 
23
  def medical_chat(message, history):
24
- if pipe is None:
25
- return "النموذج لا يزال قيد التحميل أو هناك مشكلة في الذاكرة."
26
-
27
  prompt = f"Question: {message}\nAnswer:"
28
 
29
- # تقليل عدد التوكنز لتسريع الاستجابة وتجنب المهلة الزمنية (Timeout)
30
  results = pipe(
31
  prompt,
32
- max_new_tokens=200,
33
  do_sample=True,
34
- temperature=0.7,
35
- top_p=0.9
 
 
36
  )
37
 
38
- generated_text = results[0]['generated_text']
39
- # استخراج الإجابة فقط
40
- if "Answer:" in generated_text:
41
- answer = generated_text.split("Answer:")[-1].strip()
42
- else:
43
- answer = generated_text
44
 
45
- return answer
46
 
47
- # بناء الواجهة بدون وسيط theme لتجنب الخطأ
48
  demo = gr.ChatInterface(
49
  fn=medical_chat,
50
- title="Medical Master 1.5B",
51
- description="مساعد طبي ذكي - للاستخدام التعليمي فقط",
52
- examples=["What are the symptoms of diabetes?", "كيف أحافظ على صحة القلب؟"],
53
  )
54
 
55
  if __name__ == "__main__":
 
1
  import gradio as gr
2
+ from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
 
 
 
 
 
5
  model_id = "gijl/Medical-Master-1.5B"
6
 
7
+ # 1. تحميل التوكنايزر والنموذج بشكل منفصل لضمان الدقة
8
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
9
+ model = AutoModelForCausalLM.from_pretrained(
10
+ model_id,
11
+ torch_dtype=torch.float32,
12
+ low_cpu_mem_usage=True,
13
+ trust_remote_code=True # مهم إذا كان هناك كود مخصص للنموذج
14
+ )
15
+
16
+ # 2. إعداد الـ Pipeline مع إيقاف التكرار
17
+ pipe = pipeline(
18
+ "text-generation",
19
+ model=model,
20
+ tokenizer=tokenizer,
21
+ device=-1 # استخدام CPU للمساحات المجانية
22
+ )
23
 
24
  def medical_chat(message, history):
25
+ # تجربة برومبت بسيط جداً بدون تعقيد
 
 
26
  prompt = f"Question: {message}\nAnswer:"
27
 
 
28
  results = pipe(
29
  prompt,
30
+ max_new_tokens=100,
31
  do_sample=True,
32
+ temperature=0.8,
33
+ top_p=0.9,
34
+ repetition_penalty=1.2, # لمنع التكرار أو الفراغ
35
+ return_full_text=False # لكي يعطينا الإجابة فقط بدون السؤال
36
  )
37
 
38
+ response = results[0]['generated_text'].strip()
39
+
40
+ # إذا كان الرد لا يزال فارغاً، سنعيد رسالة توضيحية
41
+ if not response:
42
+ return "النموذج لم يولد أي نص. تأكد من أن ملف pytorch_model.bin متوافق مع config.json في مستودع النموذج."
 
43
 
44
+ return response
45
 
 
46
  demo = gr.ChatInterface(
47
  fn=medical_chat,
48
+ title="Medical Master Testing Mode",
49
+ description="اختبار المخرجات الخام للنموذج"
 
50
  )
51
 
52
  if __name__ == "__main__":