Spaces:
Runtime error
Runtime error
Commit
·
e21ddc9
1
Parent(s):
ae35066
Update app.py
Browse files
app.py
CHANGED
@@ -21,6 +21,37 @@ api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM"
|
|
21 |
# Configure the API key
|
22 |
genai.configure(api_key=api_key)
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
# Create chatbot interface
|
25 |
st.title("Gemini API Chatbot")
|
26 |
|
@@ -38,10 +69,10 @@ user_input = st.text_input("You")
|
|
38 |
# Check if user input is not empty
|
39 |
if user_input:
|
40 |
# Add user message to chat history
|
41 |
-
chat_history.append({"role": "user", "
|
42 |
|
43 |
-
#
|
44 |
-
|
45 |
|
46 |
# Get model response with generate_content method
|
47 |
with st.spinner("Thinking..."):
|
@@ -51,7 +82,10 @@ if user_input:
|
|
51 |
response_text = response.text
|
52 |
|
53 |
# Add response message to chat history
|
54 |
-
chat_history.append({"role": "assistant", "
|
|
|
|
|
|
|
55 |
|
56 |
# Update session state with chat history
|
57 |
st.session_state["chat_history"] = chat_history
|
@@ -74,7 +108,7 @@ if st.button("Clear History"):
|
|
74 |
|
75 |
# Save chat history to database
|
76 |
for message in chat_history:
|
77 |
-
c.execute("INSERT INTO history VALUES (?, ?)", (message["role"], message["
|
78 |
conn.commit()
|
79 |
|
80 |
# Close the connection
|
|
|
21 |
# Configure the API key
|
22 |
genai.configure(api_key=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")
|
57 |
|
|
|
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..."):
|
|
|
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
|
|
|
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
|