Spaces:
Runtime error
Runtime error
wowww
Browse files- app.py +42 -0
- requirements.txt +0 -0
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load model and tokenizer
|
| 6 |
+
model_id = "deepseek-ai/DeepSeek-R1"
|
| 7 |
+
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
|
| 10 |
+
|
| 11 |
+
# Inference function
|
| 12 |
+
def generate(prompt, max_new_tokens=256, temperature=0.7, top_p=0.95):
|
| 13 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 14 |
+
output = model.generate(
|
| 15 |
+
**inputs,
|
| 16 |
+
max_new_tokens=max_new_tokens,
|
| 17 |
+
temperature=temperature,
|
| 18 |
+
top_p=top_p,
|
| 19 |
+
do_sample=True,
|
| 20 |
+
pad_token_id=tokenizer.eos_token_id
|
| 21 |
+
)
|
| 22 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 23 |
+
return response
|
| 24 |
+
|
| 25 |
+
# Gradio UI
|
| 26 |
+
with gr.Blocks() as demo:
|
| 27 |
+
gr.Markdown("## 🚀 DeepSeek-R1 - Hugging Face Space Demo")
|
| 28 |
+
|
| 29 |
+
with gr.Row():
|
| 30 |
+
prompt = gr.Textbox(label="Prompt", placeholder="Ask me anything...")
|
| 31 |
+
|
| 32 |
+
with gr.Row():
|
| 33 |
+
max_tokens = gr.Slider(64, 1024, value=256, step=16, label="Max new tokens")
|
| 34 |
+
temperature = gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Temperature")
|
| 35 |
+
top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p")
|
| 36 |
+
|
| 37 |
+
output = gr.Textbox(label="Generated Text")
|
| 38 |
+
|
| 39 |
+
generate_btn = gr.Button("Generate")
|
| 40 |
+
generate_btn.click(fn=generate, inputs=[prompt, max_tokens, temperature, top_p], outputs=output)
|
| 41 |
+
|
| 42 |
+
demo.launch()
|
requirements.txt
ADDED
|
File without changes
|