Spaces:
Sleeping
Sleeping
File size: 1,929 Bytes
850464f 8753d35 850464f 5d3bd30 25a231c 899b7f5 25a231c 850464f 5d3bd30 8753d35 5d3bd30 899b7f5 5d3bd30 8753d35 0014e4f 899b7f5 0014e4f 5d3bd30 8753d35 899b7f5 8753d35 899b7f5 8753d35 5d3bd30 850464f 8753d35 | 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import re
# Load GPT-Neo 1.3B
tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neo-1.3B")
model = AutoModelForCausalLM.from_pretrained("EleutherAI/gpt-neo-1.3B")
# Chat history as list of tuples (user, ai)
chat_history = []
def chat(message, history):
# Append user message
history.append((message, "")) # placeholder for AI response
# Take last 3 user messages for context
recent_user_msgs = [m[0] for m in history[-3:]]
context = "\n".join(recent_user_msgs) + "\nAI: "
inputs = tokenizer(context, return_tensors="pt")
outputs = model.generate(
**inputs,
max_new_tokens=50,
do_sample=True,
top_p=0.9,
temperature=0.8
)
# Decode and join sentences to avoid one-word-per-line
raw_response = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
sentences = re.split(r'(?<=[.!?]) +', raw_response)
response = ' '.join(sentences)
# Update last tuple with AI response
history[-1] = (message, response)
# Return updated chat history
return history
# Gradio interface with spooky theme
with gr.Blocks(css="""
body {
background-color: #0a1f44; /* Deep blue background */
}
h1 {
color: red;
font-family: 'Creepster', cursive;
text-align: center;
font-size: 48px;
margin-bottom: 20px;
}
.chatbot .message.ai {
background-color: #4444ff;
color: white;
border-radius: 10px;
padding: 8px;
}
.chatbot .message.user {
background-color: #2222aa;
color: white;
border-radius: 10px;
padding: 8px;
}
""") as demo:
gr.HTML("<h1>💀 Welcome to Ghost AI 💀</h1>")
chatbot = gr.Chatbot()
txt = gr.Textbox(placeholder="Type your message here...")
# Submit user input to chat function
txt.submit(chat, [txt, chatbot], chatbot)
demo.launch()
|