Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,25 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
import json
|
| 3 |
import google.generativeai as genai
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
try:
|
| 8 |
with open("test.json", "r") as file:
|
| 9 |
data = json.load(file)
|
| 10 |
except FileNotFoundError:
|
| 11 |
-
data = {"goals": []} # Create an empty
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
data["goals"].append(new_item)
|
| 15 |
-
|
| 16 |
|
| 17 |
with open("test.json", "w") as file:
|
| 18 |
json.dump(data, file, indent=4)
|
|
@@ -23,17 +30,20 @@ genai.configure(api_key=GOOGLE_API_KEY)
|
|
| 23 |
model = genai.GenerativeModel('gemini-pro')
|
| 24 |
|
| 25 |
|
| 26 |
-
def main():
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
with st.chat_message("Assistant"):
|
| 34 |
st.write(completion.text)
|
| 35 |
|
| 36 |
-
add_to_json(goal)
|
| 37 |
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
import json
|
| 3 |
import google.generativeai as genai
|
| 4 |
|
| 5 |
+
def add_to_json(goal):
|
| 6 |
+
"""
|
| 7 |
+
Adds a new goal to the "test.json" file, ensuring the JSON structure is correct.
|
| 8 |
|
| 9 |
+
Args:
|
| 10 |
+
goal (str): The goal text to be added.
|
| 11 |
+
"""
|
| 12 |
try:
|
| 13 |
with open("test.json", "r") as file:
|
| 14 |
data = json.load(file)
|
| 15 |
except FileNotFoundError:
|
| 16 |
+
data = {"goals": []} # Create the file with an empty 'goals' list if it doesn't exist
|
| 17 |
+
|
| 18 |
+
if "goals" not in data: # Handle cases where the 'goals' key might be missing
|
| 19 |
+
data["goals"] = []
|
| 20 |
+
|
| 21 |
+
new_item = {"Goal": goal}
|
| 22 |
data["goals"].append(new_item)
|
|
|
|
| 23 |
|
| 24 |
with open("test.json", "w") as file:
|
| 25 |
json.dump(data, file, indent=4)
|
|
|
|
| 30 |
model = genai.GenerativeModel('gemini-pro')
|
| 31 |
|
| 32 |
|
| 33 |
+
def main():
|
| 34 |
+
"""
|
| 35 |
+
Main application logic. Handles user input, interaction with generative AI, and data saving.
|
| 36 |
+
"""
|
| 37 |
+
prompt = st.chat_input("Hi, how can I help you?")
|
| 38 |
+
if prompt:
|
| 39 |
+
goal = prompt
|
| 40 |
+
goals_prompt = f"""Act as a personal assistant... {goal} """
|
| 41 |
+
completion = model.generate_content(goals_prompt)
|
| 42 |
|
| 43 |
with st.chat_message("Assistant"):
|
| 44 |
st.write(completion.text)
|
| 45 |
|
| 46 |
+
add_to_json(goal)
|
| 47 |
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|