tharun1507/Decipher-the_code_explainer
0
1# -*- coding: utf-8 -*-2"""Copy of Code Explainer.ipynb3 4Automatically generated by Colaboratory.5 6Original file is located at7 https://colab.research.google.com/drive/1tBrZ0R8BVUoBwSSkv07CMTrXnsYKmeXI8"""9 10 11#@title Code Explainer12import gradio as gr13import google.generativeai as palm14 15# load model16# PaLM API Key here17palm.configure(api_key='AIzaSyArybMiPDZARDCz7yZBjzEMx6zXgOXHtoc')18# Use the palm.list_models function to find available models19# PaLM 2 available in 4 sizes: Gecko, Otter, Bison and Unicorn (largest)20models = [m for m in palm.list_models() if 'generateText' in m.supported_generation_methods]21model = models[0].name22# define completion function23def get_completion(code_snippet):24 25 python_code_examples = f"""26 ---------------------27 Example 1: Code Snippet28 x = 1029 def foo():30 global x31 x = 532 foo()33 print(x)34 Correct output: 535 Code Explanation: Inside the foo function, the global keyword is used to modify the global variable x to be 5.36 So, print(x) outside the function prints the modified value, which is 5.37 ---------------------38 Example 2: Code Snippet39 def modify_list(input_list):40 input_list.append(4)41 input_list = [1, 2, 3]42 my_list = [0]43 modify_list(my_list)44 print(my_list)45 Correct output: [0, 4]46 Code Explanation: Inside the modify_list function, an element 4 is appended to input_list.47 Then, input_list is reassigned to a new list [1, 2, 3], but this change doesn't affect the original list.48 So, print(my_list) outputs [0, 4].49 ---------------------50 """51 52 prompt = f"""53 Your task is to act as any language Code Explainer.54 I'll give you a Code Snippet.55 Your job is to explain the Code Snippet step-by-step.56 Break down the code into as many steps as possible.57 Share intermediate checkpoints & steps along with results.58 Few good examples of Python code output between #### separator:59 ####60 {python_code_examples}61 ####62 Code Snippet is shared below, delimited with triple backticks:63 ```64 {code_snippet}65 ```66 """67 68 completion = palm.generate_text(69 model=model,70 prompt=prompt,71 temperature=0,72 # The maximum length of the response73 max_output_tokens=500,74 )75 response = completion.result76 return response77 78# define app UI79iface = gr.Interface(fn=get_completion, inputs=[gr.Textbox(label="Insert Code Snippet",lines=5)],80 outputs=[gr.Textbox(label="Explanation Here",lines=8)],81 title="Code Explainer"82 )83 84iface.launch()85 86 87 88 