s12144251 commited on
Commit
61a2851
·
verified ·
1 Parent(s): 9678f07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -37
app.py CHANGED
@@ -1,68 +1,57 @@
 
 
1
  import subprocess
2
  import sys
3
 
4
- # تثبيت الحزم المطلوبة
5
- try:
6
- subprocess.check_call([sys.executable, "-m", "pip", "install", "unsloth", "peft", "bitsandbytes", "accelerate", "transformers"])
7
- except subprocess.CalledProcessError as e:
8
- print("فشل في تثبيت الحزم:", e)
9
- sys.exit(1)
10
 
11
- # استيراد المكتبات
12
  from transformers import AutoTokenizer
13
  from unsloth import FastLanguageModel
14
- import torch
15
 
16
- # قالب السؤال والإجابة
17
- medqa_prompt = """أنت نظام للإجابة على الأسئلة الطبية. أجب عن السؤال التالي بشكل واضح ومفصل وبجمل كاملة.
18
- ### السؤال:
19
  {}
20
- ### الإجابة:
21
  """
22
 
23
- # تحميل النموذج والمحول tokenizer
24
- model_name = "Vijayendra/Phi4-MedQA"
25
  model, tokenizer = FastLanguageModel.from_pretrained(
26
  model_name=model_name,
27
  max_seq_length=2048,
28
- dtype=None, # الدقة الافتراضية
29
- load_in_4bit=True, # تحميل بخفة 4 بت
30
- device_map="auto" # يوزع النموذج تلقائياً على الأجهزة المتوفرة (مثل GPU)
31
  )
32
 
33
- # تفعيل وضع الاستدلال السريع
34
  FastLanguageModel.for_inference(model)
35
 
36
- # السؤال الطبي
37
- medical_question = "ما هي الأعراض الشائعة لمرض السكري؟"
38
-
39
- # تجهيز الإدخال
40
  inputs = tokenizer(
41
  [medqa_prompt.format(medical_question)],
42
  return_tensors="pt",
43
  padding=True,
44
  truncation=True,
45
  max_length=1024
46
- )
47
-
48
- # التأكد من إرسال الإدخال إلى الـ GPU إذا كان متوفر
49
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
50
- inputs = {k: v.to(device) for k, v in inputs.items()}
51
 
52
- # توليد الإجابة
53
  outputs = model.generate(
54
  **inputs,
55
- max_new_tokens=512, # عدد الكلمات الجديدة المسموح بها في الإجابة
56
- use_cache=True
57
  )
58
 
59
- # فك ترميز الإجابة من التوكنز إلى نص
60
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
61
 
62
- # استخراج فقط نص الإجابة
63
- answer_text = response.split("### الإجابة:")[1].strip() if "### الإجابة:" in response else response.strip()
64
-
65
- # طباعة النتيجة
66
- print(f"\nالسؤال: {medical_question}")
67
- print(f"الإجابة: {answer_text}")
68
 
 
 
 
1
+ pip install unsloth peft bitsandbytes accelerate transformers
2
+
3
  import subprocess
4
  import sys
5
 
6
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "unsloth", "peft", "bitsandbytes", "accelerate", "transformers"])
7
+
 
 
 
 
8
 
9
+ # Import necessary modules
10
  from transformers import AutoTokenizer
11
  from unsloth import FastLanguageModel
 
12
 
13
+ # Define the MedQA prompt
14
+ medqa_prompt = """You are a medical QA system. Answer the following medical question clearly and in detail with complete sentences.
15
+ ### Question:
16
  {}
17
+ ### Answer:
18
  """
19
 
20
+ # Load the model and tokenizer using unsloth
21
+ model_name = "Vijayendra/Phi4-MedQA"
22
  model, tokenizer = FastLanguageModel.from_pretrained(
23
  model_name=model_name,
24
  max_seq_length=2048,
25
+ dtype=None, # Use default precision
26
+ load_in_4bit=True, # Enable 4-bit quantization
27
+ device_map="auto" # Automatically map model to available devices
28
  )
29
 
30
+ # Enable faster inference
31
  FastLanguageModel.for_inference(model)
32
 
33
+ # Prepare the medical question
34
+ medical_question = "What are the common symptoms of diabetes?" # Replace with your medical question
 
 
35
  inputs = tokenizer(
36
  [medqa_prompt.format(medical_question)],
37
  return_tensors="pt",
38
  padding=True,
39
  truncation=True,
40
  max_length=1024
41
+ ).to("cuda") # Ensure inputs are on the GPU
 
 
 
 
42
 
43
+ # Generate the output
44
  outputs = model.generate(
45
  **inputs,
46
+ max_new_tokens=512, # Allow for detailed responses
47
+ use_cache=True # Speeds up generation
48
  )
49
 
50
+ # Decode and clean the response
51
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
52
 
53
+ # Extract and print the generated answer
54
+ answer_text = response.split("### Answer:")[1].strip() if "### Answer:" in response else response.strip()
 
 
 
 
55
 
56
+ print(f"Question: {medical_question}")
57
+ print(f"Answer: {answer_text}")