vangru/Dialogue_System
0
1import gradio as gr2from transformers import AutoModelForCausalLM, AutoTokenizer3import torch4 5# Load model and tokenizer6model_name = "microsoft/DialoGPT-medium"7 8tokenizer = AutoTokenizer.from_pretrained(model_name)9model = AutoModelForCausalLM.from_pretrained(model_name)10 11# Store conversation history12chat_history_ids = None13 14def respond(message, history):15 global chat_history_ids16 17 # Encode user input18 new_input_ids = tokenizer.encode(message + tokenizer.eos_token, return_tensors='pt')19 20 # Append to chat history21 if chat_history_ids is not None:22 bot_input_ids = torch.cat([chat_history_ids, new_input_ids], dim=-1)23 else:24 bot_input_ids = new_input_ids25 26 # Generate response27 chat_history_ids = model.generate(28 bot_input_ids,29 max_length=1000,30 pad_token_id=tokenizer.eos_token_id,31 do_sample=True,32 top_k=100,33 top_p=0.7,34 temperature=0.835 )36 37 # Decode response38 response = tokenizer.decode(39 chat_history_ids[:, bot_input_ids.shape[-1]:][0],40 skip_special_tokens=True41 )42 43 return response44 45# Create Gradio interface46demo = gr.ChatInterface(47 fn=respond,48 title="Dialogue System using DialoGPT",49 description="A simple conversational AI built with HuggingFace Transformers and Gradio."50)51 52if __name__ == "__main__":53 demo.launch()