| import gradio as gr |
| from transformers import AutoTokenizer, AutoModelForCausalLM |
| import torch |
|
|
| |
| MODEL_REPO = "DSDUDEd/firebase" |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_REPO) |
| model = AutoModelForCausalLM.from_pretrained(MODEL_REPO) |
|
|
| |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
| model.to(device) |
|
|
| |
| chat_history = [] |
|
|
| |
| def chat_with_ai(user_input): |
| global chat_history |
| chat_history.append(f"You: {user_input}") |
| |
| |
| input_text = "\n".join(chat_history) + "\nAI:" |
| inputs = tokenizer(input_text, return_tensors="pt").to(device) |
| |
| |
| outputs = model.generate( |
| **inputs, |
| max_new_tokens=150, |
| temperature=0.7, |
| top_p=0.9, |
| do_sample=True, |
| pad_token_id=tokenizer.eos_token_id |
| ) |
| |
| response = tokenizer.decode(outputs[0], skip_special_tokens=True) |
| |
| |
| ai_response = response.split("AI:")[-1].strip() |
| chat_history.append(f"AI: {ai_response}") |
| |
| |
| return "\n".join(chat_history) |
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("## π€ Custom GPT-2 AI Chat") |
| chatbot = gr.Textbox(label="Your Message", placeholder="Type here...", lines=2) |
| output = gr.Textbox(label="Chat Output", interactive=False, lines=15) |
| send_button = gr.Button("Send") |
|
|
| send_button.click(fn=chat_with_ai, inputs=chatbot, outputs=output) |
|
|
| |
| demo.launch() |
|
|