Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
],
|
|
|
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load the DeepSeek-R1-Distill-Qwen-1.5B-uncensored model
|
| 6 |
+
model_id = "thirdeyeai/DeepSeek-R1-Distill-Qwen-1.5B-uncensored"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
model_id,
|
| 10 |
+
torch_dtype=torch.float16, # Use float16 for efficiency
|
| 11 |
+
low_cpu_mem_usage=True,
|
| 12 |
+
device_map="auto" # Automatically use available devices
|
| 13 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
def generate_text(prompt, max_length=100, temperature=0.7, top_p=0.9):
|
| 16 |
+
"""Generate text based on prompt"""
|
| 17 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 18 |
+
|
| 19 |
+
# Generate
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
generation_output = model.generate(
|
| 22 |
+
input_ids=inputs.input_ids,
|
| 23 |
+
attention_mask=inputs.attention_mask,
|
| 24 |
+
max_length=len(inputs.input_ids[0]) + max_length,
|
| 25 |
+
temperature=temperature,
|
| 26 |
+
top_p=top_p,
|
| 27 |
+
do_sample=True,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Decode and return only the generated part
|
| 31 |
+
generated_text = tokenizer.decode(generation_output[0], skip_special_tokens=True)
|
| 32 |
+
return generated_text
|
| 33 |
+
|
| 34 |
+
# Create Gradio interface
|
| 35 |
+
demo = gr.Interface(
|
| 36 |
+
fn=generate_text,
|
| 37 |
+
inputs=[
|
| 38 |
+
gr.Textbox(lines=5, placeholder="Enter your prompt here...", label="Prompt"),
|
| 39 |
+
gr.Slider(minimum=10, maximum=500, value=100, step=10, label="Max Length"),
|
| 40 |
+
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
|
| 41 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-p")
|
| 42 |
],
|
| 43 |
+
outputs=gr.Textbox(label="Generated Text"),
|
| 44 |
+
title="DeepSeek-R1-Distill-Qwen-1.5B Demo",
|
| 45 |
+
description="Enter a prompt to generate text with the DeepSeek-R1-Distill-Qwen-1.5B-uncensored model."
|
| 46 |
)
|
| 47 |
|
| 48 |
+
# Launch the app
|
| 49 |
+
demo.launch()
|
|
|