Spaces:
Sleeping
Sleeping
File size: 1,188 Bytes
03fde68 |
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 |
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
import gradio as gr
model_name = "mistralai/Mistral-7B-Instruct-v0.1"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
chat_history = [{"role": "system", "content": "You are a helpful assistant."}]
def chat(user_input):
# μ¬μ©μ μ
λ ₯μ μ±ν
κΈ°λ‘μ μΆκ°
chat_history.append({"role": "user", "content": user_input})
# λͺ¨λΈμ μ±ν
κΈ°λ‘ μ λ¬
inputs = tokenizer([message['content'] for message in chat_history], return_tensors="pt", padding=True).to("cuda" if torch.cuda.is_available() else "cpu")
# λͺ¨λΈλ‘ μλ΅ μμ±
outputs = model.generate(**inputs, max_length=200)
# μμ±λ μλ΅
bot_reply = tokenizer.decode(outputs[0], skip_special_tokens=True)
# λ΄ μλ΅μ μ±ν
κΈ°λ‘μ μΆκ°
chat_history.append({"role": "assistant", "content": bot_reply})
return bot_reply
demo = gr.ChatInterface(fn=chat, type='messages', title='μ΄μ°μ§μ μ±λ΄')
demo.launch(share=True) |