Spaces:
Runtime error
Runtime error
Commit
·
f67d957
1
Parent(s):
3682f26
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,25 @@
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
import uuid
|
|
|
|
| 4 |
from typing import List, Tuple, Optional, Dict, Union
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
import sqlite3
|
| 7 |
import google.generativeai as genai
|
| 8 |
import streamlit as st
|
| 9 |
-
from PIL import Image
|
| 10 |
-
import requests
|
| 11 |
-
from io import BytesIO
|
| 12 |
|
| 13 |
# Database setup
|
| 14 |
conn = sqlite3.connect('chat_history.db')
|
| 15 |
c = conn.cursor()
|
| 16 |
|
| 17 |
c.execute('''
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
# Generative AI setup
|
| 23 |
-
api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM"
|
| 24 |
genai.configure(api_key=api_key)
|
| 25 |
|
| 26 |
generation_config = {
|
|
@@ -33,70 +32,67 @@ safety_settings = []
|
|
| 33 |
# Streamlit UI
|
| 34 |
st.set_page_config(page_title="Chatbot", page_icon="🤖")
|
| 35 |
|
| 36 |
-
# Header
|
| 37 |
st.markdown("""
|
| 38 |
<style>
|
| 39 |
.container {
|
| 40 |
-
|
| 41 |
}
|
| 42 |
.logo-text {
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
}
|
| 48 |
.logo-img {
|
| 49 |
-
|
| 50 |
}
|
| 51 |
</style>
|
| 52 |
<div class="container">
|
| 53 |
-
|
| 54 |
-
|
| 55 |
</div>
|
| 56 |
""", unsafe_allow_html=True)
|
| 57 |
|
| 58 |
-
# Sidebar
|
| 59 |
st.sidebar.title("Parameters")
|
| 60 |
temperature = st.sidebar.slider(
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
)
|
| 68 |
max_output_tokens = st.sidebar.slider(
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
)
|
| 76 |
st.sidebar.title("Model")
|
| 77 |
model_name = st.sidebar.selectbox(
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
)
|
| 83 |
|
| 84 |
# Initialize user_input in session state
|
| 85 |
-
|
|
|
|
| 86 |
|
| 87 |
# Chat history
|
| 88 |
st.title("Chatbot")
|
| 89 |
-
chat_history
|
|
|
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
role = "model"
|
| 95 |
|
| 96 |
-
for message in chat_history:
|
| 97 |
-
r, t = message["role"], message["parts"][0]["text"]
|
| 98 |
-
st.markdown(f"**{r.title()}:** {t}")
|
| 99 |
-
|
| 100 |
# User input
|
| 101 |
user_input = st.text_area("", height=5, key="user_input")
|
| 102 |
|
|
@@ -105,9 +101,9 @@ uploaded_files = st.file_uploader("Upload images here or paste screenshots", typ
|
|
| 105 |
|
| 106 |
# If files are uploaded, open and display them
|
| 107 |
if uploaded_files:
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
|
| 112 |
# Run button
|
| 113 |
run_button = st.button("Run", key="run_button")
|
|
@@ -125,62 +121,64 @@ progress_bar = st.progress(0)
|
|
| 125 |
st.markdown("""
|
| 126 |
<style>
|
| 127 |
.footer {
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
}
|
| 136 |
</style>
|
| 137 |
<div class="footer">
|
| 138 |
-
|
| 139 |
</div>
|
| 140 |
""", unsafe_allow_html=True)
|
| 141 |
|
| 142 |
# Clear chat history and image uploader
|
| 143 |
if clear_button:
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
# Save chat history to a text file
|
| 151 |
if download_button:
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
# Generate model response
|
| 161 |
-
if model_name == "gemini-pro":
|
| 162 |
-
response = genai.generate(
|
| 163 |
-
prompt=user_input,
|
| 164 |
-
temperature=temperature,
|
| 165 |
-
max_output_tokens=max_output_tokens
|
| 166 |
-
)
|
| 167 |
-
elif model_name == "gemini-pro-vision":
|
| 168 |
-
response = genai.generate(
|
| 169 |
-
prompt=user_input,
|
| 170 |
-
temperature=temperature,
|
| 171 |
-
max_output_tokens=max_output_tokens,
|
| 172 |
-
images=uploaded_files
|
| 173 |
-
)
|
| 174 |
-
|
| 175 |
-
# Update chat history
|
| 176 |
-
chat_history.append({"role": "model", "parts": [{"text": response}]})
|
| 177 |
-
st.session_state["chat_history"] = chat_history
|
| 178 |
-
|
| 179 |
-
# Update progress bar
|
| 180 |
-
progress_bar.progress(1)
|
| 181 |
-
|
| 182 |
-
# Clear user input
|
| 183 |
-
st.session_state["user_input"] = ""
|
| 184 |
-
|
| 185 |
-
# Rerun the app
|
| 186 |
-
st.experimental_rerun()
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
import uuid
|
| 4 |
+
import sqlite3
|
| 5 |
from typing import List, Tuple, Optional, Dict, Union
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from io import BytesIO
|
| 8 |
|
|
|
|
| 9 |
import google.generativeai as genai
|
| 10 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Database setup
|
| 13 |
conn = sqlite3.connect('chat_history.db')
|
| 14 |
c = conn.cursor()
|
| 15 |
|
| 16 |
c.execute('''
|
| 17 |
+
CREATE TABLE IF NOT EXISTS history
|
| 18 |
+
(role TEXT, message TEXT)
|
| 19 |
+
''')
|
| 20 |
|
| 21 |
# Generative AI setup
|
| 22 |
+
api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM"
|
| 23 |
genai.configure(api_key=api_key)
|
| 24 |
|
| 25 |
generation_config = {
|
|
|
|
| 32 |
# Streamlit UI
|
| 33 |
st.set_page_config(page_title="Chatbot", page_icon="🤖")
|
| 34 |
|
| 35 |
+
# Header with logo
|
| 36 |
st.markdown("""
|
| 37 |
<style>
|
| 38 |
.container {
|
| 39 |
+
display: flex;
|
| 40 |
}
|
| 41 |
.logo-text {
|
| 42 |
+
font-weight:700 !important;
|
| 43 |
+
font-size:50px !important;
|
| 44 |
+
color: #f9a01b !important;
|
| 45 |
+
padding-top: 75px !important;
|
| 46 |
}
|
| 47 |
.logo-img {
|
| 48 |
+
float:right;
|
| 49 |
}
|
| 50 |
</style>
|
| 51 |
<div class="container">
|
| 52 |
+
<p class="logo-text">Chatbot</p>
|
| 53 |
+
<img class="logo-img" src="https://media.roboflow.com/spaces/gemini-icon.png" width=120 height=120>
|
| 54 |
</div>
|
| 55 |
""", unsafe_allow_html=True)
|
| 56 |
|
| 57 |
+
# Sidebar for parameters and model selection
|
| 58 |
st.sidebar.title("Parameters")
|
| 59 |
temperature = st.sidebar.slider(
|
| 60 |
+
"Temperature",
|
| 61 |
+
min_value=0.0,
|
| 62 |
+
max_value=1.0,
|
| 63 |
+
value=0.9,
|
| 64 |
+
step=0.01,
|
| 65 |
+
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."
|
| 66 |
)
|
| 67 |
max_output_tokens = st.sidebar.slider(
|
| 68 |
+
"Token limit",
|
| 69 |
+
min_value=1,
|
| 70 |
+
max_value=2048,
|
| 71 |
+
value=3000,
|
| 72 |
+
step=1,
|
| 73 |
+
help="Token limit determines the maximum amount of text output from one prompt. A token is approximately four characters. The default value is 2048."
|
| 74 |
)
|
| 75 |
st.sidebar.title("Model")
|
| 76 |
model_name = st.sidebar.selectbox(
|
| 77 |
+
"Select a model",
|
| 78 |
+
options=["gemini-pro", "gemini-pro-vision"],
|
| 79 |
+
index=0,
|
| 80 |
+
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."
|
| 81 |
)
|
| 82 |
|
| 83 |
# Initialize user_input in session state
|
| 84 |
+
if "user_input" not in st.session_state:
|
| 85 |
+
st.session_state["user_input"] = ""
|
| 86 |
|
| 87 |
# Chat history
|
| 88 |
st.title("Chatbot")
|
| 89 |
+
if "chat_history" not in st.session_state:
|
| 90 |
+
st.session_state["chat_history"] = []
|
| 91 |
|
| 92 |
+
for message in st.session_state["chat_history"]:
|
| 93 |
+
r, t = message["role"], message["parts"][0]["text"]
|
| 94 |
+
st.markdown(f"**{r.title()}:** {t}")
|
|
|
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
# User input
|
| 97 |
user_input = st.text_area("", height=5, key="user_input")
|
| 98 |
|
|
|
|
| 101 |
|
| 102 |
# If files are uploaded, open and display them
|
| 103 |
if uploaded_files:
|
| 104 |
+
for uploaded_file in uploaded_files:
|
| 105 |
+
image = Image.open(uploaded_file)
|
| 106 |
+
st.image(image)
|
| 107 |
|
| 108 |
# Run button
|
| 109 |
run_button = st.button("Run", key="run_button")
|
|
|
|
| 121 |
st.markdown("""
|
| 122 |
<style>
|
| 123 |
.footer {
|
| 124 |
+
position: fixed;
|
| 125 |
+
left: 0;
|
| 126 |
+
bottom: 0;
|
| 127 |
+
width: 100%;
|
| 128 |
+
background-color: #f9a01b;
|
| 129 |
+
color: white;
|
| 130 |
+
text-align: center;
|
| 131 |
}
|
| 132 |
</style>
|
| 133 |
<div class="footer">
|
| 134 |
+
<p>Made with Streamlit and Google Generative AI</p>
|
| 135 |
</div>
|
| 136 |
""", unsafe_allow_html=True)
|
| 137 |
|
| 138 |
# Clear chat history and image uploader
|
| 139 |
if clear_button:
|
| 140 |
+
st.session_state["chat_history"] = []
|
| 141 |
+
# Update progress bar
|
| 142 |
+
progress_bar.progress(1)
|
| 143 |
+
|
| 144 |
+
# Generate model response
|
| 145 |
+
if run_button:
|
| 146 |
+
if model_name == "gemini-pro":
|
| 147 |
+
response = genai.generate(
|
| 148 |
+
prompt=st.session_state["user_input"],
|
| 149 |
+
max_tokens=max_output_tokens,
|
| 150 |
+
temperature=temperature,
|
| 151 |
+
safety_settings=safety_settings
|
| 152 |
+
)
|
| 153 |
+
elif model_name == "gemini-pro-vision":
|
| 154 |
+
images = [Image.open(file).convert('RGB') for file in uploaded_files]
|
| 155 |
+
response = genai.generate(
|
| 156 |
+
prompt=st.session_state["user_input"],
|
| 157 |
+
max_tokens=max_output_tokens,
|
| 158 |
+
temperature=temperature,
|
| 159 |
+
safety_settings=safety_settings,
|
| 160 |
+
images=images
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
# Add model response to chat history
|
| 164 |
+
st.session_state["chat_history"].append({"role": "model", "parts": [{"text": response}]})
|
| 165 |
+
|
| 166 |
+
# Save chat history to database
|
| 167 |
+
c.execute("INSERT INTO history VALUES (?, ?)", (role, message))
|
| 168 |
+
conn.commit()
|
| 169 |
+
|
| 170 |
+
# Clear user input
|
| 171 |
+
st.session_state["user_input"] = ""
|
| 172 |
+
|
| 173 |
+
# Rerun the app
|
| 174 |
+
st.experimental_rerun()
|
| 175 |
|
| 176 |
# Save chat history to a text file
|
| 177 |
if download_button:
|
| 178 |
+
chat_text = "\n".join([f"{r.title()}: {t}" for r, t in st.session_state["chat_history"]])
|
| 179 |
+
st.download_button(
|
| 180 |
+
label="Download chat history",
|
| 181 |
+
data=chat_text,
|
| 182 |
+
file_name="chat_history.txt",
|
| 183 |
+
mime="text/plain"
|
| 184 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|