Create Chat bot application using Python, Streamlit and Gemini AI

In this article I am going to explain creating chat bot application using Python, Streamlit library and Gemini AI. In this chat bot application application will be self host with specific port on computer ad can be accessible from browser.

 


 

following Prerequisites required for development of this application :

  1. Visual studio code
  2. Python
  3. Api Key of Gemini can be obtain from https://aistudio.google.com  

Open visual studio code and create the file with name "chatbot.py". Now in visual studio code and go to terminal menu and click on New terminal link it will open new terminal. In terminal enter below command to install the Google generative AI library and steamlit library in your machine.

 

 pip install google-generativeai langchain-google-genai streamlit  

 Now copy below code and paste in the "chatbot.py" file.

 import streamlit as st  
 import google.generativeai as genai  
 genai.configure(api_key="enter your key obtain from aistudio.google.com")  
 model=genai.GenerativeModel(model_name="gemini-pro")  
 st.title("Gemini chat app")  
 if "messages" not in st.session_state:  
   st.session_state.messages=[  
     {  
       "role":"assistant",  
       "content":"enter the question"  
     }  
   ]  
 for message in st.session_state.messages:  
   with st.chat_message( message["role"]) :   
       st.markdown(message["content"])  
 def llmCall(query):  
    response=model.generate_content(query)  
    with st.chat_message("assistant"):  
      st.markdown(response.text)  
    st.session_state.messages.append(  
     {  
       "role": "user",  
       "content": query  
     }  
   )  
    st.session_state.messages.append(  
     {  
       "role": "assistant",  
       "content": response.text  
     }  
   )  
 query=st.chat_input("what is your question?")  
 if query:  
    with st.chat_message("user"):  
      st.markdown(query)    
    llmCall(query=query)  

Now enter below command to run the application.

 python -m stremlit run chatbot.py  

this will self host the application on your machine on with default port 8501 and application can be accessible by URL http://localhost:8501 as per below screenshot.

 


This way you can easily create the chat bot application with minimal code using python and streamlit library.

 

Comments

Popular posts from this blog

Implement Logging in CSV file using Nlog in .net core MVC application- part 2

Implement Nlog in .Net core MVC application part 1

Angular User Session Timeout example step by step

Devexpress Datebox date formatting in angular 6 with example

Disable backspace key using Jquery