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.
import google.generativeai as plam
import os
plam.configure(api_key='xxxxxxxxxxxxxxxxxx')
models=[m for m in plam.list_models() if 'generateText' in m.supported_generation_methods]
model=models[0].name
model
models/text-bison-001
input_prompt='What is prompt engineering?'
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.
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
Input_code=f"""
X=10
if(X>0):
print("X is positive")
else:
print("X is Negative")
"""
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)
Response=get_Response(prompt)
print(Response)
-# This code is to check if the value of variable X is positive or negative.
- The variable X is assigned the value 10.
- The if statement checks if the value of X is greater than 0.
- If the value of X is greater than 0, the print statement "X is positive" is executed.
- 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.
!pip install 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)