Spaces:
Runtime error

File size: 1,538 Bytes
6e16574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
862b6ef
 
6e16574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ✅ 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"