techindia2025 commited on
Commit
10736b1
·
verified ·
1 Parent(s): 1cb1a8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -86
app.py CHANGED
@@ -3,8 +3,8 @@ import torch
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
  import spaces
5
 
6
- # Model configuration - Using only Me-LLaMA 13B-chat
7
- ME_LLAMA_MODEL = "clinicalnlplab/me-llama-13b-chat"
8
 
9
  # System prompts for different phases
10
  CONSULTATION_PROMPT = """You are a professional virtual doctor. Your goal is to collect detailed information about the user's health condition, symptoms, medical history, medications, lifestyle, and other relevant data.
@@ -36,6 +36,7 @@ patient_data = []
36
 
37
  def build_me_llama_prompt(system_prompt, history, user_input):
38
  """Format the conversation for Me-LLaMA chat model."""
 
39
  prompt = f"<s>[INST] <<SYS>>\n{system_prompt}\n<</SYS>>\n\n"
40
 
41
  # Add conversation history
@@ -53,15 +54,30 @@ def load_model_if_needed():
53
  global me_llama_model, me_llama_tokenizer
54
 
55
  if me_llama_model is None:
56
- print("Loading Me-LLaMA 13B-chat model...")
57
- me_llama_tokenizer = AutoTokenizer.from_pretrained(ME_LLAMA_MODEL)
58
- me_llama_model = AutoModelForCausalLM.from_pretrained(
59
- ME_LLAMA_MODEL,
60
- torch_dtype=torch.float16,
61
- device_map="auto",
62
- trust_remote_code=True
63
- )
64
- print("Me-LLaMA 13B-chat model loaded successfully!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  @spaces.GPU
67
  def generate_medicine_suggestions(patient_info):
@@ -96,91 +112,95 @@ def generate_response(message, history):
96
  """Generate response using only Me-LLaMA for both consultation and medicine suggestions."""
97
  global conversation_turns, patient_data
98
 
99
- # Load model if needed
100
- load_model_if_needed()
101
-
102
- # Track conversation turns
103
- conversation_turns += 1
104
-
105
- # Store patient data
106
- patient_data.append(message)
107
-
108
- # Phase 1-3: Information gathering
109
- if conversation_turns < 4:
110
- # Build consultation prompt
111
- prompt = build_me_llama_prompt(CONSULTATION_PROMPT, history, message)
112
 
113
- inputs = me_llama_tokenizer(prompt, return_tensors="pt")
 
114
 
115
- # Move inputs to the same device as the model
116
- if torch.cuda.is_available():
117
- inputs = {k: v.to(me_llama_model.device) for k, v in inputs.items()}
118
 
119
- # Generate consultation response
120
- with torch.no_grad():
121
- outputs = me_llama_model.generate(
122
- inputs["input_ids"],
123
- attention_mask=inputs["attention_mask"],
124
- max_new_tokens=400,
125
- temperature=0.7,
126
- top_p=0.9,
127
- do_sample=True,
128
- pad_token_id=me_llama_tokenizer.eos_token_id
129
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
 
131
- # Decode response
132
- full_response = me_llama_tokenizer.decode(outputs[0], skip_special_tokens=False)
133
- response = full_response.split('[/INST]')[-1].split('</s>')[0].strip()
134
-
135
- return response
136
-
137
- # Phase 4+: Summary and medicine suggestions
138
- else:
139
- # First, get summary from consultation
140
- summary_prompt = build_me_llama_prompt(
141
- CONSULTATION_PROMPT + "\n\nNow summarize what you've learned and suggest when professional care may be needed.",
142
- history,
143
- message
144
- )
145
-
146
- inputs = me_llama_tokenizer(summary_prompt, return_tensors="pt")
147
-
148
- if torch.cuda.is_available():
149
- inputs = {k: v.to(me_llama_model.device) for k, v in inputs.items()}
150
-
151
- # Generate summary
152
- with torch.no_grad():
153
- outputs = me_llama_model.generate(
154
- inputs["input_ids"],
155
- attention_mask=inputs["attention_mask"],
156
- max_new_tokens=400,
157
- temperature=0.7,
158
- top_p=0.9,
159
- do_sample=True,
160
- pad_token_id=me_llama_tokenizer.eos_token_id
161
  )
162
-
163
- summary_response = me_llama_tokenizer.decode(outputs[0], skip_special_tokens=False)
164
- summary = summary_response.split('[/INST]')[-1].split('</s>')[0].strip()
165
-
166
- # Then get medicine suggestions using the same model
167
- full_patient_info = "\n".join(patient_data) + f"\n\nMedical Summary: {summary}"
168
- medicine_suggestions = generate_medicine_suggestions(full_patient_info)
169
-
170
- # Combine both responses
171
- final_response = (
172
- f"**MEDICAL SUMMARY:**\n{summary}\n\n"
173
- f"**MEDICATION AND HOME CARE SUGGESTIONS:**\n{medicine_suggestions}\n\n"
174
- f"**DISCLAIMER:** This is AI-generated advice for informational purposes only. Please consult a licensed healthcare provider for proper medical diagnosis and treatment."
175
- )
176
-
177
- return final_response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
 
179
  # Create the Gradio interface
180
  demo = gr.ChatInterface(
181
  fn=generate_response,
182
  title="🏥 Complete Medical Assistant - Me-LLaMA 13B",
183
- description="Comprehensive medical consultation powered by Me-LLaMA 13B-chat. One model handles both consultation and medicine suggestions. Tell me about your symptoms!",
184
  examples=[
185
  "I have a persistent cough and sore throat for 3 days",
186
  "I've been having severe headaches and feel dizzy",
 
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
  import spaces
5
 
6
+ # Model configuration - Using correct Me-LLaMA model identifier
7
+ ME_LLAMA_MODEL = "clinicalnlplab/me-llama-13b" # Corrected model name
8
 
9
  # System prompts for different phases
10
  CONSULTATION_PROMPT = """You are a professional virtual doctor. Your goal is to collect detailed information about the user's health condition, symptoms, medical history, medications, lifestyle, and other relevant data.
 
36
 
37
  def build_me_llama_prompt(system_prompt, history, user_input):
38
  """Format the conversation for Me-LLaMA chat model."""
39
+ # Use standard Llama-2 chat format since Me-LLaMA is based on Llama-2
40
  prompt = f"<s>[INST] <<SYS>>\n{system_prompt}\n<</SYS>>\n\n"
41
 
42
  # Add conversation history
 
54
  global me_llama_model, me_llama_tokenizer
55
 
56
  if me_llama_model is None:
57
+ print("Loading Me-LLaMA 13B model...")
58
+ try:
59
+ me_llama_tokenizer = AutoTokenizer.from_pretrained(
60
+ ME_LLAMA_MODEL,
61
+ trust_remote_code=True
62
+ )
63
+ me_llama_model = AutoModelForCausalLM.from_pretrained(
64
+ ME_LLAMA_MODEL,
65
+ torch_dtype=torch.float16,
66
+ device_map="auto",
67
+ trust_remote_code=True
68
+ )
69
+ print("Me-LLaMA 13B model loaded successfully!")
70
+ except Exception as e:
71
+ print(f"Error loading model: {e}")
72
+ # Fallback to a working medical model
73
+ print("Falling back to Llama-2-7b-chat-hf...")
74
+ me_llama_tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf")
75
+ me_llama_model = AutoModelForCausalLM.from_pretrained(
76
+ "meta-llama/Llama-2-7b-chat-hf",
77
+ torch_dtype=torch.float16,
78
+ device_map="auto"
79
+ )
80
+ print("Fallback model loaded successfully!")
81
 
82
  @spaces.GPU
83
  def generate_medicine_suggestions(patient_info):
 
112
  """Generate response using only Me-LLaMA for both consultation and medicine suggestions."""
113
  global conversation_turns, patient_data
114
 
115
+ try:
116
+ # Load model if needed
117
+ load_model_if_needed()
 
 
 
 
 
 
 
 
 
 
118
 
