File size: 1,009 Bytes
416fd50
 
0aaac91
 
 
79342f8
0aaac91
79342f8
 
081b87c
79342f8
7b3007d
0aaac91
79342f8
081b87c
 
 
79342f8
 
416fd50
79342f8
 
 
 
 
 
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
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import os

# Check if the token is being accessed
hf_token = os.environ.get("HF_HOME", None)

# Load the model and tokenizer
model_name = "meta-llama/CodeLlama-7b-hf"
model = AutoModelForCausalLM.from_pretrained(model_name, token=hf_token, torch_dtype="float16", device_map="auto")
tokenizer = AutoTokenizer.from_pretrained(model_name, token=hf_token)


def generate_code(prompt):
    batch_size = 1
    inputs = tokenizer(input_texts, return_tensors="pt", padding=True, truncation=True, max_length=512)
    outputs = model.generate(inputs['input_ids'], max_length=512, num_return_sequences=batch_size)
    code = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return code

# Set up the Gradio interface
demo = gr.Interface(fn=generate_code, 
             inputs="text", 
             outputs="text", 
             title="CodeLlama 7B Model",
             description="Generate code with CodeLlama-7b-hf.").launch()