File size: 1,543 Bytes
b6cca88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6183dca
 
 
 
 
 
 
 
b6cca88
 
 
 
 
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
import torch
import transformers
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import gradio as gr

tokenizer = AutoTokenizer.from_pretrained("AhmedSSoliman/MarianCG-DJANGO")
model = AutoModelForSeq2SeqLM.from_pretrained("AhmedSSoliman/MarianCG-DJANGO")

def generate_code(NL):
    inputs = tokenizer(NL, padding="max_length", truncation=True, max_length=512, return_tensors="pt")
    input_ids = inputs.input_ids
    attention_mask = inputs.attention_mask
    outputs = model.generate(input_ids, attention_mask=attention_mask)

    output_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return output_code
    

iface = gr.Interface(fn=generate_code, inputs="text", outputs="text", 
                     examples=[["define the method i with an argument self."],
                               ["substitute asvar for self.asvar."],
                               ["convert host to lowercase."],
                               ["for every var in self.vars,"],
                               ["call the method parser.delete_first_token."]],
                     title="MarianCG: A Code Generation Transformer Model Inspired by Machine Translation",
                     description="This is a code generation model which can generate code from the natural language description")
iface.launch()
#iface.launch(share=True)

#output_text = gr.outputs.Textbox()
#gr.Interface(generate_code,"textbox", output_text, title="MarianCG model for Code Generation", description="MarianCG model for Code Generation").launch()