s12144251 commited on
Commit
9678f07
·
verified ·
1 Parent(s): 6d02fb4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -30
app.py CHANGED
@@ -1,61 +1,68 @@
1
- # Install required libraries
2
-
3
- pip install unsloth peft bitsandbytes accelerate transformers
4
-
5
  import subprocess
6
  import sys
7
 
8
- subprocess.check_call([sys.executable, "-m", "pip", "install", "unsloth", "peft", "bitsandbytes", "accelerate", "transformers"])
9
-
 
 
 
 
10
 
11
- # Import necessary modules
12
  from transformers import AutoTokenizer
13
  from unsloth import FastLanguageModel
 
14
 
15
- # Define the MedQA prompt
16
- medqa_prompt = """You are a medical QA system. Answer the following medical question clearly and in detail with complete sentences.
17
-
18
- ### Question:
19
  {}
20
-
21
- ### Answer:
22
  """
23
 
24
- # Load the model and tokenizer using unsloth
25
- model_name = "Vijayendra/Phi4-MedQA"
26
  model, tokenizer = FastLanguageModel.from_pretrained(
27
  model_name=model_name,
28
  max_seq_length=2048,
29
- dtype=None, # Use default precision
30
- load_in_4bit=True, # Enable 4-bit quantization
31
- device_map="auto" # Automatically map model to available devices
32
  )
33
 
34
- # Enable faster inference
35
  FastLanguageModel.for_inference(model)
36
 
37
- # Prepare the medical question
38
- medical_question = "What are the common symptoms of diabetes?" # Replace with your medical question
 
 
39
  inputs = tokenizer(
40
  [medqa_prompt.format(medical_question)],
41
  return_tensors="pt",
42
  padding=True,
43
  truncation=True,
44
  max_length=1024
45
- ).to("cuda") # Ensure inputs are on the GPU
46
 
47
- # Generate the output
 
 
 
 
48
  outputs = model.generate(
49
  **inputs,
50
- max_new_tokens=512, # Allow for detailed responses
51
- use_cache=True # Speeds up generation
52
  )
53
 
54
- # Decode and clean the response
55
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
56
 
57
- # Extract and print the generated answer
58
- answer_text = response.split("### Answer:")[1].strip() if "### Answer:" in response else response.strip()
 
 
 
 
59
 
60
- print(f"Question: {medical_question}")
61
- print(f"Answer: {answer_text}")
 
 
 
 
 
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