study11 / app.py
gampala1234's picture
Update app.py
0bf7af1 verified
Raw History Blame Contribute Delete
1.3 kB
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
# Load model
model_name = "microsoft/DialoGPT-small"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# Chat function
def chat_with_bot(message, history):
global chat_history_ids
if not history:
chat_history_ids = None
# Encode input
new_input_ids = tokenizer.encode(message + tokenizer.eos_token, return_tensors='pt')
# Append to history
bot_input_ids = torch.cat([chat_history_ids, new_input_ids], dim=-1) if chat_history_ids is not None else new_input_ids
# Generate response
chat_history_ids = model.generate(
bot_input_ids,
max_length=1000,
pad_token_id=tokenizer.eos_token_id,
no_repeat_ngram_size=3,
do_sample=True,
top_k=50,
top_p=0.9,
temperature=0.7
)
# Decode response
response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
return response
# Gradio interface
iface = gr.ChatInterface(
fn=chat_with_bot,
title="Rey Mahesh Bau Bot 😎",
description="Your chill AI bro for memes, roasts, and Twitter-style banter!",
theme="soft"
)
iface.launch()