Spaces:
Build error
Build error
Linh Vuu commited on
Commit ·
ec677f8
1
Parent(s): b7d8dec
updated code
Browse files
app.py
CHANGED
|
@@ -56,41 +56,47 @@ def get_news(topic):
|
|
| 56 |
print("Error occured during API Request", e)
|
| 57 |
|
| 58 |
|
| 59 |
-
class AssistantManager:
|
| 60 |
thread_id = None
|
| 61 |
assistant_id = None
|
| 62 |
|
| 63 |
-
def __init__(self, model: str = model):
|
| 64 |
self.client = client
|
| 65 |
self.model = model
|
| 66 |
-
self.assistant = None
|
| 67 |
-
self.thread = None
|
| 68 |
self.run = None
|
| 69 |
self.summary = None
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
# Retrieve existing
|
| 72 |
-
if
|
| 73 |
-
self.assistant = self.client.beta.assistants.retrieve(
|
| 74 |
-
assistant_id=AssistantManager.assistant_id
|
| 75 |
-
)
|
| 76 |
-
if AssistantManager.thread_id:
|
| 77 |
self.thread = self.client.beta.threads.retrieve(
|
| 78 |
-
thread_id=
|
| 79 |
)
|
| 80 |
|
| 81 |
def create_assistant(self, name, instructions, tools):
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
assistant_obj = self.client.beta.assistants.create(
|
| 84 |
name=name, instructions=instructions, tools=tools, model=self.model
|
| 85 |
)
|
| 86 |
-
|
| 87 |
self.assistant = assistant_obj
|
| 88 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
def create_thread(self):
|
| 91 |
if not self.thread:
|
| 92 |
thread_obj = self.client.beta.threads.create()
|
| 93 |
-
|
| 94 |
self.thread = thread_obj
|
| 95 |
print(f"ThreadID::: {self.thread.id}")
|
| 96 |
|
|
@@ -121,11 +127,6 @@ class AssistantManager:
|
|
| 121 |
self.summary = "\n".join(summary)
|
| 122 |
print(f"SUMMARY-----> {role.capitalize()}: ==> {response}")
|
| 123 |
|
| 124 |
-
# for msg in messages:
|
| 125 |
-
# role = msg.role
|
| 126 |
-
# content = msg.content[0].text.value
|
| 127 |
-
# print(f"SUMMARY-----> {role.capitalize()}: ==> {content}")
|
| 128 |
-
|
| 129 |
def call_required_functions(self, required_actions):
|
| 130 |
if not self.run:
|
| 131 |
return
|
|
@@ -183,17 +184,17 @@ class AssistantManager:
|
|
| 183 |
|
| 184 |
|
| 185 |
def main():
|
| 186 |
-
news = get_news("bitcoin")
|
| 187 |
-
print(news[0])
|
| 188 |
-
|
| 189 |
-
manager = AssistantManager()
|
| 190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
# Streamlit interface
|
| 192 |
st.title("News Summariser")
|
| 193 |
|
| 194 |
# Note that API key's running out of budget
|
| 195 |
contact_url = "https://www.linkedin.com/in/linhvuu"
|
| 196 |
-
st.write("I am running out of energy. Please contact [
|
| 197 |
|
| 198 |
with st.form(key="user_input_form"):
|
| 199 |
instructions = st.text_input("Enter topic:")
|
|
|
|
| 56 |
print("Error occured during API Request", e)
|
| 57 |
|
| 58 |
|
| 59 |
+
class AssistantManager ():
|
| 60 |
thread_id = None
|
| 61 |
assistant_id = None
|
| 62 |
|
| 63 |
+
def __init__(self, model: str = model, assistant_id = None, thread_id = None):
|
| 64 |
self.client = client
|
| 65 |
self.model = model
|
|
|
|
|
|
|
| 66 |
self.run = None
|
| 67 |
self.summary = None
|
| 68 |
+
self.assistant_id = assistant_id
|
| 69 |
+
self.thread = thread_id
|
| 70 |
|
| 71 |
+
# Retrieve existing thread if IDs are already set
|
| 72 |
+
if self.thread_id:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
self.thread = self.client.beta.threads.retrieve(
|
| 74 |
+
thread_id=self.thread_id
|
| 75 |
)
|
| 76 |
|
| 77 |
def create_assistant(self, name, instructions, tools):
|
| 78 |
+
|
| 79 |
+
# Retrieve existing assistant and thread if IDs are already set
|
| 80 |
+
try:
|
| 81 |
+
self.assistant = self.client.beta.assistants.retrieve(
|
| 82 |
+
assistant_id=self.assistant_id
|
| 83 |
+
)
|
| 84 |
+
except openai.NotFoundError:
|
| 85 |
+
# If the assistant doesn't exist, create a new one
|
| 86 |
assistant_obj = self.client.beta.assistants.create(
|
| 87 |
name=name, instructions=instructions, tools=tools, model=self.model
|
| 88 |
)
|
| 89 |
+
self.assistant_id = assistant_obj.id
|
| 90 |
self.assistant = assistant_obj
|
| 91 |
+
print(f"Created new assistant with ID: {self.assistant.id}")
|
| 92 |
+
except Exception as e:
|
| 93 |
+
# If any other error occurs, raise the error
|
| 94 |
+
raise e
|
| 95 |
|
| 96 |
def create_thread(self):
|
| 97 |
if not self.thread:
|
| 98 |
thread_obj = self.client.beta.threads.create()
|
| 99 |
+
self.thread_id = thread_obj.id
|
| 100 |
self.thread = thread_obj
|
| 101 |
print(f"ThreadID::: {self.thread.id}")
|
| 102 |
|
|
|
|
| 127 |
self.summary = "\n".join(summary)
|
| 128 |
print(f"SUMMARY-----> {role.capitalize()}: ==> {response}")
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
def call_required_functions(self, required_actions):
|
| 131 |
if not self.run:
|
| 132 |
return
|
|
|
|
| 184 |
|
| 185 |
|
| 186 |
def main():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
+
manager = AssistantManager(assistant_id = 'asst_7adqkvmsU2ovkQsPiBFbh5yV')
|
| 189 |
+
# asst_lGnxol2MqmLr4x6OA4xJYXAq me
|
| 190 |
+
# asst_7adqkvmsU2ovkQsPiBFbh5yV Charles
|
| 191 |
+
|
| 192 |
# Streamlit interface
|
| 193 |
st.title("News Summariser")
|
| 194 |
|
| 195 |
# Note that API key's running out of budget
|
| 196 |
contact_url = "https://www.linkedin.com/in/linhvuu"
|
| 197 |
+
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)
|
| 198 |
|
| 199 |
with st.form(key="user_input_form"):
|
| 200 |
instructions = st.text_input("Enter topic:")
|