Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,68 +1,57 @@
|
|
|
|
|
|
1 |
import subprocess
|
2 |
import sys
|
3 |
|
4 |
-
|
5 |
-
|
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 |
-
#
|
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, #
|
30 |
-
device_map="auto"
|
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("###
|
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}")
|