File size: 1,120 Bytes
654faf6
 
 
0f08d47
 
 
654faf6
 
 
 
0f08d47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
654faf6
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Install required libraries
pip install transformers huggingface_hub gradio torch
# Import necessary modules
from huggingface_hub import login
from transformers import AutoTokenizer, AutoModelForCausalLM

# Log in to Hugging Face (replace 'your_token' with your actual Hugging Face token)
login("your_huggingface_token")

# Load the tokenizer and model from Hugging Face
tokenizer = AutoTokenizer.from_pretrained("Salesforce/codegen-350M-multi")
model = AutoModelForCausalLM.from_pretrained("Salesforce/codegen-350M-multi")

# Input text for code generation
text = "def bubble_sort(list_elements):"

# Tokenize the input text
input_ids = tokenizer(text, return_tensors="pt").input_ids

# Generate code based on the input text
generated_ids = model.generate(
    input_ids,
    max_length=200,  # Adjust as needed
    num_return_sequences=1,  # Number of generated sequences to return
    pad_token_id=tokenizer.eos_token_id  # Handle padding tokens
)

# Decode the generated tokens to text
generated_code = tokenizer.decode(generated_ids[0], skip_special_tokens=True)

# Output the generated code
print(generated_code)