Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,8 +5,12 @@ import torch
|
|
| 5 |
# Load a model suited for code generation
|
| 6 |
model_name = "Salesforce/codegen-350M-mono" # Choose a suitable model for your needs
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Set the device
|
| 11 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
model.to(device)
|
|
@@ -18,9 +22,6 @@ def generate_code(prompt):
|
|
| 18 |
# Tokenize the input and set pad token
|
| 19 |
input_tensor = tokenizer(full_prompt, return_tensors="pt", padding=True, truncation=True).to(device)
|
| 20 |
|
| 21 |
-
# Set pad_token_id if not already set
|
| 22 |
-
pad_token_id = tokenizer.pad_token_id if tokenizer.pad_token_id is not None else tokenizer.eos_token_id
|
| 23 |
-
|
| 24 |
# Generate code with attention mask
|
| 25 |
with torch.no_grad():
|
| 26 |
generated_ids = model.generate(
|
|
@@ -29,7 +30,7 @@ def generate_code(prompt):
|
|
| 29 |
max_length=300, # Adjust this length as needed
|
| 30 |
num_beams=5, # This controls the diversity of outputs
|
| 31 |
early_stopping=True,
|
| 32 |
-
pad_token_id=pad_token_id # Set pad token id
|
| 33 |
)
|
| 34 |
|
| 35 |
# Decode and return the generated code
|
|
@@ -39,5 +40,5 @@ def generate_code(prompt):
|
|
| 39 |
# Set up the Gradio interface
|
| 40 |
iface = gr.Interface(fn=generate_code, inputs="text", outputs="text", allow_flagging="never")
|
| 41 |
|
| 42 |
-
# Launch the app
|
| 43 |
-
iface.launch(server_name="0.0.0.0", server_port=7860
|
|
|
|
| 5 |
# Load a model suited for code generation
|
| 6 |
model_name = "Salesforce/codegen-350M-mono" # Choose a suitable model for your needs
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
| 8 |
|
| 9 |
+
# Set a padding token if it doesn't exist
|
| 10 |
+
if tokenizer.pad_token is None:
|
| 11 |
+
tokenizer.pad_token = tokenizer.eos_token # Set pad_token to eos_token
|
| 12 |
+
|
| 13 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 14 |
# Set the device
|
| 15 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 16 |
model.to(device)
|
|
|
|
| 22 |
# Tokenize the input and set pad token
|
| 23 |
input_tensor = tokenizer(full_prompt, return_tensors="pt", padding=True, truncation=True).to(device)
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
# Generate code with attention mask
|
| 26 |
with torch.no_grad():
|
| 27 |
generated_ids = model.generate(
|
|
|
|
| 30 |
max_length=300, # Adjust this length as needed
|
| 31 |
num_beams=5, # This controls the diversity of outputs
|
| 32 |
early_stopping=True,
|
| 33 |
+
pad_token_id=tokenizer.pad_token_id # Set pad token id
|
| 34 |
)
|
| 35 |
|
| 36 |
# Decode and return the generated code
|
|
|
|
| 40 |
# Set up the Gradio interface
|
| 41 |
iface = gr.Interface(fn=generate_code, inputs="text", outputs="text", allow_flagging="never")
|
| 42 |
|
| 43 |
+
# Launch the app
|
| 44 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|