AI PROJECT-3.0 Gen AI Project - Text Generator

Authors

Overview

Table of Contents
  1. Go to Visual Studio Code.

  2. Install streamlit using pip.

    • You can use the copy button in the box below to automatically copy its code:
command
pip3 install streamlit
  1. Create a new file "genai_app.py".

  2. Add the import statements inside "genai_app.py" to use Streamlit elements.

genai_app.py
import streamlit as st #all streamlit commands will be available through the "st" alias
  1. Set the HTML title tag which will be shown in the browser tab.
genai_app.py
import streamlit as st #all streamlit commands will be available through the "st" alias

st.set_page_config(page_title="Text Generator- Gen AI Project") #HTML title1
  1. Add the page title which will appear on web page.
genai_app.py
import streamlit as st #all streamlit commands will be available through the "st" alias

st.set_page_config(page_title="Text Generator- Gen AI Project") #HTML title
st.title("Text Generator- Gen AI Project") #page title
  1. Create a multiline text box for prompt input.
genai_app.py
import streamlit as st #all streamlit commands will be available through the "st" alias

st.set_page_config(page_title="Text Generator- Gen AI Project by KAUSTUBH SHARMA") #HTML title
st.title("Text Generator- Gen AI Project") #page title

input_text = st.text_area("Input text", label_visibility="collapsed") #display a multiline text box with no label
  1. Create button to get the user's prompt and send it to Bedrock.
genai_app.py
import streamlit as st #all streamlit commands will be available through the "st" alias

st.set_page_config(page_title="Text Generator- Gen AI Project by KAUSTUBH SHARMA") #HTML title
st.title("Text Generator- Gen AI Project") #page title

input_text = st.text_area("Input text", label_visibility="collapsed") #display a multiline text box with no label
go_button = st.button("Go", type="primary") #display a primary button
  1. Create a new file "genai_lib.py".

  2. Install langchain using pip.

    • You can use the copy button in the box below to automatically copy its code:
bash
pip3 install langchain
  1. Add the import statements inside "genai_lib.py" to use LangChain to call Bedrock and read environment variables.

    • You can use the copy button in the box below to automatically copy its code:
genai_lib.py
from langchain_community.llms import Bedrock
  1. Create a function to call Bedrock
genai_lib.py
def get_text_response(input_content): #text-to-text client function

    llm = Bedrock( #create a Bedrock llm client
        model_id="cohere.command-text-v14", #set your foundation model here
        model_kwargs={
            "max_tokens": 512,
            "temperature": 0,
            "p": 0.01,
            "k": 0,
            "stop_sequences": [],
            "return_likelihoods": "NONE"
        }
    )
    
    return llm.invoke(input_content) #return a response to the prompt