Gopal Agarwal
commited on
Commit
•
71bb1ec
1
Parent(s):
1d86626
model updated
Browse files- app.py +79 -3
- requirements.txt +5 -1
app.py
CHANGED
@@ -1,7 +1,83 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
return "Hello " + name + "!!"
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
3 |
+
import torch
|
4 |
|
5 |
+
model_name = "ruslanmv/Medical-Llama3-8B"
|
|
|
6 |
|
7 |
+
# Check for CUDA availability
|
8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
+
|
10 |
+
# Adjust configuration based on available hardware
|
11 |
+
if device == "cuda":
|
12 |
+
device_map = 'auto'
|
13 |
+
bnb_config = BitsAndBytesConfig(
|
14 |
+
load_in_4bit=True,
|
15 |
+
bnb_4bit_quant_type="nf4",
|
16 |
+
bnb_4bit_compute_dtype=torch.float16,
|
17 |
+
)
|
18 |
+
else:
|
19 |
+
device_map = None
|
20 |
+
bnb_config = None
|
21 |
+
|
22 |
+
# Load the model with adjusted parameters
|
23 |
+
model = AutoModelForCausalLM.from_pretrained(
|
24 |
+
model_name,
|
25 |
+
quantization_config=bnb_config,
|
26 |
+
trust_remote_code=True,
|
27 |
+
use_cache=False,
|
28 |
+
device_map=device_map,
|
29 |
+
low_cpu_mem_usage=True if device == "cuda" else False
|
30 |
+
)
|
31 |
+
|
32 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
33 |
+
tokenizer.pad_token = tokenizer.eos_token
|
34 |
+
|
35 |
+
tokenizer.chat_template = """
|
36 |
+
{% for message in messages %}
|
37 |
+
{% if message['role'] == 'system' %}
|
38 |
+
System: {{ message['content'] }}
|
39 |
+
{% elif message['role'] == 'user' %}
|
40 |
+
Human: {{ message['content'] }}
|
41 |
+
{% elif message['role'] == 'assistant' %}
|
42 |
+
Assistant: {{ message['content'] }}
|
43 |
+
{% endif %}
|
44 |
+
{% endfor %}
|
45 |
+
Human: {{ messages[-1]['content'] }}
|
46 |
+
Assistant:"""
|
47 |
+
|
48 |
+
def process_medical_history(prescription_details):
|
49 |
+
sys_message = '''
|
50 |
+
You are an AI Medical Assistant. Given a string of prescription details, generate a structured medical history output.
|
51 |
+
Include the following sections with appropriate headings:
|
52 |
+
1. Date of Prescription
|
53 |
+
2. Duration of Medicines
|
54 |
+
3. Problems Recognized
|
55 |
+
4. Test Results
|
56 |
+
|
57 |
+
Format the output clearly with each section having its own heading and content on a new line.
|
58 |
+
Do not include unnecessary details like additional notes, extra tokens and markers like <|endoftext|> or <|pad|>.
|
59 |
+
'''
|
60 |
+
|
61 |
+
question = f"Please format the following prescription details into a structured medical history: {prescription_details}"
|
62 |
+
|
63 |
+
messages = [
|
64 |
+
{"role": "system", "content": sys_message},
|
65 |
+
{"role": "user", "content": question}
|
66 |
+
]
|
67 |
+
|
68 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
69 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
70 |
+
|
71 |
+
with torch.no_grad():
|
72 |
+
outputs = model.generate(**inputs, max_new_tokens=300, use_cache=True)
|
73 |
+
|
74 |
+
response_text = tokenizer.batch_decode(outputs)[0].strip()
|
75 |
+
answer = response_text.split('Assistant:')[-1].strip()
|
76 |
+
|
77 |
+
# Clean up the output
|
78 |
+
answer = answer.replace('<|endoftext|>', '').replace('<|pad|>', '').strip()
|
79 |
+
|
80 |
+
return answer
|
81 |
+
|
82 |
+
demo = gr.Interface(fn=process_medical_history, inputs="text", outputs="text")
|
83 |
demo.launch()
|
requirements.txt
CHANGED
@@ -1 +1,5 @@
|
|
1 |
-
gradio
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
transformers
|
3 |
+
torch
|
4 |
+
bitsandbytes
|
5 |
+
accelerate
|