Spaces:
Runtime error
Runtime error
goldenboy3332
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -3,25 +3,15 @@ import torch
|
|
3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
|
5 |
# Load CodeGen model and tokenizer
|
6 |
-
model_name = "Salesforce/codegen-2B-mono"
|
7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
model = model.to(device)
|
11 |
|
12 |
-
def generate_response(input_text, max_length, temperature, top_p, top_k):
|
13 |
"""
|
14 |
Generate response using the CodeGen model based on user input and selected parameters.
|
15 |
-
|
16 |
-
Args:
|
17 |
-
input_text (str): The prompt or question for the model.
|
18 |
-
max_length (int): Maximum length of the generated text.
|
19 |
-
temperature (float): Sampling temperature for response creativity.
|
20 |
-
top_p (float): Nucleus sampling for generating top-p probable tokens.
|
21 |
-
top_k (int): Top-k sampling for generating top-k probable tokens.
|
22 |
-
|
23 |
-
Returns:
|
24 |
-
str: Generated response from CodeGen.
|
25 |
"""
|
26 |
try:
|
27 |
# Encode input and prepare input tensor
|
@@ -44,55 +34,48 @@ def generate_response(input_text, max_length, temperature, top_p, top_k):
|
|
44 |
return response
|
45 |
|
46 |
except Exception as e:
|
47 |
-
return f"Error: {str(e)}"
|
48 |
|
49 |
# Create Gradio interface
|
50 |
with gr.Blocks() as codegen_app:
|
51 |
gr.Markdown("# CodeGen-powered Text Generation")
|
52 |
-
gr.Markdown("Generate high-quality, high-quantity output using the CodeGen model.")
|
53 |
|
54 |
# Input box for user prompt
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
)
|
61 |
|
62 |
# Sliders for customization
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
maximum=100,
|
92 |
-
step=5,
|
93 |
-
value=50,
|
94 |
-
interactive=True
|
95 |
-
)
|
96 |
|
97 |
# Output box
|
98 |
output_text = gr.Textbox(
|
@@ -101,7 +84,7 @@ with gr.Blocks() as codegen_app:
|
|
101 |
lines=15
|
102 |
)
|
103 |
|
104 |
-
# Generate button
|
105 |
generate_button = gr.Button("Generate Response")
|
106 |
generate_button.click(
|
107 |
fn=generate_response,
|
|
|
3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
|
5 |
# Load CodeGen model and tokenizer
|
6 |
+
model_name = "Salesforce/codegen-2B-mono"
|
7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
model = model.to(device)
|
11 |
|
12 |
+
def generate_response(input_text, max_length=250, temperature=0.7, top_p=0.9, top_k=50):
|
13 |
"""
|
14 |
Generate response using the CodeGen model based on user input and selected parameters.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
"""
|
16 |
try:
|
17 |
# Encode input and prepare input tensor
|
|
|
34 |
return response
|
35 |
|
36 |
except Exception as e:
|
37 |
+
return f"Error generating response: {str(e)}"
|
38 |
|
39 |
# Create Gradio interface
|
40 |
with gr.Blocks() as codegen_app:
|
41 |
gr.Markdown("# CodeGen-powered Text Generation")
|
|
|
42 |
|
43 |
# Input box for user prompt
|
44 |
+
input_text = gr.Textbox(
|
45 |
+
label="Input Text",
|
46 |
+
placeholder="Type your question or prompt here",
|
47 |
+
lines=3
|
48 |
+
)
|
|
|
49 |
|
50 |
# Sliders for customization
|
51 |
+
max_length = gr.Slider(
|
52 |
+
label="Max Length",
|
53 |
+
minimum=50,
|
54 |
+
maximum=1024,
|
55 |
+
step=10,
|
56 |
+
value=250
|
57 |
+
)
|
58 |
+
temperature = gr.Slider(
|
59 |
+
label="Temperature",
|
60 |
+
minimum=0.1,
|
61 |
+
maximum=1.0,
|
62 |
+
step=0.1,
|
63 |
+
value=0.7
|
64 |
+
)
|
65 |
+
top_p = gr.Slider(
|
66 |
+
label="Top-p (Nucleus Sampling)",
|
67 |
+
minimum=0.1,
|
68 |
+
maximum=1.0,
|
69 |
+
step=0.1,
|
70 |
+
value=0.9
|
71 |
+
)
|
72 |
+
top_k = gr.Slider(
|
73 |
+
label="Top-k (Sampling Limit)",
|
74 |
+
minimum=0,
|
75 |
+
maximum=100,
|
76 |
+
step=5,
|
77 |
+
value=50
|
78 |
+
)
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
# Output box
|
81 |
output_text = gr.Textbox(
|
|
|
84 |
lines=15
|
85 |
)
|
86 |
|
87 |
+
# Generate button
|
88 |
generate_button = gr.Button("Generate Response")
|
89 |
generate_button.click(
|
90 |
fn=generate_response,
|