Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,15 @@ import gradio as gr
|
|
5 |
import random
|
6 |
from textwrap import wrap
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
def wrap_text(text, width=90):
|
10 |
lines = text.split('\n')
|
11 |
wrapped_lines = [textwrap.fill(line, width=width) for line in lines]
|
@@ -13,22 +21,12 @@ def wrap_text(text, width=90):
|
|
13 |
return wrapped_text
|
14 |
|
15 |
def multimodal_prompt(user_input, system_prompt="You are an expert medical analyst:"):
|
16 |
-
|
17 |
-
Generates text using a large language model, given a user input and a system prompt.
|
18 |
-
Args:
|
19 |
-
user_input: The user's input text to generate a response for.
|
20 |
-
system_prompt: Optional system prompt.
|
21 |
-
Returns:
|
22 |
-
A string containing the generated text.
|
23 |
-
"""
|
24 |
-
# Combine user input and system prompt
|
25 |
formatted_input = f"<s>[INST]{system_prompt} {user_input}[/INST]</s>"
|
26 |
|
27 |
-
# Encode the input text
|
28 |
encodeds = tokenizer(formatted_input, return_tensors="pt", add_special_tokens=False)
|
29 |
model_inputs = encodeds.to(device)
|
30 |
|
31 |
-
# Generate a response using the model
|
32 |
output = model.generate(
|
33 |
**model_inputs,
|
34 |
max_length=max_length,
|
@@ -46,26 +44,11 @@ def multimodal_prompt(user_input, system_prompt="You are an expert medical analy
|
|
46 |
|
47 |
return response_text
|
48 |
|
49 |
-
# Define the device
|
50 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
51 |
|
52 |
-
# Use the base model's ID
|
53 |
-
base_model_id = "mistralai/Mistral-7B-v0.1"
|
54 |
-
model_directory = "Tonic/mistralmed"
|
55 |
-
|
56 |
-
# Instantiate the Tokenizer
|
57 |
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1", trust_remote_code=True, padding_side="left")
|
58 |
-
# tokenizer = AutoTokenizer.from_pretrained("Tonic/mistralmed", trust_remote_code=True, padding_side="left")
|
59 |
tokenizer.pad_token = tokenizer.eos_token
|
60 |
tokenizer.padding_side = 'left'
|
61 |
|
62 |
-
# Specify the configuration class for the model
|
63 |
-
#model_config = AutoConfig.from_pretrained(base_model_id)
|
64 |
-
|
65 |
-
# Load the PEFT model with the specified configuration
|
66 |
-
#peft_model = AutoModelForCausalLM.from_pretrained(base_model_id, config=model_config)
|
67 |
-
|
68 |
-
# Load the PEFT model
|
69 |
peft_config = PeftConfig.from_pretrained("Tonic/mistralmed")
|
70 |
peft_model = MistralForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.1", trust_remote_code=True)
|
71 |
peft_model = PeftModel.from_pretrained(peft_model, "Tonic/mistralmed")
|
@@ -78,36 +61,25 @@ class ChatBot:
|
|
78 |
|
79 |
class ChatBot:
|
80 |
def __init__(self):
|
81 |
-
# Initialize the ChatBot class with an empty history
|
82 |
self.history = []
|
83 |
|
84 |
-
|
85 |
-
# Combine the user's input with the system prompt
|
86 |
formatted_input = f"<s>[INST]{system_prompt} {user_input}[/INST]"
|
87 |
-
|
88 |
-
# Encode the formatted input using the tokenizer
|
89 |
user_input_ids = tokenizer.encode(formatted_input, return_tensors="pt")
|
90 |
-
|
91 |
-
|
92 |
-
response = peft_model.generate(input_ids=user_input_ids, max_length=512, pad_token_id=tokenizer.eos_token_id)
|
93 |
-
|
94 |
-
# Decode the generated response to text
|
95 |
response_text = tokenizer.decode(response[0], skip_special_tokens=True)
|
96 |
|
97 |
-
return response_text
|
98 |
|
99 |
bot = ChatBot()
|
100 |
|
101 |
-
title = "๐๐ปํ ๋์ ๋ฏธ์คํธ๋๋ฉ๋ ์ฑํ
์ ์ค์ ๊ฒ์ ํ์ํฉ๋๋ค๐๐๐ปWelcome to Tonic's MistralMed Chat๐"
|
102 |
-
description = "์ด ๊ณต๊ฐ์ ์ฌ์ฉํ์ฌ ํ์ฌ ๋ชจ๋ธ์ ํ
์คํธํ ์ ์์ต๋๋ค. [(Tonic/MistralMed)](https://huggingface.co/Tonic/MistralMed) ๋๋ ์ด ๊ณต๊ฐ์ ๋ณต์ ํ๊ณ ๋ก์ปฌ ๋๋ ๐คHuggingFace์์ ์ฌ์ฉํ ์ ์์ต๋๋ค. [Discord์์ ํจ๊ป ๋ง๋ค๊ธฐ ์ํด Discord์ ๊ฐ์
ํ์ญ์์ค](https://discord.gg/VqTxc76K3u). You can use this Space to test out the current model [(Tonic/MistralMed)](https://huggingface.co/Tonic/MistralMed) or duplicate this Space and use it locally or on ๐คHuggingFace. [Join me on Discord to build together](https://discord.gg/VqTxc76K3u)."
|
103 |
-
examples = [["[Question:] What is the proper treatment for buccal herpes?", "You are a medicine and public health expert, you will receive a question, answer the question, and provide a complete answer"]]
|
104 |
-
|
105 |
iface = gr.Interface(
|
106 |
fn=bot.predict,
|
107 |
title=title,
|
108 |
description=description,
|
109 |
examples=examples,
|
110 |
-
inputs=["text", "text"],
|
111 |
outputs="text",
|
112 |
theme="ParityError/Anime"
|
113 |
)
|
|
|
5 |
import random
|
6 |
from textwrap import wrap
|
7 |
|
8 |
+
|
9 |
+
title = "๐๐ปํ ๋์ ๋ฏธ์คํธ๋๋ฉ๋ ์ฑํ
์ ์ค์ ๊ฒ์ ํ์ํฉ๋๋ค๐๐๐ปWelcome to Tonic's MistralMed Chat๐"
|
10 |
+
description = "์ด ๊ณต๊ฐ์ ์ฌ์ฉํ์ฌ ํ์ฌ ๋ชจ๋ธ์ ํ
์คํธํ ์ ์์ต๋๋ค. [(Tonic/MistralMed)](https://huggingface.co/Tonic/MistralMed) ๋๋ ์ด ๊ณต๊ฐ์ ๋ณต์ ํ๊ณ ๋ก์ปฌ ๋๋ ๐คHuggingFace์์ ์ฌ์ฉํ ์ ์์ต๋๋ค. [Discord์์ ํจ๊ป ๋ง๋ค๊ธฐ ์ํด Discord์ ๊ฐ์
ํ์ญ์์ค](https://discord.gg/VqTxc76K3u). You can use this Space to test out the current model [(Tonic/MistralMed)](https://huggingface.co/Tonic/MistralMed) or duplicate this Space and use it locally or on ๐คHuggingFace. [Join me on Discord to build together](https://discord.gg/VqTxc76K3u)."
|
11 |
+
examples = [["[Question:] What is the proper treatment for buccal herpes?", "You are a medicine and public health expert, you will receive a question, answer the question, and provide a complete answer"]]
|
12 |
+
|
13 |
+
base_model_id = "mistralai/Mistral-7B-v0.1"
|
14 |
+
model_directory = "Tonic/mistralmed"
|
15 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
16 |
+
|
17 |
def wrap_text(text, width=90):
|
18 |
lines = text.split('\n')
|
19 |
wrapped_lines = [textwrap.fill(line, width=width) for line in lines]
|
|
|
21 |
return wrapped_text
|
22 |
|
23 |
def multimodal_prompt(user_input, system_prompt="You are an expert medical analyst:"):
|
24 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
formatted_input = f"<s>[INST]{system_prompt} {user_input}[/INST]</s>"
|
26 |
|
|
|
27 |
encodeds = tokenizer(formatted_input, return_tensors="pt", add_special_tokens=False)
|
28 |
model_inputs = encodeds.to(device)
|
29 |
|
|
|
30 |
output = model.generate(
|
31 |
**model_inputs,
|
32 |
max_length=max_length,
|
|
|
44 |
|
45 |
return response_text
|
46 |
|
|
|
|
|
47 |
|
|
|
|
|
|
|
|
|
|
|
48 |
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1", trust_remote_code=True, padding_side="left")
|
|
|
49 |
tokenizer.pad_token = tokenizer.eos_token
|
50 |
tokenizer.padding_side = 'left'
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
peft_config = PeftConfig.from_pretrained("Tonic/mistralmed")
|
53 |
peft_model = MistralForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.1", trust_remote_code=True)
|
54 |
peft_model = PeftModel.from_pretrained(peft_model, "Tonic/mistralmed")
|
|
|
61 |
|
62 |
class ChatBot:
|
63 |
def __init__(self):
|
|
|
64 |
self.history = []
|
65 |
|
66 |
+
def predict(self, user_input, system_prompt="You are an expert medical analyst:"):
|
|
|
67 |
formatted_input = f"<s>[INST]{system_prompt} {user_input}[/INST]"
|
|
|
|
|
68 |
user_input_ids = tokenizer.encode(formatted_input, return_tensors="pt")
|
69 |
+
user_input_ids = user_input_ids.to(device)
|
70 |
+
response = peft_model.generate(input_ids=user_input_ids, max_length=256, pad_token_id=tokenizer.eos_token_id)
|
|
|
|
|
|
|
71 |
response_text = tokenizer.decode(response[0], skip_special_tokens=True)
|
72 |
|
73 |
+
return response_text
|
74 |
|
75 |
bot = ChatBot()
|
76 |
|
|
|
|
|
|
|
|
|
77 |
iface = gr.Interface(
|
78 |
fn=bot.predict,
|
79 |
title=title,
|
80 |
description=description,
|
81 |
examples=examples,
|
82 |
+
inputs=["text", "text"],
|
83 |
outputs="text",
|
84 |
theme="ParityError/Anime"
|
85 |
)
|