betajuned's picture
Update app.py
e5f4930 verified
raw
history blame
1.39 kB
from transformers import AutoModelForCausalLM, AutoTokenizer
import gradio as gr
import torch
title = "GPT-2 JTE Chatbot"
description = "GPT-2 Menjadi Chatbot"
examples = [["Bagaimana cara mengisi KRS?"]]
tokenizer = AutoTokenizer.from_pretrained("betajuned/GPT-2_Kombinasi4")
model = AutoModelForCausalLM.from_pretrained("betajuned/GPT-2_Kombinasi4")
def predict(input, history=[]):
# Tokenize the new input sentence
new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors="pt")
# Append the new user input tokens to the chat history
bot_input_ids = torch.cat([torch.tensor(history), new_user_input_ids], dim=-1) if history else new_user_input_ids
# Generate a response
chat_history_ids = model.generate(bot_input_ids, max_length=200, pad_token_id=tokenizer.eos_token_id)
# Convert the tokens to text
response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
# Update the history with the new tokens
new_history = chat_history_ids.tolist()
return response, new_history
gr.Interface(
fn=predict,
title=title,
description=description,
examples=examples,
inputs=[gr.components.Textbox(lines=2, placeholder="Enter your message here..."), "state"],
outputs=[gr.components.Textbox(), "state"],
theme="finlaymacklon/boxy_violet",
).launch()