File size: 1,131 Bytes
3d69e25
 
 
 
 
 
 
 
5f10640
3d69e25
5f10640
3d69e25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
import gradio as gr
import os
import torch
from transformers import RobertaTokenizer, T5ForConditionalGeneration

model_name = "ThoughtFocusAI/CodeGeneration-CodeT5-small"
device = "cuda" if torch.cuda.is_available() else "cpu"
model = T5ForConditionalGeneration.from_pretrained(
    model_name).to(device)
tokenizer = RobertaTokenizer.from_pretrained(
    model_name)


def generate_code(user_input):
    query = "Generate Python: " + user_input
    encoded_text = tokenizer(query, return_tensors='pt', padding='max_length',
                             truncation=True, max_length=512).input_ids.to(device)

    # inference
    generated_code = model.generate(encoded_text, max_length=512)

    # decode generated tokens
    decoded_code = tokenizer.decode(
        generated_code.numpy()[0], skip_special_tokens=True)

    return decoded_code


interface = gr.Interface(fn=generate_code,
                         inputs=gr.inputs.Textbox(
                             lines=3, label="Enter Text", placeholder="Ex-Add two numbers"),
                         outputs=gr.outputs.Textbox(label="Generated Code"))
interface.launch()