Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| import torch | |
| tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large") | |
| model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large") | |
| def chatbot(input): | |
| #loop length = number of chats | |
| for step in range(50): | |
| # take user input | |
| #text = input(">> You: ") | |
| # encode the input and add end of string token | |
| input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors="pt") | |
| # concatenate new user input with chat history (if there is) | |
| bot_input_ids = torch.cat([chat_history_ids, input_ids], dim=-1) if step > 0 else input_ids | |
| # generate a bot response | |
| chat_history_ids = model.generate( | |
| bot_input_ids, | |
| max_length=1000, | |
| do_sample=True, | |
| top_p=0.95, | |
| top_k=0, | |
| temperature=0.75, | |
| pad_token_id=tokenizer.eos_token_id | |
| ) | |
| #print the output | |
| output = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True) | |
| return output | |
| inputs = gr.inputs.Textbox(lines=7, label="Chat with AI") | |
| outputs = gr.outputs.Textbox(label="Reply") | |
| gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="Self_Trained_V1", | |
| description="Ask anything you want", | |
| ).launch() |