Spaces:
Sleeping
Sleeping
File size: 5,943 Bytes
d5d675e a43ee22 d5d675e a43ee22 d5d675e a43ee22 d5d675e a43ee22 d5d675e 5faeb60 a43ee22 d5d675e a43ee22 d5d675e 5faeb60 d5d675e a43ee22 d5d675e a43ee22 d5d675e 5faeb60 d5d675e 5faeb60 d5d675e cdb6de6 d5d675e cdb6de6 d5d675e cdb6de6 d5d675e cdb6de6 d5d675e cdb6de6 d5d675e cdb6de6 d5d675e cdb6de6 d5d675e 5faeb60 d5d675e a43ee22 d5d675e |
1 2 3 4 5 6 7 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
import gc
import os
import gradio as gr
from llama_cpp import Llama
ALPACA_SYSTEM_PROMPT = 'Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request'
ALPACA_SYSTEM_PROMPT_NO_INPUT = 'Below is an instruction that describes a task. Write a response that appropriately completes the request.'
DEFAULT_MODEL = 'Med-Alpaca-2-7b-chat.Q4_K_M'
model_paths = {
'Med-Alpaca-2-7b-chat.Q2_K': {
'repo_id': 'minhnguyent546/Med-Alpaca-2-7b-chat-GGUF',
'filename': 'Med-Alpaca-2-7B-chat.Q2_K.gguf',
},
'Med-Alpaca-2-7b-chat.Q4_K_M': {
'repo_id': 'minhnguyent546/Med-Alpaca-2-7b-chat-GGUF',
'filename': 'Med-Alpaca-2-7B-chat.Q4_K_M.gguf',
},
'Med-Alpaca-2-7b-chat.Q6_K': {
'repo_id': 'minhnguyent546/Med-Alpaca-2-7b-chat-GGUF',
'filename': 'Med-Alpaca-2-7B-chat.Q6_K.gguf',
},
'Med-Alpaca-2-7b-chat.Q8_0': {
'repo_id': 'minhnguyent546/Med-Alpaca-2-7b-chat-GGUF',
'filename': 'Med-Alpaca-2-7B-chat.Q8_0.gguf',
},
'Med-Alpaca-2-7b-chat.F16': {
'repo_id': 'minhnguyent546/Med-Alpaca-2-7b-chat-GGUF',
'filename': 'Med-Alpaca-2-7B-chat.F16.gguf',
},
}
model = None
def generate_alpaca_prompt(
instruction: str,
input: str | None = None,
response: str = '',
) -> str:
prompt = ''
if input is not None and input and input.strip() != '<noinput>':
prompt = (
f'{ALPACA_SYSTEM_PROMPT}\n\n'
f'### Instruction:\n'
f'{instruction}\n\n'
f'### Input:\n'
f'{input}\n\n'
f'### Response: '
f'{response}'
)
else:
prompt = (
f'{ALPACA_SYSTEM_PROMPT_NO_INPUT}\n\n'
f'### Instruction:\n'
f'{instruction}\n\n'
f'### Response: '
f'{response}'
)
return prompt.strip()
def chat_completion(
message,
history,
seed: int,
max_new_tokens: int,
temperature: float,
repeatition_penalty: float,
top_k: int,
top_p: float,
):
if model is None:
reload_model(DEFAULT_MODEL)
prompt = generate_alpaca_prompt(instruction=message)
response_iterator = model(
prompt,
stream=True,
seed=seed,
max_tokens=max_new_tokens,
temperature=temperature,
top_p=top_p,
top_k=top_k,
repeat_penalty=repeatition_penalty,
)
partial_response = ''
for token in response_iterator:
partial_response += token['choices'][0]['text']
yield partial_response
def reload_model(model_name: str):
global model
if 'model' in globals():
del model
gc.collect()
model = Llama.from_pretrained(
**model_paths[model_name],
n_ctx=4096,
n_threads=4,
cache_dir='./.hf_cache'
)
app_title_mark = gr.Markdown(f"""<center><font size=18>{model_name}</center>""")
chatbot = gr.Chatbot(
type='messages',
height=500,
placeholder='<strong>Hi doctor, I have a headache, what should I do?</strong>',
label=model_name,
avatar_images=[None, 'https://raw.githubusercontent.com/minhnguyent546/medical-llama2/demo/assets/med_alpaca.png'], # pyright: ignore[reportArgumentType]
)
return app_title_mark, chatbot
def main() -> None:
with gr.Blocks(theme=gr.themes.Ocean()) as demo:
app_title_mark = gr.Markdown(f"""<center><font size=18>{DEFAULT_MODEL}</center>""")
model_options = list(model_paths.keys())
with gr.Row():
with gr.Column(scale=2):
with gr.Row():
model_radio = gr.Radio(choices=model_options, label='Model', value=DEFAULT_MODEL)
with gr.Row():
seed = gr.Number(value=998244353, label='Seed')
max_new_tokens = gr.Number(value=512, minimum=64, maximum=2048, label='Max new tokens')
with gr.Row():
temperature = gr.Slider(0, 2, step=0.01, label='Temperature', value=0.6)
repeatition_penalty = gr.Slider(0.01, 5, step=0.05, label='Repetition penalty', value=1.1)
with gr.Row():
top_k = gr.Slider(1, 100, step=1, label='Top k', value=40)
top_p = gr.Slider(0, 1, step=0.01, label='Top p', value=0.9)
with gr.Column(scale=5):
chatbot = gr.Chatbot(
type='messages',
height=500,
placeholder='<strong>Hi doctor, I have a headache, what should I do?</strong>',
label=DEFAULT_MODEL,
avatar_images=[None, 'https://raw.githubusercontent.com/minhnguyent546/medical-llama2/demo/assets/med_alpaca.png'], # pyright: ignore[reportArgumentType]
)
textbox = gr.Textbox(
placeholder='Hi doctor, I have a headache, what should I do?',
container=False,
submit_btn=True,
stop_btn=True,
)
chat_interface = gr.ChatInterface(
chat_completion,
type='messages',
chatbot=chatbot,
textbox=textbox,
additional_inputs=[
seed,
max_new_tokens,
temperature,
repeatition_penalty,
top_k,
top_p,
],
)
model_radio.change(reload_model, inputs=[model_radio], outputs=[app_title_mark, chatbot])
demo.queue(api_open=False, default_concurrency_limit=20)
demo.launch(max_threads=5, share=os.environ.get('GRADIO_SHARE', False))
if __name__ == '__main__':
main()
|