File size: 1,269 Bytes
8374739
 
 
 
75822ed
 
8374739
75822ed
b6f3967
8374739
75822ed
8374739
75822ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8374739
75822ed
 
 
 
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
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
import gradio as gr

try:
    model_name = "gpt2"

    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = AutoModelForCausalLM.from_pretrained(model_name)

    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)
except Exception as e:
    print(f"Error occurred: {e}")