ashwinR/PythonCodeExplainer
0
1import gradio as gr2 3from transformers import (4 AutoModelForSeq2SeqLM,5 AutoTokenizer,6 AutoConfig,7 pipeline,8)9 10model_name = "ashwinR/CodeExplainer"11 12tokenizer = AutoTokenizer.from_pretrained(model_name, padding=True)13 14model = AutoModelForSeq2SeqLM.from_pretrained(model_name)15 16config = AutoConfig.from_pretrained(model_name)17 18model.eval()19 20pipe = pipeline("summarization", model=model_name, config=config, tokenizer=tokenizer)21 22def generate_text(text_prompt):23 response = pipe(text_prompt)24 return response[0]['summary_text']25 26textbox1 = gr.Textbox(value = """27class Solution(object):28 def isValid(self, s):29 stack = []30 mapping = {")": "(", "}": "{", "]": "["}31 for char in s:32 if char in mapping:33 top_element = stack.pop() if stack else '#'34 if mapping[char] != top_element:35 return False36 else:37 stack.append(char)38 return not stack""")39 40textbox2 = gr.Textbox()41 42if __name__ == "__main__":43 gr.Textbox("The Inference Takes about 1 min 30 seconds")44 with gr.Blocks() as demo:45 gr.Interface(fn = generate_text, inputs = textbox1, outputs = textbox2)46 with gr.Row():47 gr.Image(value = "output.jpg", label = "Sample Code for Checking if a Binary Tree is Mirrored")48 gr.Image(value = "code.jpg", label = "Sample Output Explaination in Natural language")49 demo.launch()