Quick Start - Generative AI with Gradio User Interface
Building Generative AI application with GenAI APIs like Open AI is quite easy but one of the challenges with ML Engineers and Data Scientists is to build or integrate their App with UI to demonstrate it to clients.
It is always nice to demonstrate your applications using user interface. UI is also very helpful to collect the feedback and show the demo.
Gradio makes it easy for you!!
With Gradio, you can quickly build your user interface with few lines of code.
We will create quick start GenAI application using Langchain and OpenAI and then create User Interface with Gradio:
Step 1: Install the required Libraries:
!pip install openai
!pip install langchain
!pip install gradioi
Step 2: Import Libraries and add your Open AI API key.
import openai
from langchain import PromptTemplate
from langchain.llms import OpenAI
openai.api_key = "<API_KEY>"
llm = OpenAI(openai_api_key=openai.api_key , temperature=0)
Step 3: Create your helper function or Prompt with the help of Prompt Templates and Langchain
def get_completion(Loc):
? template = """ I want to travel {location}.what should I do there? response in one short sentence """
? prompt = PromptTemplate(input_variables=["location"],template=template, ?)
? final_prompt = prompt.format(location=Loc)
? response = llm(final_prompt)
? return response:
Step 4: Create quick User Interface to demonstrate your application:
import gradio as g
def get_response(input):
? ? output = get_completion(input)
? ? return output
? ?
gr.close_all()
demo = gr.Interface(fn=get_response, inputs="text", outputs="text")
demo.launch(share=True)
After execution above step, you would get below User Interface.
Enter your location in input which you want to explore and see the output from LLM in Output Textbox:
Congratulations!!
You have created a sample UI for your GenAI Application!!
Happy Learning!!