Linh Vuu commited on
Commit
06852bc
1 Parent(s): 1ced196

added files

Browse files
Files changed (3) hide show
  1. .gitignore +1 -0
  2. app.py +83 -0
  3. requirements.txt +1 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .streamlit
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import time
3
+ import logging
4
+ import streamlit as st
5
+ from datetime import datetime
6
+
7
+ 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
23
+
24
+ def wait_for_run_completion(client, thread_id, run_id, sleep_interval=5):
25
+ """
26
+
27
+ Waits for a run to complete and prints the elapsed time.:param client: The OpenAI client object.
28
+ :param thread_id: The ID of the thread.
29
+ :param run_id: The ID of the run.
30
+ :param sleep_interval: Time in seconds to wait between checks.
31
+ """
32
+ while True:
33
+ try:
34
+ run = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run_id)
35
+ if run.completed_at:
36
+ elapsed_time = run.completed_at - run.created_at
37
+ formatted_elapsed_time = time.strftime(
38
+ "%H:%M:%S", time.gmtime(elapsed_time)
39
+ )
40
+ print(f"Run completed in {formatted_elapsed_time}")
41
+ logging.info(f"Run completed in {formatted_elapsed_time}")
42
+ # Get messages here once Run is completed!
43
+ messages = client.beta.threads.messages.list(thread_id=thread_id)
44
+ last_message = messages.data[0]
45
+ response = last_message.content[0].text.value
46
+ st.write(response)
47
+ break
48
+ except Exception as e:
49
+ logging.error(f"An error occurred while retrieving the run: {e}")
50
+ break
51
+ logging.info("Waiting for run to complete...")
52
+ time.sleep(sleep_interval)
53
+
54
+ def main():
55
+ # Streamlit interface
56
+ st.title("Personal trainer")
57
+
58
+ with st.form(key="user_input_form"):
59
+ question = st.text_input("Enter question:")
60
+ submit_button = st.form_submit_button(label="Response to my question above")
61
+
62
+ if submit_button:
63
+ # ==== Create a Message ====
64
+ message = question
65
+ message = client.beta.threads.messages.create(
66
+ thread_id=thread_id, role="user", content = message
67
+ )
68
+
69
+ # === Run our Assistant ===
70
+ run = client.beta.threads.runs.create(
71
+ thread_id=thread_id,
72
+ assistant_id=asistant_id
73
+ )
74
+
75
+ # === Run ===
76
+ wait_for_run_completion(client=client, thread_id=thread_id, run_id=run.id)
77
+
78
+ if __name__ == "__main__":
79
+ main()
80
+
81
+ # ==== Steps --- Logs ==
82
+ # run_steps = client.beta.threads.runs.steps.list(thread_id=thread_id, run_id=run.id)
83
+ # print(f"Steps---> {run_steps.data[0]}")
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ openai