DEV Community

Cover image for Explaining Code with GPT-3
Ansh Gupta
Ansh Gupta

Posted on

Explaining Code with GPT-3

Hello everyone,In this post I'm gonna show you can build one of the most amazing application out of GPT-3.

A tool which can explain you code so you can roam freely in an unknown territory.
It will be able to explain you code in any major programming language like C,C++,Java,Python, Javascript, Assembly,Golang etc.
So without wasting any time let's start with the coding part.

Part-1:Connecting the API

pip install openai
Enter fullscreen mode Exit fullscreen mode
import os
import openai

openai.api_key = input("API-KEY:")

def result(code):
  response = openai.Completion.create(
    engine="text-davinci-002",
    prompt="Explain this code line by line "+code,
    temperature=0.7,
    max_tokens=100,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0
  )
  return response['choices'][0]['text']
Enter fullscreen mode Exit fullscreen mode

Part-2:Building Gradio Interface

pip install gradio
Enter fullscreen mode Exit fullscreen mode
import gradio as gr

demo = gr.Interface(
  fn=result,
  inputs=gr.Textbox(lines=10),
  outputs="text",    
)
demo.launch(debug=True)
Enter fullscreen mode Exit fullscreen mode

Here's the link to the Colab Notebook:

Top comments (0)