vaishakgkumar commited on
Commit
989f6d4
1 Parent(s): ed92110

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -20
app.py CHANGED
@@ -68,31 +68,35 @@ class ChatBot:
68
  def __init__(self):
69
  self.history = []
70
 
71
- def predict(self, user_input, system_prompt="You are an expert medical analyst trained on medical datatset:"):
72
- # Combine user input and system prompt
73
- formatted_input = f"{user_input}{system_prompt}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
- # Encode user input
76
- user_input_ids = tokenizer.encode(formatted_input, return_tensors="pt")
77
-
78
- # Concatenate the user input with chat history
79
- if len(self.history) > 0:
80
- chat_history_ids = torch.cat([self.history, user_input_ids], dim=-1)
81
- else:
82
- chat_history_ids = user_input_ids
83
-
84
- # Generate a response using the PEFT model
85
- response = peft_model.generate(input_ids=chat_history_ids, max_length=512, pad_token_id=tokenizer.eos_token_id)
86
-
87
- # Update chat history
88
- self.history = chat_history_ids
89
-
90
- # Decode and return the response
91
- response_text = tokenizer.decode(response[0], skip_special_tokens=True)
92
  return response_text
93
 
94
  bot = ChatBot()
95
 
 
 
96
  title = "StableDoc Chat"
97
  description = """
98
  You can use this Space to test out the current model vaishakgkumar/stablemedv3.
 
68
  def __init__(self):
69
  self.history = []
70
 
71
+ def predict(self, user_input, system_prompt="You are an expert analyst and provide assessment:"):
72
+ prompt = [{'role': 'user', 'content': user_input + "\n" + system_prompt + ":"}]
73
+ inputs = tokenizer.apply_chat_template(
74
+ prompt,
75
+ add_generation_prompt=True,
76
+ return_tensors='pt'
77
+ )
78
+
79
+ # Generate a response using the model
80
+ tokens = peft_model.generate(
81
+ inputs.to(model.device),
82
+ max_new_tokens=512,
83
+ temperature=0.8,
84
+ do_sample=False
85
+ )
86
+
87
+ # Decode the response
88
+ response_text = tokenizer.decode(tokens[0], skip_special_tokens=False)
89
+
90
+ # Free up memory
91
+ del tokens
92
+ torch.cuda.empty_cache()
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  return response_text
95
 
96
  bot = ChatBot()
97
 
98
+
99
+
100
  title = "StableDoc Chat"
101
  description = """
102
  You can use this Space to test out the current model vaishakgkumar/stablemedv3.