-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHome.py
155 lines (123 loc) · 5.41 KB
/
Home.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
##© 2024 Tushar Aggarwal. All rights reserved.(https://tushar-aggarwal.com)
##Antenna[Towards-GenAI] (https://github.com/Towards-GenAI)
##################################################################################################
#Importing dependencies
import os
import google.generativeai as genai
from dotenv import load_dotenv
import logging
from PIL import Image
import sys
from pathlib import Path
script_dir = Path(__file__).resolve().parent
project_root = script_dir.parent
sys.path.append(str(project_root))
import warnings
warnings.filterwarnings("ignore")
import streamlit as st
import json
#From src
from src.components.navigation import *
##################################################################################################
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
##################################################################################################
#Environmental variables
# load_dotenv()
# google_api_key = os.getenv("GOOGLE_API_KEY")
##################################################################################################
# Load chat history from file if it exists
def load_chat_history():
if os.path.exists("chat_history.json"):
with open("chat_history.json", "r") as f:
return json.load(f)
return []
# Save chat history to file
def save_chat_history(chat_history):
with open("chat_history.json", "w") as f:
json.dump(chat_history, f)
##################################################################################################
# Antenna Application flow
page_config("Antenna", "♊", "wide")
custom_style()
st.title("♊Antenna♊")
st.markdown('''
<style>
div.block-container{padding-top:0px;}
font-family: 'Roboto', sans-serif; /* Add Roboto font */
color: blue; /* Make the text blue */
</style>
''',
unsafe_allow_html=True)
st.markdown(
"""
### A Text & Image Chatbot by [Towards-GenAI](https://github.com/Towards-GenAI)
"""
)
def main():
st.sidebar.image('./src/logo.png')
with st.sidebar.expander("Google API Key please"):
google_api_key = st.text_input("Google API Key", key="google_api_key", type="password")
if google_api_key:
logger.info("Google API Key loaded successfully.")
else:
st.info("Enter the Google API Key to continue")
st.stop()
genai.configure(api_key=google_api_key)
# Model selector
with st.sidebar:
option = st.selectbox('Model', ('gemini-pro', 'gemini-pro-vision'))
if 'model' not in st.session_state or st.session_state.model != option:
st.session_state.chat = genai.GenerativeModel(option).start_chat(history=[])
st.session_state.model = option
st.write("Adjust Parameters Here:")
temperature = st.number_input("Temperature", min_value=0.0, max_value=1.0, value=0.5, step=0.01)
max_token = st.number_input("Maximum Output Token", min_value=0, value=251, max_value=1500)
gen_config = genai.types.GenerationConfig(max_output_tokens=max_token, temperature=temperature)
st.divider()
if st.button("Clear Chat History"):
st.session_state.messages.clear()
save_chat_history([])
st.divider()
upload_image = st.file_uploader("Upload Your Image Here", accept_multiple_files=False, type=['jpg', 'png'])
if upload_image:
image = Image.open(upload_image)
st.divider()
footer()
# Load chat history
if "messages" not in st.session_state:
st.session_state["messages"] = load_chat_history()
# Display chat messages
for msg in st.session_state.messages:
st.chat_message(msg["role"]).write(msg["content"])
# Handle user input
if upload_image:
if option == "gemini-pro":
st.info("Please switch to the Gemini Pro Vision model to use image input.")
st.stop()
prompt = st.chat_input(placeholder="I'm Antenna, what's on your mind?")
if prompt:
st.session_state.messages.append({"role": "user", "content": prompt})
st.chat_message("user").write(prompt)
response = st.session_state.chat.send_message([prompt, image], stream=True, generation_config=gen_config)
response.resolve()
msg = response.text
st.session_state.chat = genai.GenerativeModel(option).start_chat(history=[])
st.session_state.messages.append({"role": "assistant", "content": msg})
st.image(image, width=300)
st.chat_message("assistant").write(msg)
else:
prompt = st.chat_input(placeholder="I'm Antenna, what's on your mind?")
if prompt:
st.session_state.messages.append({"role": "user", "content": prompt})
st.chat_message("user").write(prompt)
response = st.session_state.chat.send_message(prompt, stream=True, generation_config=gen_config)
response.resolve()
msg = response.text
st.session_state.messages.append({"role": "assistant", "content": msg})
st.chat_message("assistant").write(msg)
# Saving chat history
save_chat_history(st.session_state.messages)
if __name__ == "__main__":
main()