Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import torch | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| # Model name | |
| model_name = "deepseek-ai/DeepSeek-R1" | |
| # Load tokenizer | |
| tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True) | |
| # Load model with quantization | |
| model = AutoModelForCausalLM.from_pretrained( | |
| model_name, | |
| trust_remote_code=True | |
| ).to("cuda" if torch.cuda.is_available() else "cpu") | |
| # Define the text generation function | |
| def generate_response(prompt): | |
| inputs = tokenizer(prompt, return_tensors="pt").to(model.device) | |
| with torch.no_grad(): | |
| output = model.generate(**inputs, max_length=150) | |
| return tokenizer.decode(output[0], skip_special_tokens=True) | |
| # Set up Gradio UI | |
| interface = gr.Interface( | |
| fn=generate_response, | |
| inputs=gr.Textbox(label="Enter your prompt"), | |
| outputs=gr.Textbox(label="AI Response"), | |
| title="DeepSeek-R1 Chatbot", | |
| description="Enter a prompt and receive a response from DeepSeek-R1." | |
| ) | |
| # Launch the app | |
| interface.launch() | |