47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
import torch
|
|
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
from transformers.generation.utils import GenerationConfig
|
|
import gradio as gr
|
|
from gradio.themes.utils import sizes
|
|
|
|
|
|
theme = gr.themes.Default(radius_size=sizes.radius_none).set(
|
|
block_label_text_color = '#4D63FF',
|
|
block_title_text_color = '#4D63FF',
|
|
button_primary_text_color = '#4D63FF',
|
|
button_primary_background_fill='#FFFFFF',
|
|
button_primary_border_color='#4D63FF',
|
|
button_primary_background_fill_hover='#EDEFFF',
|
|
)
|
|
|
|
css = "footer {visibility: hidden}"
|
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained("baichuan-inc/Baichuan-13B-Chat", use_fast=False, trust_remote_code=True)
|
|
model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan-13B-Chat", device_map="auto", torch_dtype=torch.float16, trust_remote_code=True)
|
|
model.generation_config = GenerationConfig.from_pretrained("baichuan-inc/Baichuan-13B-Chat")
|
|
|
|
with gr.Blocks(theme=theme, css=css) as demo:
|
|
gr.Markdown("""
|
|
<div align='center' ><font size='60'>Baichuan-13B</font></div>
|
|
""")
|
|
chatbot = gr.Chatbot()
|
|
msg = gr.Textbox()
|
|
clear = gr.ClearButton([msg, chatbot])
|
|
|
|
def respond(message, chat_history):
|
|
messages = []
|
|
messages.append({"role": "user", "content": message})
|
|
|
|
bot_message = model.chat(tokenizer, messages)
|
|
|
|
chat_history.append((message, bot_message))
|
|
|
|
return "", chat_history
|
|
|
|
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
|
|
|
if __name__ == "__main__":
|
|
demo.launch(server_name="0.0.0.0")
|
|
|