Sujatha commited on
Commit
742a305
Β·
verified Β·
1 Parent(s): 4e58414

Create app.py

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