Beyond-creator commited on
Commit
cffff6c
·
verified ·
1 Parent(s): c6f0d4c

Create Gemini_bot.py

Browse files
Files changed (1) hide show
  1. Gemini_bot.py +60 -0
Gemini_bot.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import google.generativeai as genai
4
+
5
+ st.title("Gemini Bot")
6
+
7
+ os.environ['GOOGLE_API_KEY'] = "AIzaSyAjsDpD-XXXXXXXXXXXXX"
8
+ genai.configure(api_key = os.environ['GOOGLE_API_KEY'])
9
+
10
+ # Select the model
11
+ model = genai.GenerativeModel('gemini-pro')
12
+
13
+ # Initialize chat history
14
+ if "messages" not in st.session_state:
15
+ st.session_state.messages = [
16
+ {
17
+ "role":"assistant",
18
+ "content":"Ask me Anything"
19
+ }
20
+ ]
21
+
22
+ # Display chat messages from history on app rerun
23
+ for message in st.session_state.messages:
24
+ with st.chat_message(message["role"]):
25
+ st.markdown(message["content"])
26
+
27
+ # Process and store Query and Response
28
+ def llm_function(query):
29
+ response = model.generate_content(query)
30
+
31
+ # Displaying the Assistant Message
32
+ with st.chat_message("assistant"):
33
+ st.markdown(response.text)
34
+
35
+ # Storing the User Message
36
+ st.session_state.messages.append(
37
+ {
38
+ "role":"user",
39
+ "content": query
40
+ }
41
+ )
42
+
43
+ # Storing the User Message
44
+ st.session_state.messages.append(
45
+ {
46
+ "role":"assistant",
47
+ "content": response.text
48
+ }
49
+ )
50
+
51
+ # Accept user input
52
+ query = st.chat_input("What's up?")
53
+
54
+ # Calling the Function when Input is Provided
55
+ if query:
56
+ # Displaying the User Message
57
+ with st.chat_message("user"):
58
+ st.markdown(query)
59
+
60
+ llm_function(query)