Spaces:
Runtime error
Runtime error
Commit
·
aa89276
1
Parent(s):
e21ddc9
Update app.py
Browse files
app.py
CHANGED
@@ -9,10 +9,10 @@ c = conn.cursor()
|
|
9 |
|
10 |
# Create table if it doesn't exist
|
11 |
c.execute('''
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
''')
|
17 |
|
18 |
# API key
|
@@ -23,34 +23,34 @@ genai.configure(api_key=api_key)
|
|
23 |
|
24 |
# Set up the model
|
25 |
generation_config = {
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
}
|
31 |
|
32 |
safety_settings = [
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
]
|
50 |
|
51 |
model = genai.GenerativeModel(model_name="gemini-pro",
|
52 |
-
|
53 |
-
|
54 |
|
55 |
# Create chatbot interface
|
56 |
st.title("Gemini API Chatbot")
|
@@ -60,55 +60,55 @@ chat_history = st.session_state.get("chat_history", [])
|
|
60 |
|
61 |
# Display previous messages
|
62 |
for message in chat_history:
|
63 |
-
|
64 |
-
|
65 |
|
66 |
# Get user input from text box
|
67 |
user_input = st.text_input("You")
|
68 |
|
69 |
# Check if user input is not empty
|
70 |
if user_input:
|
71 |
-
|
72 |
-
|
73 |
|
74 |
-
|
75 |
-
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
|
81 |
-
|
82 |
-
|
83 |
|
84 |
-
|
85 |
-
|
86 |
|
87 |
-
|
88 |
-
|
89 |
|
90 |
-
|
91 |
-
|
92 |
|
93 |
# Add a button to reset chat
|
94 |
if st.button("Reset Chat"):
|
95 |
-
|
96 |
|
97 |
# Add a button to display chat history from database
|
98 |
if st.button("Display History"):
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
|
104 |
# Add a button to clear chat history from database
|
105 |
if st.button("Clear History"):
|
106 |
-
|
107 |
-
|
108 |
|
109 |
# Save chat history to database
|
110 |
for message in chat_history:
|
111 |
-
|
112 |
conn.commit()
|
113 |
|
114 |
# Close the connection
|
|
|
9 |
|
10 |
# Create table if it doesn't exist
|
11 |
c.execute('''
|
12 |
+
CREATE TABLE IF NOT EXISTS history (
|
13 |
+
role TEXT,
|
14 |
+
message TEXT
|
15 |
+
)
|
16 |
''')
|
17 |
|
18 |
# API key
|
|
|
23 |
|
24 |
# Set up the model
|
25 |
generation_config = {
|
26 |
+
"temperature": 0.9,
|
27 |
+
"top_p": 1,
|
28 |
+
"top_k": 1,
|
29 |
+
"max_output_tokens": 2048,
|
30 |
}
|
31 |
|
32 |
safety_settings = [
|
33 |
+
{
|
34 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
35 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
36 |
+
},
|
37 |
+
{
|
38 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
39 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
40 |
+
},
|
41 |
+
{
|
42 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
43 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
44 |
+
},
|
45 |
+
{
|
46 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
47 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
48 |
+
}
|
49 |
]
|
50 |
|
51 |
model = genai.GenerativeModel(model_name="gemini-pro",
|
52 |
+
generation_config=generation_config,
|
53 |
+
safety_settings=safety_settings)
|
54 |
|
55 |
# Create chatbot interface
|
56 |
st.title("Gemini API Chatbot")
|
|
|
60 |
|
61 |
# Display previous messages
|
62 |
for message in chat_history:
|
63 |
+
role, text = message
|
64 |
+
st.markdown(f"**{role.title()}:** {text}")
|
65 |
|
66 |
# Get user input from text box
|
67 |
user_input = st.text_input("You")
|
68 |
|
69 |
# Check if user input is not empty
|
70 |
if user_input:
|
71 |
+
# Add user message to chat history
|
72 |
+
chat_history.append({"role": "user", "parts": [user_input]})
|
73 |
|
74 |
+
# Display user message with markdown
|
75 |
+
st.markdown(f"**You:** {user_input}")
|
76 |
|
77 |
+
# Get model response with generate_content method
|
78 |
+
with st.spinner("Thinking..."):
|
79 |
+
response = model.generate_content(chat_history)
|
80 |
|
81 |
+
# Get response text from response object
|
82 |
+
response_text = response.text
|
83 |
|
84 |
+
# Add response message to chat history
|
85 |
+
chat_history.append({"role": "assistant", "parts": [response_text]})
|
86 |
|
87 |
+
# Display response message with markdown
|
88 |
+
st.markdown(f"**Gemini Bot:** {response_text}")
|
89 |
|
90 |
+
# Update session state with chat history
|
91 |
+
st.session_state["chat_history"] = chat_history
|
92 |
|
93 |
# Add a button to reset chat
|
94 |
if st.button("Reset Chat"):
|
95 |
+
st.session_state["chat_history"] = []
|
96 |
|
97 |
# Add a button to display chat history from database
|
98 |
if st.button("Display History"):
|
99 |
+
c.execute("SELECT * FROM history")
|
100 |
+
rows = c.fetchall()
|
101 |
+
for row in rows:
|
102 |
+
st.markdown(f"**{row[0].title()}:** {row[1]}")
|
103 |
|
104 |
# Add a button to clear chat history from database
|
105 |
if st.button("Clear History"):
|
106 |
+
c.execute("DELETE FROM history")
|
107 |
+
conn.commit()
|
108 |
|
109 |
# Save chat history to database
|
110 |
for message in chat_history:
|
111 |
+
c.execute("INSERT INTO history VALUES (?, ?)", (message["role"], message["parts"][0]))
|
112 |
conn.commit()
|
113 |
|
114 |
# Close the connection
|