DEV Community

Cover image for Explaining Code with GPT-3
Ansh
Ansh

Posted on

3 1

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:

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay