File size: 1,676 Bytes
ec2ed92 d8f7120 60cffd8 ec2ed92 60cffd8 d8f7120 60cffd8 bea9bd0 60cffd8 bea9bd0 60cffd8 bea9bd0 60cffd8 bea9bd0 60cffd8 bea9bd0 60cffd8 bea9bd0 60cffd8 bea9bd0 60cffd8 bea9bd0 60cffd8 bea9bd0 60cffd8 f96b59d bea9bd0 60cffd8 bea9bd0 60cffd8 64b74f4 60cffd8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
# 1οΈβ£ Load the model
MODEL_REPO = "DSDUDEd/firebase" # your HF model repo
tokenizer = AutoTokenizer.from_pretrained(MODEL_REPO)
model = AutoModelForCausalLM.from_pretrained(MODEL_REPO)
# Set device (CPU or GPU if available)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
# 2οΈβ£ Chat history
chat_history = []
# 3οΈβ£ Function to generate AI response
def chat_with_ai(user_input):
global chat_history
chat_history.append(f"You: {user_input}")
# Prepare input for the model
input_text = "\n".join(chat_history) + "\nAI:"
inputs = tokenizer(input_text, return_tensors="pt").to(device)
# Generate output
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)
# Extract only the AI's last response
ai_response = response.split("AI:")[-1].strip()
chat_history.append(f"AI: {ai_response}")
# Display the chat nicely
return "\n".join(chat_history)
# 4οΈβ£ Gradio interface
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)
# 5οΈβ£ Launch the Space
demo.launch()
|