|
|
| import gradio as gr |
| from transformers import AutoTokenizer, AutoModelForCausalLM |
| import torch |
|
|
| |
| tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/DeepSeek-R1-Distill-Qwen-7B", trust_remote_code=True) |
| model = AutoModelForCausalLM.from_pretrained("deepseek-ai/DeepSeek-R1-Distill-Qwen-7B", trust_remote_code=True, torch_dtype=torch.bfloat16, device_map="auto") |
|
|
| def generate_response(prompt): |
| """ |
| Generates a response from the model. |
| """ |
| inputs = tokenizer(prompt, return_tensors="pt").to(model.device) |
| outputs = model.generate(**inputs, max_new_tokens=512) |
| |
| |
| generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True) |
| |
| return generated_text |
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("# Text-to-Text Generation with DeepSeek-R1-Distill-Qwen-7B") |
| gr.Markdown("Enter a prompt and the model will generate a response.") |
| |
| with gr.Row(): |
| prompt_input = gr.Textbox(label="Prompt", lines=4, placeholder="Enter your prompt here...") |
| |
| with gr.Row(): |
| generate_button = gr.Button("Generate") |
| |
| with gr.Row(): |
| response_output = gr.Textbox(label="Response", lines=8, interactive=False) |
| |
| generate_button.click( |
| fn=generate_response, |
| inputs=prompt_input, |
| outputs=response_output |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch() |
|
|