119
+ # Track conversation turns
120
+ conversation_turns += 1
121
 
122
+ # Store patient data
123
+ patient_data.append(message)
 
124
 
125
+ # Phase 1-3: Information gathering
126
+ if conversation_turns < 4:
127
+ # Build consultation prompt
128
+ prompt = build_me_llama_prompt(CONSULTATION_PROMPT, history, message)
129
+
130
+ inputs = me_llama_tokenizer(prompt, return_tensors="pt")
131
+
132
+ # Move inputs to the same device as the model
133
+ if torch.cuda.is_available():
134
+ inputs = {k: v.to(me_llama_model.device) for k, v in inputs.items()}
135
+
136
+ # Generate consultation response
137
+ with torch.no_grad():
138
+ outputs = me_llama_model.generate(
139
+ inputs["input_ids"],
140
+ attention_mask=inputs["attention_mask"],
141
+ max_new_tokens=400,
142
+ temperature=0.7,
143
+ top_p=0.9,
144
+ do_sample=True,
145
+ pad_token_id=me_llama_tokenizer.eos_token_id
146
+ )
147
+
148
+ # Decode response
149
+ full_response = me_llama_tokenizer.decode(outputs[0], skip_special_tokens=False)
150
+ response = full_response.split('[/INST]')[-1].split('</s>')[0].strip()
151
+
152
+ return response
153
 
154
+ # Phase 4+: Summary and medicine suggestions
155
+ else:
156
+ # First, get summary from consultation
157
+ summary_prompt = build_me_llama_prompt(
158
+ CONSULTATION_PROMPT + "\n\nNow summarize what you've learned and suggest when professional care may be needed.",
159
+ history,
160
+ message
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
  )
162
+
163
+ inputs = me_llama_tokenizer(summary_prompt, return_tensors="pt")
164
+
165
+ if torch.cuda.is_available():
166
+ inputs = {k: v.to(me_llama_model.device) for k, v in inputs.items()}
167
+
168
+ # Generate summary
169
+ with torch.no_grad():
170
+ outputs = me_llama_model.generate(
171
+ inputs["input_ids"],
172
+ attention_mask=inputs["attention_mask"],
173
+ max_new_tokens=400,
174
+ temperature=0.7,
175
+ top_p=0.9,
176
+ do_sample=True,
177
+ pad_token_id=me_llama_tokenizer.eos_token_id
178
+ )
179
+
180
+ summary_response = me_llama_tokenizer.decode(outputs[0], skip_special_tokens=False)
181
+ summary = summary_response.split('[/INST]')[-1].split('</s>')[0].strip()
182
+
183
+ # Then get medicine suggestions using the same model
184
+ full_patient_info = "\n".join(patient_data) + f"\n\nMedical Summary: {summary}"
185
+ medicine_suggestions = generate_medicine_suggestions(full_patient_info)
186
+
187
+ # Combine both responses
188
+ final_response = (
189
+ f"**MEDICAL SUMMARY:**\n{summary}\n\n"
190
+ f"**MEDICATION AND HOME CARE SUGGESTIONS:**\n{medicine_suggestions}\n\n"
191
+ f"**DISCLAIMER:** This is AI-generated advice for informational purposes only. Please consult a licensed healthcare provider for proper medical diagnosis and treatment."
192
+ )
193
+
194
+ return final_response
195
+
196
+ except Exception as e:
197
+ return f"I apologize, but I'm experiencing technical difficulties. Please try again. Error: {str(e)}"
198
 
199
  # Create the Gradio interface
200
  demo = gr.ChatInterface(
201
  fn=generate_response,
202
  title="🏥 Complete Medical Assistant - Me-LLaMA 13B",
203
+ description="Comprehensive medical consultation powered by Me-LLaMA 13B. One model handles both consultation and medicine suggestions. Tell me about your symptoms!",
204
  examples=[
205
  "I have a persistent cough and sore throat for 3 days",
206
  "I've been having severe headaches and feel dizzy",