Update app.py
Browse files
app.py
CHANGED
@@ -1,84 +1,113 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import transformers
|
4 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
import torch
|
6 |
import gradio as gr
|
7 |
-
import
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
|
|
52 |
|
53 |
-
|
54 |
-
response = self.tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
return response
|
60 |
-
|
61 |
-
Orca_bot = OrcaChatBot(model, tokenizer)
|
62 |
-
|
63 |
-
def gradio_predict(user_message, system_message, max_new_tokens, temperature, top_p, repetition_penalty):
|
64 |
-
full_message = f"{system_message}\n{user_message}" if system_message else user_message
|
65 |
-
return Orca_bot.predict(full_message, temperature, max_new_tokens, top_p, repetition_penalty)
|
66 |
|
67 |
iface = gr.Interface(
|
68 |
-
fn=
|
69 |
title=title,
|
70 |
description=description,
|
71 |
-
|
72 |
-
|
73 |
-
gr.Textbox(label="Introduce a Character Here or Set a Scene (system prompt)", type="text", lines=2),
|
74 |
-
gr.Slider(label="Max new tokens", value=1200, minimum=25, maximum=4096, step=1),
|
75 |
-
gr.Slider(label="Temperature", value=0.7, minimum=0.05, maximum=1.0, step=0.05),
|
76 |
-
gr.Slider(label="Top-p (nucleus sampling)", value=0.90, minimum=0.01, maximum=0.99, step=0.05),
|
77 |
-
gr.Slider(label="Repetition penalty", value=1.9, minimum=1.0, maximum=2.0, step=0.05)
|
78 |
-
],
|
79 |
outputs="text",
|
80 |
theme="ParityError/Anime"
|
81 |
)
|
82 |
|
83 |
-
# Launch the Gradio interface
|
84 |
iface.launch()
|
|
|
1 |
+
from transformers import AutoConfig, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM, MistralForCausalLM
|
2 |
+
from peft import PeftModel, PeftConfig
|
|
|
|
|
3 |
import torch
|
4 |
import gradio as gr
|
5 |
+
import random
|
6 |
+
from textwrap import wrap
|
7 |
+
|
8 |
+
# Functions to Wrap the Prompt Correctly
|
9 |
+
def wrap_text(text, width=90):
|
10 |
+
lines = text.split('\n')
|
11 |
+
wrapped_lines = [textwrap.fill(line, width=width) for line in lines]
|
12 |
+
wrapped_text = '\n'.join(wrapped_lines)
|
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]"
|
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,
|
35 |
+
use_cache=True,
|
36 |
+
early_stopping=True,
|
37 |
+
bos_token_id=model.config.bos_token_id,
|
38 |
+
eos_token_id=model.config.eos_token_id,
|
39 |
+
pad_token_id=model.config.eos_token_id,
|
40 |
+
temperature=0.1,
|
41 |
+
do_sample=True
|
42 |
+
)
|
43 |
+
|
44 |
+
# Decode the response
|
45 |
+
response_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
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")
|
72 |
+
|
73 |
+
class ChatBot:
|
74 |
+
def __init__(self):
|
75 |
+
self.history = []
|
76 |
+
|
77 |
+
class ChatBot:
|
78 |
+
def __init__(self):
|
79 |
+
# Initialize the ChatBot class with an empty history
|
80 |
+
self.history = []
|
81 |
+
|
82 |
+
def predict(self, user_input, system_prompt="You are an expert medical analyst:"):
|
83 |
+
# Combine the user's input with the system prompt
|
84 |
+
formatted_input = f"<s>[INST]{system_prompt} {user_input}[/INST]"
|
85 |
+
|
86 |
+
# Encode the formatted input using the tokenizer
|
87 |
+
user_input_ids = tokenizer.encode(formatted_input, return_tensors="pt")
|
88 |
+
|
89 |
+
# Generate a response using the PEFT model
|
90 |
+
response = peft_model.generate(input_ids=user_input_ids, max_length=512, pad_token_id=tokenizer.eos_token_id)
|
91 |
+
|
92 |
+
# Decode the generated response to text
|
93 |
+
response_text = tokenizer.decode(response[0], skip_special_tokens=True)
|
94 |
|
95 |
+
return response_text # Return the generated response
|
96 |
|
97 |
+
bot = ChatBot()
|
|
|
98 |
|
99 |
+
title = "๐๐ปํ ๋์ ๋ฏธ์คํธ๋๋ฉ๋ ์ฑํ
์ ์ค์ ๊ฒ์ ํ์ํฉ๋๋ค๐๐๐ปWelcome to Tonic's MistralMed Chat๐"
|
100 |
+
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)."
|
101 |
+
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"]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
iface = gr.Interface(
|
104 |
+
fn=bot.predict,
|
105 |
title=title,
|
106 |
description=description,
|
107 |
+
examples=examples,
|
108 |
+
inputs=["text", "text"], # Take user input and system prompt separately
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
outputs="text",
|
110 |
theme="ParityError/Anime"
|
111 |
)
|
112 |
|
|
|
113 |
iface.launch()
|