File size: 1,394 Bytes
fd92166
49ea7d7
c838a56
49ea7d7
ac75fba
49ea7d7
fd92166
49ea7d7
 
 
 
 
fd92166
49ea7d7
ac75fba
312f78d
82b0953
312f78d
82b0953
 
312f78d
ac75fba
312f78d
 
 
 
 
 
ac75fba
312f78d
 
 
 
 
 
 
 
ac75fba
312f78d
 
 
49ea7d7
312f78d
01ea371
49ea7d7
ac75fba
 
01ea371
 
ac75fba
49ea7d7
fd92166
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

model_name = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"

print("Loading model...")
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    device_map="auto"
)
print("Model loaded!")

def chat(message, history):
    try:
        # Build conversation from history (each item is [user_msg, bot_msg])
        conversation = ""
        for user_msg, bot_msg in history:
            conversation += f"User: {user_msg}\nAssistant: {bot_msg}\n"
        conversation += f"User: {message}\nAssistant:"

        inputs = tokenizer(
            conversation,
            return_tensors="pt",
            truncation=True,
            max_length=1024
        ).to(model.device)

        outputs = model.generate(
            **inputs,
            max_new_tokens=150,
            temperature=0.7,
            top_p=0.9,
            repetition_penalty=1.1,
            do_sample=True
        )

        response = tokenizer.decode(outputs[0], skip_special_tokens=True)
        response = response.split("Assistant:")[-1].strip()
        return response

    except Exception as e:
        return f"Error: {str(e)}"

iface = gr.ChatInterface(
    fn=chat,
    title="DeepSeek Chat AI",
    description="Chat with DeepSeek 1.5B model"
)

iface.launch()