Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
+
|
5 |
+
# Load CodeGen model and tokenizer
|
6 |
+
model_name = "Salesforce/codegen-2B-mono" # Replace with the specific CodeGen model variant you need
|
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
|
28 |
+
inputs = tokenizer(input_text, return_tensors="pt").to(device)
|
29 |
+
|
30 |
+
# Generate text based on model output
|
31 |
+
outputs = model.generate(
|
32 |
+
inputs.input_ids,
|
33 |
+
max_length=max_length,
|
34 |
+
temperature=temperature,
|
35 |
+
top_p=top_p,
|
36 |
+
top_k=top_k,
|
37 |
+
do_sample=True,
|
38 |
+
num_return_sequences=1,
|
39 |
+
no_repeat_ngram_size=2
|
40 |
+
)
|
41 |
+
|
42 |
+
# Decode and return the generated text
|
43 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
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 |
+
with gr.Row():
|
56 |
+
input_text = gr.Textbox(
|
57 |
+
label="Input Text",
|
58 |
+
placeholder="Type your question or prompt here",
|
59 |
+
lines=3
|
60 |
+
)
|
61 |
+
|
62 |
+
# Sliders for customization
|
63 |
+
with gr.Row():
|
64 |
+
max_length = gr.Slider(
|
65 |
+
label="Max Length",
|
66 |
+
minimum=50,
|
67 |
+
maximum=1024,
|
68 |
+
step=10,
|
69 |
+
value=250,
|
70 |
+
interactive=True
|
71 |
+
)
|
72 |
+
temperature = gr.Slider(
|
73 |
+
label="Temperature",
|
74 |
+
minimum=0.1,
|
75 |
+
maximum=1.0,
|
76 |
+
step=0.1,
|
77 |
+
value=0.7,
|
78 |
+
interactive=True
|
79 |
+
)
|
80 |
+
top_p = gr.Slider(
|
81 |
+
label="Top-p (Nucleus Sampling)",
|
82 |
+
minimum=0.1,
|
83 |
+
maximum=1.0,
|
84 |
+
step=0.1,
|
85 |
+
value=0.9,
|
86 |
+
interactive=True
|
87 |
+
)
|
88 |
+
top_k = gr.Slider(
|
89 |
+
label="Top-k (Sampling Limit)",
|
90 |
+
minimum=0,
|
91 |
+
maximum=100,
|
92 |
+
step=5,
|
93 |
+
value=50,
|
94 |
+
interactive=True
|
95 |
+
)
|
96 |
+
|
97 |
+
# Output box
|
98 |
+
output_text = gr.Textbox(
|
99 |
+
label="Generated Response",
|
100 |
+
placeholder="The model's response will appear here",
|
101 |
+
lines=15
|
102 |
+
)
|
103 |
+
|
104 |
+
# Generate button to trigger response generation
|
105 |
+
generate_button = gr.Button("Generate Response")
|
106 |
+
generate_button.click(
|
107 |
+
fn=generate_response,
|
108 |
+
inputs=[input_text, max_length, temperature, top_p, top_k],
|
109 |
+
outputs=output_text
|
110 |
+
)
|
111 |
+
|
112 |
+
# Launch the app
|
113 |
+
codegen_app.launch()
|