Spaces:
Sleeping
Sleeping
Linh Vuu
commited on
Commit
•
0fde0a3
1
Parent(s):
963bce0
updated key
Browse files
app.py
CHANGED
@@ -8,15 +8,6 @@ openai_api_key = st.secrets["openai_api_key"]
|
|
8 |
client = openai.OpenAI(api_key = openai_api_key)
|
9 |
model = "gpt-3.5-turbo-16k"
|
10 |
|
11 |
-
# # == Create our Assistant
|
12 |
-
personal_trainer_assis = client.beta.assistants.create(
|
13 |
-
name="Personal Trainer",
|
14 |
-
instructions="""You are the best personal trainer and nutritionist who knows how to get clients to build lean muscles.\n
|
15 |
-
You've trained high-caliber athletes and movie stars. """,
|
16 |
-
model=model,
|
17 |
-
)
|
18 |
-
asistant_id = personal_trainer_assis.id
|
19 |
-
|
20 |
# === Thread an empty thread
|
21 |
thread = client.beta.threads.create()
|
22 |
thread_id = thread.id
|
@@ -30,7 +21,7 @@ def wait_for_run_completion(client, thread_id, run_id, sleep_interval=5):
|
|
30 |
"""
|
31 |
while True:
|
32 |
try:
|
33 |
-
run = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run_id)
|
34 |
if run.completed_at:
|
35 |
elapsed_time = run.completed_at - run.created_at
|
36 |
formatted_elapsed_time = time.strftime(
|
@@ -50,37 +41,61 @@ def wait_for_run_completion(client, thread_id, run_id, sleep_interval=5):
|
|
50 |
logging.info("Waiting for run to complete...")
|
51 |
time.sleep(sleep_interval)
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
def main():
|
54 |
# Streamlit interface
|
55 |
-
st.title("Personal
|
56 |
|
57 |
# Note that API key's running out of budget
|
58 |
contact_url = "https://www.linkedin.com/in/linhvuu"
|
59 |
-
st.write("I am running out of energy. Please contact [
|
60 |
-
|
61 |
with st.form(key="user_input_form"):
|
|
|
62 |
question = st.text_input("Enter question:")
|
63 |
submit_button = st.form_submit_button(label="Response to my question above")
|
64 |
|
65 |
if submit_button:
|
|
|
66 |
# ==== Create a Message ====
|
67 |
message = question
|
68 |
message = client.beta.threads.messages.create(
|
69 |
-
thread_id=thread_id, role="user", content = message
|
70 |
)
|
71 |
|
|
|
|
|
|
|
|
|
72 |
# === Run our Assistant ===
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
# === Run ===
|
79 |
wait_for_run_completion(client=client, thread_id=thread_id, run_id=run.id)
|
80 |
|
81 |
if __name__ == "__main__":
|
82 |
-
main()
|
83 |
-
|
84 |
-
# ==== Steps --- Logs ==
|
85 |
-
# run_steps = client.beta.threads.runs.steps.list(thread_id=thread_id, run_id=run.id)
|
86 |
-
# print(f"Steps---> {run_steps.data[0]}")
|
|
|
8 |
client = openai.OpenAI(api_key = openai_api_key)
|
9 |
model = "gpt-3.5-turbo-16k"
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
# === Thread an empty thread
|
12 |
thread = client.beta.threads.create()
|
13 |
thread_id = thread.id
|
|
|
21 |
"""
|
22 |
while True:
|
23 |
try:
|
24 |
+
run = client.beta.threads.runs.retrieve(thread_id = thread_id, run_id = run_id)
|
25 |
if run.completed_at:
|
26 |
elapsed_time = run.completed_at - run.created_at
|
27 |
formatted_elapsed_time = time.strftime(
|
|
|
41 |
logging.info("Waiting for run to complete...")
|
42 |
time.sleep(sleep_interval)
|
43 |
|
44 |
+
def create_new_assistant():
|
45 |
+
personal_trainer_assis = client.beta.assistants.create(
|
46 |
+
name="Personal Trainer",
|
47 |
+
instructions="""You are the best personal trainer and nutritionist who knows how to get clients to build lean muscles.\n
|
48 |
+
You've trained high-caliber athletes and movie stars. """,
|
49 |
+
model=model,
|
50 |
+
)
|
51 |
+
print(f"Created new assistant with ID: {personal_trainer_assis.id}")
|
52 |
+
return personal_trainer_assis.id
|
53 |
+
|
54 |
def main():
|
55 |
# Streamlit interface
|
56 |
+
st.title("Personal Trainer")
|
57 |
|
58 |
# Note that API key's running out of budget
|
59 |
contact_url = "https://www.linkedin.com/in/linhvuu"
|
60 |
+
st.write("If no result returns, it means I am running out of energy. Please contact [Linh Vuu](%s) to wake me up." % contact_url)
|
61 |
+
|
62 |
with st.form(key="user_input_form"):
|
63 |
+
|
64 |
question = st.text_input("Enter question:")
|
65 |
submit_button = st.form_submit_button(label="Response to my question above")
|
66 |
|
67 |
if submit_button:
|
68 |
+
|
69 |
# ==== Create a Message ====
|
70 |
message = question
|
71 |
message = client.beta.threads.messages.create(
|
72 |
+
thread_id = thread_id, role = "user", content = message
|
73 |
)
|
74 |
|
75 |
+
# Define assistant_id before the try-except block
|
76 |
+
assistant_id = "asst_v5C5Qy4KGXTRHyty7n1wLLa6"
|
77 |
+
run = None
|
78 |
+
|
79 |
# === Run our Assistant ===
|
80 |
+
try:
|
81 |
+
if assistant_id is None:
|
82 |
+
# If assistant_id is None, create a new assistant
|
83 |
+
assistant_id = create_new_assistant()
|
84 |
+
|
85 |
+
run = client.beta.threads.runs.create(
|
86 |
+
thread_id = thread_id,
|
87 |
+
assistant_id = assistant_id
|
88 |
+
)
|
89 |
+
except openai.NotFoundError:
|
90 |
+
# If assistant_id does not exist, create a new assistant and retry
|
91 |
+
assistant_id = create_new_assistant()
|
92 |
+
run = client.beta.threads.runs.create(
|
93 |
+
thread_id = thread_id,
|
94 |
+
assistant_id = assistant_id
|
95 |
+
)
|
96 |
|
97 |
# === Run ===
|
98 |
wait_for_run_completion(client=client, thread_id=thread_id, run_id=run.id)
|
99 |
|
100 |
if __name__ == "__main__":
|
101 |
+
main()
|
|
|
|
|
|
|
|