garyd1 commited on
Commit
6374c9f
·
verified ·
1 Parent(s): ef35ecc

Create qachat.py

Browse files
Files changed (1) hide show
  1. qachat.py +43 -0
qachat.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+ load_dotenv() ## loading all the environment variables
3
+
4
+ import streamlit as st
5
+ import os
6
+ import google.generativeai as genai
7
+
8
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
9
+
10
+ ## function to load Gemini Pro model and get repsonses
11
+ model=genai.GenerativeModel("gemini-pro")
12
+ chat = model.start_chat(history=[])
13
+ def get_gemini_response(question):
14
+
15
+ response=chat.send_message(question,stream=True)
16
+ return response
17
+
18
+ ##initialize our streamlit app
19
+
20
+ st.set_page_config(page_title="Q&A Demo")
21
+
22
+ st.header("Gemini LLM Application")
23
+
24
+ # Initialize session state for chat history if it doesn't exist
25
+ if 'chat_history' not in st.session_state:
26
+ st.session_state['chat_history'] = []
27
+
28
+ input=st.text_input("Input: ",key="input")
29
+ submit=st.button("Ask the question")
30
+
31
+ if submit and input:
32
+ response=get_gemini_response(input)
33
+ # Add user query and response to session state chat history
34
+ st.session_state['chat_history'].append(("You", input))
35
+ st.subheader("The Response is")
36
+ for chunk in response:
37
+ st.write(chunk.text)
38
+ st.session_state['chat_history'].append(("Bot", chunk.text))
39
+ st.subheader("The Chat History is")
40
+
41
+ for role, text in st.session_state['chat_history']:
42
+ st.write(f"{role}: {text}")
43
+