File size: 1,082 Bytes
c0d1457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer

# Load pre-trained model and tokenizer
model_name = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
tokenizer = AutoTokenizer.from_pretrained(model_name)  # Use AutoTokenizer to automatically detect the correct tokenizer
model = AutoModelForCausalLM.from_pretrained(model_name)  # Use AutoModelForCausalLM for causal language models

def generate_response(message, history):
    # Combine the conversation history with the new message
    input_text = f"{message}"
    
    # Tokenize input text
    inputs = tokenizer.encode(input_text, return_tensors="pt")
    
    # Generate response using the model
    outputs = model.generate(inputs, max_length=50, num_return_sequences=1)
    
    # Decode generated text
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    
    return response

# Create ChatInterface
demo = gr.ChatInterface(
    fn=generate_response,
    title="Chat with DeepSeek",
    description="A simple chatbot powered by DeepSeek."
)

# Launch the app
demo.launch()