Spaces:
Running
Running
acecalisto3
commited on
Commit
•
44b0d60
1
Parent(s):
b3d3434
Create germmacode/code_generator.py
Browse files- germmacode/code_generator.py +25 -0
germmacode/code_generator.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import transformers
|
2 |
+
|
3 |
+
def generate(idea):
|
4 |
+
# Load the code generation model
|
5 |
+
model_name = "Salesforce/codegen-350M-mono"
|
6 |
+
model = transformers.AutoModelForCausalLM.from_pretrained(model_name)
|
7 |
+
tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
|
8 |
+
|
9 |
+
# Generate the code
|
10 |
+
input_text = f"""
|
11 |
+
# Idea: {idea}
|
12 |
+
|
13 |
+
# Code:
|
14 |
+
"""
|
15 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
16 |
+
output_sequences = model.generate(
|
17 |
+
input_ids=input_ids,
|
18 |
+
max_length=1024,
|
19 |
+
num_return_sequences=1,
|
20 |
+
no_repeat_ngram_size=2,
|
21 |
+
early_stopping=True,
|
22 |
+
)
|
23 |
+
generated_code = tokenizer.decode(output_sequences[0], skip_special_tokens=True)
|
24 |
+
|
25 |
+
return generated_code
|