acecalisto3 commited on
Commit
d617eb9
1 Parent(s): caef4e5

Update code_generator.py

Browse files
Files changed (1) hide show
  1. code_generator.py +22 -3
code_generator.py CHANGED
@@ -1,15 +1,24 @@
1
  import transformers
 
2
 
3
  def generate(idea):
 
 
 
 
 
 
 
 
 
4
  # Load the code generation model
5
- model_name = "PhiCo-D-Instruk"
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")
@@ -19,7 +28,17 @@ def generate(idea):
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
 
 
 
 
 
 
 
 
 
1
  import transformers
2
+ from transformers import pipeline
3
 
4
  def generate(idea):
5
+ """Generates code based on a given idea using the PhiCo-D-Instruk model.
6
+
7
+ Args:
8
+ idea: The idea for the code to be generated.
9
+
10
+ Returns:
11
+ The generated code as a string.
12
+ """
13
+
14
  # Load the code generation model
15
+ model_name = "bigscience/T0_3B" # Use a model that works for code generation
16
  model = transformers.AutoModelForCausalLM.from_pretrained(model_name)
17
  tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
18
 
19
  # Generate the code
20
  input_text = f"""
21
  # Idea: {idea}
 
22
  # Code:
23
  """
24
  input_ids = tokenizer.encode(input_text, return_tensors="pt")
 
28
  num_return_sequences=1,
29
  no_repeat_ngram_size=2,
30
  early_stopping=True,
31
+ temperature=0.7, # Adjust temperature for creativity
32
+ top_k=50, # Adjust top_k for diversity
33
  )
34
  generated_code = tokenizer.decode(output_sequences[0], skip_special_tokens=True)
35
 
36
+ # Remove the prompt and formatting
37
+ generated_code = generated_code.split("\n# Code:")[1].strip()
38
+
39
+ return generated_code
40
+
41
+ # Example usage
42
+ idea = "Write a Python function to calculate the factorial of a number"
43
+ code = generate(idea)
44
+ print(code)