Spaces:
Sleeping
Sleeping
| 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}") |