File size: 2,041 Bytes
ea5a9be
 
 
 
a4782b5
 
eb5d232
ea5a9be
 
 
eb5d232
 
ea5a9be
 
eb5d232
ea5a9be
 
dc2d2ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea5a9be
eb5d232
dc2d2ec
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
import torch

model_id = "deepseek-ai/deepseek-coder-7b-base"

# Load tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype=torch.float16,
    device_map="auto"
)

# Create pipeline
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)

# Memory to store chat history
chat_history = []

# Format prompt with history
def format_prompt(history, user_input):
    prompt = ""
    for i, (user, bot) in enumerate(history):
        prompt += f"User: {user}\nAssistant: {bot}\n"
    prompt += f"User: {user_input}\nAssistant:"
    return prompt

# Chat function
def chat(user_input):
    global chat_history
    prompt = format_prompt(chat_history, user_input)
    output = pipe(prompt, max_new_tokens=200, do_sample=True, temperature=0.7)[0]['generated_text']
    
    # Extract new response only (everything after last "Assistant:")
    assistant_response = output.split("Assistant:")[-1].strip()

    # Add to history
    chat_history.append((user_input, assistant_response))

    # Build full conversation display
    chat_display = ""
    for user, bot in chat_history:
        chat_display += f"πŸ§‘β€πŸ’» User: {user}\nπŸ€– Assistant: {bot}\n\n"

    return chat_display.strip()

# Reset chat
def reset_chat():
    global chat_history
    chat_history = []
    return ""

# Gradio UI
with gr.Blocks(title="🧠 DeepSeek 7B Chat with Memory") as demo:
    gr.Markdown("## πŸ€– DeepSeek Coder R1 7B\nChat with memory. Ask coding questions or continue a conversation.")
    chatbot = gr.Textbox(lines=20, interactive=False, label="Chat History")
    msg = gr.Textbox(label="Type your message here", placeholder="What can I help you with today?")
    send_btn = gr.Button("Send")
    clear_btn = gr.Button("🧹 Clear Chat")

    send_btn.click(chat, inputs=msg, outputs=chatbot)
    clear_btn.click(reset_chat, outputs=chatbot)

demo.launch()