Spaces:
Runtime error
Runtime error
File size: 2,293 Bytes
742a305 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
# Load the tokenizer and model
tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/DeepSeek-V3")
model = AutoModelForCausalLM.from_pretrained(
"deepseek-ai/DeepSeek-V3",
torch_dtype=torch.float16, # Use FP16 for efficiency
device_map="auto" # Automatically use GPU if available
)
# Function to generate text
def generate_text(prompt, max_length=100, temperature=0.7, top_k=50):
# Tokenize the input prompt
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
# Generate text
outputs = model.generate(
inputs["input_ids"],
max_length=max_length,
temperature=temperature,
top_k=top_k,
do_sample=True
)
# Decode the generated text
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
return generated_text
# Gradio interface
def gradio_interface(prompt, max_length, temperature, top_k):
try:
output = generate_text(prompt, max_length, temperature, top_k)
return output
except Exception as e:
return f"Error: {str(e)}"
# Custom CSS for a fancy theme
custom_css = """
.gradio-container {
background: linear-gradient(135deg, #1e3c72, #2a5298);
color: white;
font-family: 'Arial', sans-serif;
}
h1 {
color: #ffd700;
text-align: center;
}
.description {
color: #ffffff;
text-align: center;
font-size: 16px;
}
.input-label, .output-label {
color: #ffffff;
}
.slider-label {
color: #ffffff;
}
"""
# Create the Gradio app
iface = gr.Interface(
fn=gradio_interface,
inputs=[
gr.Textbox(label="Enter your prompt", lines=3, placeholder="Once upon a time..."),
gr.Slider(minimum=10, maximum=200, value=100, label="Max Length"),
gr.Slider(minimum=0.1, maximum=1.0, value=0.7, label="Temperature"),
gr.Slider(minimum=1, maximum=100, value=50, label="Top-k Sampling")
],
outputs=gr.Textbox(label="Generated Text", lines=10),
title="DreamWeaver AI",
description="Crafting Stories, One Prompt at a Time. Generate text using the DeepSeek-V3 model. Adjust the parameters to control the output.",
css=custom_css
)
# Launch the app
iface.launch() |