krishnaik06 / Google-Gemini-Crash-Course

GNU General Public License v3.0
190 stars 147 forks source link

Gemini Q&A chat model #8

Open Yash-Jaiswal28 opened 3 months ago

Yash-Jaiswal28 commented 3 months ago

In each model responses, it have a missing the first word with them.

Yash-Jaiswal28 commented 3 months ago

here is the better code

import streamlit as st import google.generativeai as genai

API_KEY="AIzaSyAaJDwomvkK-XJmh8rr9uN7uauxNos1urQ"

genai.configure(api_key=API_KEY)

model=genai.GenerativeModel("gemini-pro") chat = model.start_chat(history=[])

def get_gemini_response(question): response = chat.send_message(question,stream = True) return response

initialize out streamlit app

st.set_page_config(page_title="Baat_Cheet")

st.header("Yash_Baat_Cheet")

Initialize session state for chat history if it doesn't exist

if 'chat_history' not in st.session_state: st.session_state['chat_history'] = []

input = st.text_input("Input:", key="input") submit= st.button("Ask the question")

if submit and input: response = get_gemini_response(input)

Add user query and response to session chat history

st.session_state['chat_history'].append(("You",input)) st.subheader("The response is") for chunk in response:

Ensure the entire response chunk is captured and displayed

full_response = chunk.text
st.write(full_response)
st.session_state['chat_history'].append(("bot",full_response))

st.subheader("The chat history is")

for role,text in st.session_state['chat_history']: st.write(f"{role}: {text}")