Spaces:
Runtime error

chatbot / app.py
Neelma's picture
Update app.py
6e16574 verified
Raw History Blame Contribute Delete
1.54 kB
# ✅ Advanced Chatbot with Memory in ONE Cell (Google Colab)
# 1️⃣ Install required libraries
!pip install -q transformers accelerate
# 2️⃣ Import libraries
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
# 3️⃣ Load a better model (Meta LLaMA 3.1 8B-Instruct)
model_name = "meta-llama/Meta-Llama-3-8B-Instruct"
print("⏳ Loading model... (this may take ~1-2 mins)")
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
# 4️⃣ Create a pipeline
chatbot = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
max_length=1024,
temperature=0.7,
top_p=0.9,
repetition_penalty=1.1
)
# 5️⃣ Conversation memory
memory = "You are an advanced helpful chatbot with good reasoning and memory.\n"
print("🤖 Advanced Chatbot Ready! Type 'quit' to exit.\n")
while True:
user_input = input("You: ")
if user_input.lower() in ["quit", "exit", "bye"]:
print("🤖 Chatbot: Goodbye! 👋")
break
# Append user message to memory
memory += f"User: {user_input}\nAssistant:"
# Generate response
response = chatbot(memory, max_length=1024, do_sample=True, top_p=0.9)[0]['generated_text']
# Extract only the new assistant reply
assistant_reply = response.split("Assistant:")[-1].strip().split("\n")[0]
print(f"🤖 Chatbot:", assistant_reply)
# Update memory with assistant reply
memory += f" {assistant_reply}\n"