Spaces:
Runtime error
Runtime error
Vaishak G Kumar
commited on
Commit
·
e4c43f7
1
Parent(s):
5c24ca1
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,38 +14,6 @@ from huggingface_hub import login
|
|
| 14 |
hf_token = os.environ.get('HUGGINGFACE_TOKEN')
|
| 15 |
|
| 16 |
login(hf_token)
|
| 17 |
-
# Functions to Wrap the Prompt Correctly
|
| 18 |
-
def wrap_text(text, width=90):
|
| 19 |
-
lines = text.split('\n')
|
| 20 |
-
wrapped_lines = [textwrap.fill(line, width=width) for line in lines]
|
| 21 |
-
wrapped_text = '\n'.join(wrapped_lines)
|
| 22 |
-
return wrapped_text
|
| 23 |
-
def multimodal_prompt(user_input, system_prompt="You are an expert medical analyst:"):
|
| 24 |
-
|
| 25 |
-
# Combine user input and system prompt
|
| 26 |
-
formatted_input = f"{user_input}{system_prompt}"
|
| 27 |
-
|
| 28 |
-
# Encode the input text
|
| 29 |
-
encodeds = tokenizer(formatted_input, return_tensors="pt", add_special_tokens=False)
|
| 30 |
-
model_inputs = encodeds.to(device)
|
| 31 |
-
|
| 32 |
-
# Generate a response using the model
|
| 33 |
-
output = model.generate(
|
| 34 |
-
**model_inputs,
|
| 35 |
-
max_length=max_length,
|
| 36 |
-
use_cache=True,
|
| 37 |
-
early_stopping=True,
|
| 38 |
-
bos_token_id=model.config.bos_token_id,
|
| 39 |
-
eos_token_id=model.config.eos_token_id,
|
| 40 |
-
pad_token_id=model.config.eos_token_id,
|
| 41 |
-
temperature=0.1,
|
| 42 |
-
do_sample=True
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
# Decode the response
|
| 46 |
-
response_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 47 |
-
|
| 48 |
-
return response_text
|
| 49 |
|
| 50 |
# Define the device
|
| 51 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
@@ -67,35 +35,31 @@ peft_model = PeftModel.from_pretrained(peft_model, "vaishakgkumar/stablemedv1",
|
|
| 67 |
class ChatBot:
|
| 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=250,
|
| 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 |
-
|
|
|
|
|
|
|
| 95 |
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
|
| 101 |
title = "StableDoc Chat"
|
|
|
|
| 14 |
hf_token = os.environ.get('HUGGINGFACE_TOKEN')
|
| 15 |
|
| 16 |
login(hf_token)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Define the device
|
| 19 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 35 |
class ChatBot:
|
| 36 |
def __init__(self):
|
| 37 |
self.history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
def predict(self, user_input, system_prompt="You are an expert medical analyst:"):
|
| 40 |
+
# Combine user input and system prompt
|
| 41 |
+
formatted_input = f"{system_prompt}{user_input}"
|
| 42 |
|
| 43 |
+
# Encode user input
|
| 44 |
+
user_input_ids = tokenizer.encode(formatted_input, return_tensors="pt")
|
| 45 |
|
| 46 |
+
# Concatenate the user input with chat history
|
| 47 |
+
if len(self.history) > 0:
|
| 48 |
+
chat_history_ids = torch.cat([self.history, user_input_ids], dim=-1)
|
| 49 |
+
else:
|
| 50 |
+
chat_history_ids = user_input_ids
|
| 51 |
+
|
| 52 |
+
# Generate a response using the PEFT model
|
| 53 |
+
response = peft_model.generate(input_ids=chat_history_ids, max_length=1200, pad_token_id=tokenizer.eos_token_id)
|
| 54 |
+
|
| 55 |
+
# Update chat history
|
| 56 |
+
self.history = chat_history_ids
|
| 57 |
|
| 58 |
+
# Decode and return the response
|
| 59 |
+
response_text = tokenizer.decode(response[0], skip_special_tokens=True)
|
| 60 |
+
return response_text
|
| 61 |
+
|
| 62 |
+
bot = ChatBot()
|
| 63 |
|
| 64 |
|
| 65 |
title = "StableDoc Chat"
|