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()