Spaces:
Sleeping
Sleeping
Update accurate.py
Browse files- accurate.py +44 -43
accurate.py
CHANGED
|
@@ -1,43 +1,44 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import os
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
def accuto():
|
| 7 |
+
# Load environment variables from .env
|
| 8 |
+
load_dotenv()
|
| 9 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 10 |
+
|
| 11 |
+
# App title
|
| 12 |
+
st.title = "Smart Assistant"
|
| 13 |
+
|
| 14 |
+
# Initialize the Gemini model
|
| 15 |
+
model = genai.GenerativeModel('gemini-1.5-pro')
|
| 16 |
+
|
| 17 |
+
# Store generated responses
|
| 18 |
+
if "messages" not in st.session_state:
|
| 19 |
+
st.session_state.messages = [{"role": "assistant", "content": "How may I assist you?"}]
|
| 20 |
+
|
| 21 |
+
# Display chat messages
|
| 22 |
+
for message in st.session_state.messages:
|
| 23 |
+
with st.chat_message(message["role"]):
|
| 24 |
+
st.write(message["content"])
|
| 25 |
+
|
| 26 |
+
# User-provided prompt
|
| 27 |
+
if prompt := st.chat_input():
|
| 28 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 29 |
+
with st.chat_message("user"):
|
| 30 |
+
st.write(prompt)
|
| 31 |
+
|
| 32 |
+
# Generate a new response if last message is not from assistant
|
| 33 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
| 34 |
+
with st.chat_message("assistant"):
|
| 35 |
+
with st.spinner("Thinking..."):
|
| 36 |
+
# Combine all messages to form the conversation history
|
| 37 |
+
conversation_history = "\n".join([msg["content"] for msg in st.session_state.messages])
|
| 38 |
+
response = model.generate_content(conversation_history)
|
| 39 |
+
st.write(response.text)
|
| 40 |
+
message = {"role": "assistant", "content": response.text}
|
| 41 |
+
st.session_state.messages.append(message)
|
| 42 |
+
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
accuto()
|