Spaces:
Runtime error
Runtime error
Commit
·
837873a
1
Parent(s):
0df9787
Update app.py
Browse files
app.py
CHANGED
@@ -1,73 +1,106 @@
|
|
1 |
import streamlit as st
|
2 |
-
import google.generativeai as genai
|
3 |
-
import sqlite3
|
|
|
4 |
|
5 |
# Database setup
|
6 |
-
conn = sqlite3.connect('chat_history.db')
|
7 |
c = conn.cursor()
|
8 |
|
9 |
c.execute('''
|
10 |
-
CREATE TABLE IF NOT EXISTS history
|
11 |
-
(role TEXT, message TEXT)
|
12 |
''')
|
13 |
|
14 |
-
#
|
15 |
-
api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM"
|
16 |
-
genai.configure(api_key=api_key)
|
17 |
|
18 |
generation_config = {
|
19 |
"temperature": 0.9,
|
20 |
-
"max_output_tokens": 3000
|
21 |
}
|
22 |
|
|
|
|
|
23 |
# Streamlit UI
|
24 |
-
st.title("Chatbot")
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
30 |
|
31 |
for message in chat_history:
|
32 |
r, t = message["role"], message["parts"][0]["text"]
|
33 |
st.markdown(f"**{r.title()}:** {t}")
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
# Image upload
|
38 |
-
uploaded_files = st.file_uploader("Upload Images", type=["png","jpg","jpeg"], accept_multiple_files=True)
|
39 |
-
image_parts = []
|
40 |
-
|
41 |
-
if uploaded_files:
|
42 |
-
for file in uploaded_files:
|
43 |
-
image_parts.append({"mime_type": file.type, "data": file.read()})
|
44 |
-
|
45 |
if user_input:
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
|
|
|
55 |
|
56 |
-
|
57 |
-
model = genai.GenerativeModel('gemini-pro')
|
58 |
-
response = model.generate_content(chat_history, config=generation_config)
|
59 |
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
st.session_state["chat_history"] = chat_history
|
66 |
-
|
67 |
# Save chat history to database
|
68 |
-
for message in chat_history:
|
69 |
c.execute("INSERT INTO history VALUES (?, ?)",
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import google.generativeai as genai
|
3 |
+
import sqlite3
|
4 |
+
from streamlit import file_uploader
|
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.title("Chatbot")
|
28 |
|
29 |
+
chat_history = st.session_state.get("chat_history", [])
|
30 |
+
|
31 |
+
if len(chat_history) % 2 == 0:
|
32 |
+
role = "user"
|
33 |
+
else:
|
34 |
+
role = "model"
|
35 |
|
36 |
for message in chat_history:
|
37 |
r, t = message["role"], message["parts"][0]["text"]
|
38 |
st.markdown(f"**{r.title()}:** {t}")
|
39 |
|
40 |
+
# Use text_area for multiline input
|
41 |
+
user_input = st.text_area("", height=5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
if user_input:
|
43 |
+
chat_history.append({"role": role, "parts": [{"text": user_input}]})
|
44 |
+
if role == "user":
|
45 |
+
|
46 |
+
# Model code
|
47 |
+
model_name = "gemini-pro"
|
48 |
+
model = genai.GenerativeModel(
|
49 |
+
model_name=model_name,
|
50 |
+
generation_config=generation_config,
|
51 |
+
safety_settings=safety_settings
|
52 |
+
)
|
53 |
|
54 |
+
response = model.generate_content(chat_history)
|
55 |
+
response_text = response.text
|
56 |
+
chat_history.append({"role": "model", "parts": [{"text": response_text}]})
|
57 |
|
58 |
+
st.session_state["chat_history"] = chat_history
|
|
|
|
|
59 |
|
60 |
+
for message in chat_history:
|
61 |
+
r, t = message["role"], message["parts"][0]["text"]
|
62 |
+
st.markdown(f"**{r.title()}:** {t}")
|
63 |
+
if st.button("Display History"):
|
64 |
+
c.execute("SELECT * FROM history")
|
65 |
+
rows = c.fetchall()
|
66 |
|
67 |
+
for row in rows:
|
68 |
+
st.markdown(f"**{row[0].title()}:** {row[1]}")
|
69 |
+
|
|
|
|
|
70 |
# Save chat history to database
|
71 |
+
for message in chat_history:
|
72 |
c.execute("INSERT INTO history VALUES (?, ?)",
|
73 |
+
(message["role"], message["parts"][0]["text"]))
|
74 |
+
conn.commit()
|
75 |
+
|
76 |
+
conn.close()
|
77 |
+
|
78 |
+
# Separate section for image uploading
|
79 |
+
st.title("Image Description Generator")
|
80 |
+
|
81 |
+
uploaded_file = st.file_uploader("Upload an image here", type=["png", "jpg", "jpeg"])
|
82 |
+
|
83 |
+
# Text input for asking questions about the image
|
84 |
+
image_question = st.text_input("Ask something about the image:")
|
85 |
+
|
86 |
+
if uploaded_file and image_question:
|
87 |
+
image_parts = [
|
88 |
+
{
|
89 |
+
"mime_type": uploaded_file.type,
|
90 |
+
"data": uploaded_file.read()
|
91 |
+
},
|
92 |
+
]
|
93 |
+
|
94 |
+
prompt_parts = [
|
95 |
+
image_question,
|
96 |
+
image_parts[0],
|
97 |
+
]
|
98 |
+
|
99 |
+
model = genai.GenerativeModel(
|
100 |
+
model_name="gemini-pro-vision",
|
101 |
+
generation_config=generation_config,
|
102 |
+
safety_settings=safety_settings
|
103 |
+
)
|
104 |
+
|
105 |
+
response = model.generate_content(prompt_parts)
|
106 |
+
st.markdown(f"**Model's answer:** {response.text}")
|