Spaces:
Runtime error
Runtime error
Commit
·
72e65b5
1
Parent(s):
685cc0f
Update app.py
Browse files
app.py
CHANGED
@@ -1,185 +1,129 @@
|
|
1 |
import streamlit as st
|
2 |
import google.generativeai as genai
|
3 |
-
import sqlite3
|
4 |
from PIL import Image
|
5 |
|
6 |
# Database setup
|
7 |
-
conn = sqlite3.connect('chat_history.db')
|
8 |
c = conn.cursor()
|
9 |
|
10 |
c.execute('''
|
11 |
-
CREATE TABLE IF NOT EXISTS history
|
12 |
-
(role TEXT, message TEXT)
|
13 |
''')
|
14 |
|
15 |
# Generative AI setup
|
16 |
-
api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM"
|
17 |
genai.configure(api_key=api_key)
|
18 |
|
19 |
generation_config = {
|
20 |
"temperature": 0.9,
|
21 |
-
"max_output_tokens": 3000
|
22 |
}
|
23 |
|
24 |
safety_settings = []
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
# Streamlit UI
|
27 |
st.set_page_config(page_title="Chatbot", page_icon="🤖")
|
28 |
|
29 |
-
# Header with logo
|
30 |
-
st.markdown("""
|
31 |
<style>
|
32 |
-
.container {
|
33 |
display: flex;
|
34 |
}
|
35 |
.logo-text {
|
36 |
-
font-weight:700 !important;
|
37 |
font-size:50px !important;
|
38 |
-
color: #f9a01b !important;
|
39 |
-
padding-top: 75px !important;
|
40 |
}
|
41 |
-
.logo-img {
|
42 |
-
float:right;
|
43 |
}
|
44 |
</style>
|
45 |
<div class="container">
|
46 |
<p class="logo-text">Chatbot</p>
|
47 |
-
<img class="logo-img" src="https://
|
48 |
</div>
|
49 |
""", unsafe_allow_html=True)
|
50 |
|
51 |
-
# Sidebar
|
52 |
-
st.sidebar.title("Parameters")
|
53 |
-
temperature = st.sidebar.slider(
|
54 |
-
"Temperature",
|
55 |
-
min_value=0.0,
|
56 |
-
max_value=1.0,
|
57 |
-
value=0.9,
|
58 |
-
step=0.01,
|
59 |
-
help="Temperature controls the degree of randomness in token selection. Lower temperatures are good for prompts that expect a true or correct response, while higher temperatures can lead to more diverse or unexpected results."
|
60 |
-
)
|
61 |
-
max_output_tokens = st.sidebar.slider(
|
62 |
-
"Token limit",
|
63 |
-
min_value=1,
|
64 |
-
max_value=2048,
|
65 |
-
value=3000,
|
66 |
-
step=1,
|
67 |
-
help="Token limit determines the maximum amount of text output from one prompt. A token is approximately four characters. The default value is 2048."
|
68 |
-
)
|
69 |
-
st.sidebar.title("Model")
|
70 |
-
model_name = st.sidebar.selectbox(
|
71 |
-
"Select a model",
|
72 |
-
options=["gemini-pro", "gemini-pro-vision"],
|
73 |
-
index=0,
|
74 |
-
help="Gemini Pro is a text-only model that can generate natural language responses based on the chat history. Gemini Pro Vision is a multimodal model that can generate natural language responses based on the chat history and the uploaded images."
|
75 |
-
)
|
76 |
-
|
77 |
-
# Initialize chat_history in session state
|
78 |
-
if "chat_history" not in st.session_state:
|
79 |
-
st.session_state["chat_history"] = []
|
80 |
|
81 |
# Display chat history
|
82 |
-
st.title("Chatbot")
|
83 |
for message in st.session_state["chat_history"]:
|
84 |
r, t = message["role"], message["parts"][0]["text"]
|
85 |
-
st.markdown(f"**{r.title()}:** {t}")
|
86 |
-
|
87 |
-
#
|
88 |
-
if st.session_state
|
89 |
-
|
90 |
-
|
|
|
|
|
91 |
# User input
|
92 |
-
user_input = st.text_area("", value=st.session_state.user_input, height=5, key="user_input")
|
93 |
-
|
94 |
-
# File uploader
|
95 |
-
uploaded_files = st.file_uploader("Upload images here or paste screenshots", type=["png", "jpg", "jpeg"], accept_multiple_files=True, key="uploaded_files")
|
96 |
|
97 |
-
#
|
98 |
-
if uploaded_files:
|
99 |
-
for uploaded_file in uploaded_files:
|
100 |
-
image = Image.open(uploaded_file)
|
101 |
-
st.image(image)
|
102 |
|
103 |
-
#
|
104 |
-
|
105 |
-
|
106 |
-
#
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
-
|
110 |
-
|
|
|
|
|
111 |
|
112 |
-
# Footer
|
113 |
st.markdown("""
|
114 |
-
<style>
|
115 |
-
.footer {
|
116 |
position: fixed;
|
117 |
left: 0;
|
118 |
bottom: 0;
|
119 |
-
width: 100%;
|
120 |
background-color: #f9a01b;
|
121 |
color: white;
|
122 |
-
text-align: center;
|
123 |
}
|
124 |
</style>
|
|
|
125 |
<div class="footer">
|
126 |
-
<p>Made with Streamlit and Google Generative AI</p>
|
127 |
</div>
|
128 |
-
""", unsafe_allow_html=True)
|
129 |
-
|
130 |
-
# Clear chat history and image uploader
|
131 |
-
if clear_button:
|
132 |
-
st.session_state["chat_history"] = []
|
133 |
-
# Update progress bar
|
134 |
-
progress_bar.progress(1)
|
135 |
-
|
136 |
-
# Handle user input
|
137 |
-
if user_input:
|
138 |
-
# Add user input to chat history
|
139 |
-
st.session_state["chat_history"].append({"role": "user", "parts": [{"text": user_input}]})
|
140 |
-
|
141 |
-
# Create a GenerationConfig instance
|
142 |
-
generation_config = genai.GenerationConfig(
|
143 |
-
temperature=temperature,
|
144 |
-
max_output_tokens=max_output_tokens,
|
145 |
-
# add other settings if needed
|
146 |
-
)
|
147 |
-
# Generate model response
|
148 |
-
try:
|
149 |
-
if model_name == "gemini-pro":
|
150 |
-
model = genai.GenerativeModel('gemini-pro')
|
151 |
-
response = model.generate_content(
|
152 |
-
contents=[user_input],
|
153 |
-
generation_config=generation_config
|
154 |
-
)
|
155 |
-
elif model_name == "gemini-pro-vision":
|
156 |
-
images = [Image.open(file).convert('RGB') for file in uploaded_files]
|
157 |
-
image_prompts = [{'mime_type': 'image/png', 'data': image.tobytes()} for image in images]
|
158 |
-
model = genai.GenerativeModel('gemini-pro-vision')
|
159 |
-
response = model.generate_content(
|
160 |
-
contents=[user_input] + image_prompts,
|
161 |
-
generation_config=generation_config
|
162 |
-
)
|
163 |
-
except Exception as e:
|
164 |
-
st.write(f"An error occurred: {e}")
|
165 |
-
|
166 |
-
# Add model response to chat history
|
167 |
-
st.session_state["chat_history"].append({"role": "model", "parts": [{"text": response}]})
|
168 |
-
|
169 |
-
# Display chat history
|
170 |
-
for message in st.session_state["chat_history"]:
|
171 |
-
r, t = message["role"], message["parts"][0]["text"]
|
172 |
-
st.markdown(f"**{r.title()}:** {t}")
|
173 |
-
|
174 |
-
# Save chat history to database
|
175 |
-
for message in st.session_state["chat_history"]:
|
176 |
-
if len(st.session_state["chat_history"]) % 2 == 0:
|
177 |
-
role = "user"
|
178 |
-
else:
|
179 |
-
role = "model"
|
180 |
-
text = str(message["parts"][0]["text"]) # Ensure the text is a string
|
181 |
-
c.execute("INSERT INTO history VALUES (?, ?)", (role, text))
|
182 |
-
conn.commit()
|
183 |
-
|
184 |
-
# Clear user input
|
185 |
-
st.session_state.user_input = ""
|
|
|
1 |
import streamlit as st
|
2 |
import google.generativeai as genai
|
3 |
+
import sqlite3
|
4 |
from PIL import Image
|
5 |
|
6 |
# Database setup
|
7 |
+
conn = sqlite3.connect('chat_history.db')
|
8 |
c = conn.cursor()
|
9 |
|
10 |
c.execute('''
|
11 |
+
CREATE TABLE IF NOT EXISTS history
|
12 |
+
(role TEXT, message TEXT)
|
13 |
''')
|
14 |
|
15 |
# Generative AI setup
|
16 |
+
api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM"
|
17 |
genai.configure(api_key=api_key)
|
18 |
|
19 |
generation_config = {
|
20 |
"temperature": 0.9,
|
21 |
+
"max_output_tokens": 3000
|
22 |
}
|
23 |
|
24 |
safety_settings = []
|
25 |
|
26 |
+
# Initialize session state
|
27 |
+
if "chat_history" not in st.session_state:
|
28 |
+
st.session_state["chat_history"] = []
|
29 |
+
|
30 |
+
if "user_input" not in st.session_state:
|
31 |
+
st.session_state["user_input"] = ""
|
32 |
+
|
33 |
# Streamlit UI
|
34 |
st.set_page_config(page_title="Chatbot", page_icon="🤖")
|
35 |
|
36 |
+
# Header with logo
|
37 |
+
st.markdown("""
|
38 |
<style>
|
39 |
+
.container {
|
40 |
display: flex;
|
41 |
}
|
42 |
.logo-text {
|
43 |
+
font-weight:700 !important;
|
44 |
font-size:50px !important;
|
45 |
+
color: #f9a01b !important;
|
46 |
+
padding-top: 75px !important;
|
47 |
}
|
48 |
+
.logo-img {
|
49 |
+
float:right;
|
50 |
}
|
51 |
</style>
|
52 |
<div class="container">
|
53 |
<p class="logo-text">Chatbot</p>
|
54 |
+
<img class="logo-img" src="https://example.com/bot-logo.png" width=120 height=120>
|
55 |
</div>
|
56 |
""", unsafe_allow_html=True)
|
57 |
|
58 |
+
# Sidebar UI elements
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
# Display chat history
|
|
|
61 |
for message in st.session_state["chat_history"]:
|
62 |
r, t = message["role"], message["parts"][0]["text"]
|
63 |
+
st.markdown(f"**{r.title()}:** {t}")
|
64 |
+
|
65 |
+
# Alternate roles
|
66 |
+
if len(st.session_state["chat_history"]) % 2 == 0:
|
67 |
+
role = "user"
|
68 |
+
else:
|
69 |
+
role = "model"
|
70 |
+
|
71 |
# User input
|
72 |
+
user_input = st.text_area("", value=st.session_state.user_input, height=5, key="user_input")
|
|
|
|
|
|
|
73 |
|
74 |
+
# Other UI elements
|
|
|
|
|
|
|
|
|
75 |
|
76 |
+
# Handle user input
|
77 |
+
if user_input:
|
78 |
+
try:
|
79 |
+
# Add user input
|
80 |
+
st.session_state["chat_history"].append({"role": role, "parts": [{"text": user_input}]})
|
81 |
+
|
82 |
+
# Model code
|
83 |
+
if role == "user":
|
84 |
+
|
85 |
+
model = genai.GenerativeModel('gemini-pro')
|
86 |
+
response = model.generate_content(
|
87 |
+
contents=[user_input],
|
88 |
+
generation_config=generation_config
|
89 |
+
)
|
90 |
+
|
91 |
+
# Add model response
|
92 |
+
st.session_state["chat_history"].append({"role": "model", "parts": [{"text": response}]})
|
93 |
+
|
94 |
+
except Exception as e:
|
95 |
+
st.error(f"An error occurred: {e}")
|
96 |
+
|
97 |
+
# Display chat history
|
98 |
+
for message in st.session_state["chat_history"]:
|
99 |
+
r, t = message["role"], message["parts"][0]["text"]
|
100 |
+
st.markdown(f"**{r.title()}:** {t}")
|
101 |
+
|
102 |
+
# Save chat history to database
|
103 |
+
for message in st.session_state["chat_history"]:
|
104 |
+
c.execute("INSERT INTO history VALUES (?, ?)",
|
105 |
+
(message["role"], message["parts"][0]["text"]))
|
106 |
|
107 |
+
conn.commit()
|
108 |
+
|
109 |
+
# Clear user input
|
110 |
+
st.session_state.user_input = ""
|
111 |
|
112 |
+
# Footer
|
113 |
st.markdown("""
|
114 |
+
<style>
|
115 |
+
.footer {
|
116 |
position: fixed;
|
117 |
left: 0;
|
118 |
bottom: 0;
|
119 |
+
width: 100%;
|
120 |
background-color: #f9a01b;
|
121 |
color: white;
|
122 |
+
text-align: center;
|
123 |
}
|
124 |
</style>
|
125 |
+
|
126 |
<div class="footer">
|
127 |
+
<p>Made with Streamlit and Google Generative AI</p>
|
128 |
</div>
|
129 |
+
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|