Update README.md
Browse files
README.md
CHANGED
|
@@ -67,19 +67,68 @@ Use the code below to get started with the model.
|
|
| 67 |
[Tonic/MistralMED_Chat](https://huggingface.co/Tonic/MistralMED_Chat)
|
| 68 |
|
| 69 |
```python
|
| 70 |
-
from transformers import
|
| 71 |
-
from peft import PeftModel, PeftConfig
|
| 72 |
import torch
|
| 73 |
import gradio as gr
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
# Use the base model's ID
|
| 77 |
base_model_id = "mistralai/Mistral-7B-v0.1"
|
| 78 |
model_directory = "Tonic/mistralmed"
|
| 79 |
|
| 80 |
-
# Instantiate the
|
| 81 |
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1", trust_remote_code=True, padding_side="left")
|
| 82 |
-
#tokenizer
|
|
|
|
| 83 |
tokenizer.padding_side = 'left'
|
| 84 |
|
| 85 |
|
|
@@ -98,9 +147,9 @@ class ChatBot:
|
|
| 98 |
def __init__(self):
|
| 99 |
self.history = []
|
| 100 |
|
| 101 |
-
def predict(self,
|
| 102 |
# Encode user input
|
| 103 |
-
user_input_ids = tokenizer.encode(
|
| 104 |
|
| 105 |
# Concatenate the user input with chat history
|
| 106 |
if len(self.history) > 0:
|
|
@@ -122,7 +171,7 @@ bot = ChatBot()
|
|
| 122 |
|
| 123 |
title = "👋🏻Welcome to Tonic's MistralMed Chat🚀"
|
| 124 |
description = "You can use this Space to test out the current model (MistralMed) or duplicate this Space and use it for any other model on 🤗HuggingFace. Join me on Discord to build together."
|
| 125 |
-
examples = [["What is the boiling point of nitrogen"]]
|
| 126 |
|
| 127 |
iface = gr.Interface(
|
| 128 |
fn=bot.predict,
|
|
|
|
| 67 |
[Tonic/MistralMED_Chat](https://huggingface.co/Tonic/MistralMED_Chat)
|
| 68 |
|
| 69 |
```python
|
| 70 |
+
from transformers import AutoTokenizer, MistralForCausalLM
|
|
|
|
| 71 |
import torch
|
| 72 |
import gradio as gr
|
| 73 |
+
import random
|
| 74 |
+
from textwrap import wrap
|
| 75 |
+
import random
|
| 76 |
+
|
| 77 |
+
# Functions to Wrap the Prompt Correctly
|
| 78 |
+
|
| 79 |
+
def wrap_text(text, width=90):
|
| 80 |
+
lines = text.split('\n')
|
| 81 |
+
wrapped_lines = [textwrap.fill(line, width=width) for line in lines]
|
| 82 |
+
wrapped_text = '\n'.join(wrapped_lines)
|
| 83 |
+
return wrapped_text
|
| 84 |
+
|
| 85 |
+
def multimodal_prompt(input_text, system_prompt="", max_length=512):
|
| 86 |
+
"""
|
| 87 |
+
Generates text using a large language model, given a prompt and a device.
|
| 88 |
+
Args:
|
| 89 |
+
input_text: The input text to generate a response for.
|
| 90 |
+
system_prompt: Optional system prompt.
|
| 91 |
+
max_length: Maximum length of the generated text.
|
| 92 |
+
Returns:
|
| 93 |
+
A string containing the generated text.
|
| 94 |
+
"""
|
| 95 |
+
# Modify the input text to include the desired format
|
| 96 |
+
formatted_input = f"""<s>[INST]{input_text}[/INST]"""
|
| 97 |
+
|
| 98 |
+
# Encode the input text
|
| 99 |
+
encodeds = tokenizer(formatted_input, return_tensors="pt", add_special_tokens=False)
|
| 100 |
+
model_inputs = encodeds.to(device)
|
| 101 |
+
|
| 102 |
+
# Generate a response using the model
|
| 103 |
+
output = model.generate(
|
| 104 |
+
**model_inputs,
|
| 105 |
+
max_length=max_length,
|
| 106 |
+
use_cache=True,
|
| 107 |
+
early_stopping=True,
|
| 108 |
+
bos_token_id=model.config.bos_token_id,
|
| 109 |
+
eos_token_id=model.config.eos_token_id,
|
| 110 |
+
pad_token_id=model.config.eos_token_id,
|
| 111 |
+
temperature=0.1,
|
| 112 |
+
do_sample=True
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
# Decode the response
|
| 116 |
+
response_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 117 |
+
|
| 118 |
+
return response_text
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
# Define the device
|
| 122 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 123 |
+
|
| 124 |
# Use the base model's ID
|
| 125 |
base_model_id = "mistralai/Mistral-7B-v0.1"
|
| 126 |
model_directory = "Tonic/mistralmed"
|
| 127 |
|
| 128 |
+
# Instantiate the Tokenizer
|
| 129 |
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1", trust_remote_code=True, padding_side="left")
|
| 130 |
+
# tokenizer = AutoTokenizer.from_pretrained("Tonic/mistralmed", trust_remote_code=True, padding_side="left")
|
| 131 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 132 |
tokenizer.padding_side = 'left'
|
| 133 |
|
| 134 |
|
|
|
|
| 147 |
def __init__(self):
|
| 148 |
self.history = []
|
| 149 |
|
| 150 |
+
def predict(self, input_text):
|
| 151 |
# Encode user input
|
| 152 |
+
user_input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
| 153 |
|
| 154 |
# Concatenate the user input with chat history
|
| 155 |
if len(self.history) > 0:
|
|
|
|
| 171 |
|
| 172 |
title = "👋🏻Welcome to Tonic's MistralMed Chat🚀"
|
| 173 |
description = "You can use this Space to test out the current model (MistralMed) or duplicate this Space and use it for any other model on 🤗HuggingFace. Join me on Discord to build together."
|
| 174 |
+
examples = [["What is the boiling point of nitrogen?"]]
|
| 175 |
|
| 176 |
iface = gr.Interface(
|
| 177 |
fn=bot.predict,
|