👨‍💻👨‍🏫 Code Explainer with Generative Ai 🤖📚

This project showcases how to use Google's Generative AI to explain Python code snippets. The project involves configuring the API, selecting a model, defining a response function, preparing a prompt, generating a response, and building an interactive web interface using Gradio. The resulting application allows users to input Python code and receive a detailed explanation of its functionality, making it a valuable tool for learning and debugging.

1️⃣ Import required Libraries

import google.generativeai as plam
import os

2️⃣ Configure Api key

plam.configure(api_key='xxxxxxxxxxxxxxxxxx')

3️⃣ Select the model(generate text)

models=[m for m in plam.list_models() if 'generateText' in m.supported_generation_methods]
model=models[0].name
model

models/text-bison-001

Sample Prompting🧪

input_prompt='What is prompt engineering?'
Here we use gererate_text function to retrive the information from generative ai
Ans=plam.generate_text(
    model=model,
    prompt=input_prompt,
    temperature=0, #for more determinastic result and 1 for more randomness result
    max_output_tokens=100
)
Ans.result

Prompt engineering is the process of designing and implementing prompts that can be used to guide language models to generate specific kinds of text. This can be used for a variety of purposes, such as generating creative content, completing tasks, or answering questions. Prompt engineering is a relatively new field, and there is still a lot of research to be done. However, it has the potential to be a powerful tool for creating new kinds of language models and applications.

4️⃣ Make Response Function

We make this function to get response from generative ai API

def get_Response(prompt):
  Ans=plam.generate_text(
    model=model,
    prompt=prompt,
    temperature=0, #for more determinastic result and 1 for more randomness result
    max_output_tokens=500
  )
  response=Ans.result
  return response

5️⃣ Input the code snippet

Input_code=f"""
X=10
if(X>0):
  print("X is positive")
else:
  print("X is Negative")
"""

6️⃣ Prepare the prompt

prompt=f"""
Your task is to act as a Python Code Explainer.
I'll give you a code Snippet.
Your job is to explain the code in simplest way with steps.
Also, give the final result of the code with reason.
Code snippet is shared below, delimited with triple backticks:
-```-
{Input_code}
-```-
"""
print(prompt)

7️⃣ Get Response

Response=get_Response(prompt)
print(Response)

-# This code is to check if the value of variable X is positive or negative.

  1. The variable X is assigned the value 10.
  2. The if statement checks if the value of X is greater than 0.
  3. If the value of X is greater than 0, the print statement "X is positive" is executed.
  4. If the value of X is not greater than 0, the print statement "X is Negative" is executed.

The final result of the code is "X is positive" because the value of X is greater than 0.

Make an Interface for This Explainer 🎴

install gradio

!pip install gradio

make interface using gradio

import gradio as gr
import os
import google.generativeai as plam

#Configure API key
plam.configure(api_key='AIzaSyByPkcLmF72Itb1yX17F6L65sg4Z4bq904')

#select model
models=[m for m in plam.list_models() if 'generateText' in m.supported_generation_methods]
model=models[0].name


#Get response function
def get_Response(input_txt):
  prompt=f"""
  Your task is to act as a Python Code Explainer.
  I'll give you a code Snippet.
  Your job is to explain the code in simplest way with steps.
  Also, give the final result of the code with reason.
  Code snippet is shared below, delimited with triple backticks:
  -```-
  {input_txt}
  -```-
  """
  Ans=plam.generate_text(
    model=model,
    prompt=prompt,
    temperature=0, #for more determinastic result and 1 for more randomness result
    max_output_tokens=500
  )
  response=Ans.result
  return response


#For interface
iface=gr.Interface(fn=get_Response,inputs=[gr.Textbox(label="Insert code Snippet",lines=8)],outputs=[gr.Textbox(label="Explanation here",lines=8)],title="Python Code Explainer")

iface.launch(share=True,debug=True)

Screens

1. Code explainer

image

2. Even Odd number

image