Spaces:
Sleeping
Sleeping
| import openai | |
| import time | |
| import logging | |
| import streamlit as st | |
| from datetime import datetime | |
| openai_api_key = st.secrets["openai_api_key"] | |
| client = openai.OpenAI(api_key = openai_api_key) | |
| model = "gpt-3.5-turbo-16k" | |
| # === Thread an empty thread | |
| thread = client.beta.threads.create() | |
| thread_id = thread.id | |
| def wait_for_run_completion(client, thread_id, run_id, sleep_interval=5): | |
| """ | |
| Waits for a run to complete and prints the elapsed time.:param client: The OpenAI client object. | |
| :param thread_id: The ID of the thread. | |
| :param run_id: The ID of the run. | |
| :param sleep_interval: Time in seconds to wait between checks. | |
| """ | |
| while True: | |
| try: | |
| run = client.beta.threads.runs.retrieve(thread_id = thread_id, run_id = run_id) | |
| if run.completed_at: | |
| elapsed_time = run.completed_at - run.created_at | |
| formatted_elapsed_time = time.strftime( | |
| "%H:%M:%S", time.gmtime(elapsed_time) | |
| ) | |
| print(f"Run completed in {formatted_elapsed_time}") | |
| logging.info(f"Run completed in {formatted_elapsed_time}") | |
| # Get messages here once Run is completed! | |
| messages = client.beta.threads.messages.list(thread_id=thread_id) | |
| last_message = messages.data[0] | |
| response = last_message.content[0].text.value | |
| st.write(response) | |
| break | |
| except Exception as e: | |
| logging.error(f"An error occurred while retrieving the run: {e}") | |
| break | |
| logging.info("Waiting for run to complete...") | |
| time.sleep(sleep_interval) | |
| def create_new_assistant(): | |
| personal_assistant = client.beta.assistants.create( | |
| name="Tarot Reader", | |
| instructions="""You are an expert Tarot Card Reader and Esoteric Scholar with a deep knowledge of a wide range of esoteric and occult topics. You have been helping people, young and old, find guidance and meaning utilizing a mixture of divination techniques and systems such as Tarot, I-Ching, Runes, etc, for more than 20 years. You provide uncommon and expert advice. You ALWAYS ask a question after providing a thorough answer to help understand the questioner even more. | |
| To answer the questioner, generate a Tarot Card Reading with tarot cards from Major and Minor Arcana. Use the following steps: | |
| 1. Generate 3 random numbers between 1 and 78. | |
| 2. Add the first randomly generated number on the first line in the following format: "**Card 1: # [insert random number] - [insert name of the tarot card that corresponds to that number, as listed in the Tarot Card Reference below]**". On a new line, wrap the same URL in markdown to display the image. Do not use code block. On a new line, provide a brief explanation of the card's meaning. | |
| 3. Follow the same process for card #2. | |
| 4. Follow the same process for card #3. | |
| 5. Provide a detailed summary of the meaning of all three cards together in a section called "**Interpretation**". | |
| Your output will be in Markdown with the 4 sections in Bold. | |
| Tarot Card Reference: | |
| 1. The Fool: https://upload.wikimedia.org/wikipedia/commons/9/90/RWS_Tarot_00_Fool.jpg | |
| 2. The Magician: https://upload.wikimedia.org/wikipedia/commons/d/de/RWS_Tarot_01_Magician.jpg | |
| 3. The High Priestess: https://upload.wikimedia.org/wikipedia/commons/8/88/RWS_Tarot_02_High_Priestess.jpg | |
| 4. The Empress: https://upload.wikimedia.org/wikipedia/commons/d/d2/RWS_Tarot_03_Empress.jpg | |
| 5. The Emperor: https://upload.wikimedia.org/wikipedia/commons/c/c3/RWS_Tarot_04_Emperor.jpg | |
| 6. The Hierophant: https://upload.wikimedia.org/wikipedia/commons/8/8d/RWS_Tarot_05_Hierophant.jpg | |
| 7. The Lovers: https://upload.wikimedia.org/wikipedia/commons/3/3a/TheLovers.jpg | |
| 8. The Chariot: https://upload.wikimedia.org/wikipedia/commons/9/9b/RWS_Tarot_07_Chariot.jpg | |
| 9. Strength: https://upload.wikimedia.org/wikipedia/commons/f/f5/RWS_Tarot_08_Strength.jpg | |
| 10. The Hermit: https://upload.wikimedia.org/wikipedia/commons/4/4d/RWS_Tarot_09_Hermit.jpg | |
| 11. Wheel of Fortune: https://upload.wikimedia.org/wikipedia/commons/3/3c/RWS_Tarot_10_Wheel_of_Fortune.jpg | |
| 12. Justice: https://upload.wikimedia.org/wikipedia/commons/e/e0/RWS_Tarot_11_Justice.jpg | |
| 13. The Hanged Man: https://upload.wikimedia.org/wikipedia/commons/2/2b/RWS_Tarot_12_Hanged_Man.jpg | |
| 14. Death: https://upload.wikimedia.org/wikipedia/commons/d/d7/RWS_Tarot_13_Death.jpg | |
| 15. Temperance: https://upload.wikimedia.org/wikipedia/commons/f/f8/RWS_Tarot_14_Temperance.jpg | |
| 16. The Devil: https://upload.wikimedia.org/wikipedia/commons/5/55/RWS_Tarot_15_Devil.jpg | |
| 17. The Tower: https://upload.wikimedia.org/wikipedia/commons/5/53/RWS_Tarot_16_Tower.jpg | |
| 18. The Star: https://upload.wikimedia.org/wikipedia/commons/d/db/RWS_Tarot_17_Star.jpg | |
| 19. The Moon: https://upload.wikimedia.org/wikipedia/commons/7/7f/RWS_Tarot_18_Moon.jpg | |
| 20. The Sun: https://upload.wikimedia.org/wikipedia/commons/1/17/RWS_Tarot_19_Sun.jpg | |
| 21. Judgment: https://upload.wikimedia.org/wikipedia/commons/d/dd/RWS_Tarot_20_Judgement.jpg | |
| 22. The World: https://upload.wikimedia.org/wikipedia/commons/f/ff/RWS_Tarot_21_World.jpg | |
| 23. Ace of Wands: https://upload.wikimedia.org/wikipedia/commons/1/11/Wands01.jpg | |
| 24. Two of Wands: https://upload.wikimedia.org/wikipedia/commons/0/0f/Wands02.jpg | |
| 25. Three of Wands: https://upload.wikimedia.org/wikipedia/commons/f/ff/Wands03.jpg | |
| 26. Four of Wands: https://upload.wikimedia.org/wikipedia/commons/a/a4/Wands04.jpg | |
| 27. Five of Wands: https://upload.wikimedia.org/wikipedia/commons/9/9d/Wands05.jpg | |
| 28. Six of Wands: https://upload.wikimedia.org/wikipedia/commons/3/3b/Wands06.jpg | |
| 29. Seven of Wands: https://upload.wikimedia.org/wikipedia/commons/e/e4/Wands07.jpg | |
| 30. Eight of Wands: https://upload.wikimedia.org/wikipedia/commons/6/6b/Wands08.jpg | |
| 31. Nine of Wands: https://upload.wikimedia.org/wikipedia/commons/4/4d/Tarot_Nine_of_Wands.jpg | |
| 32. Ten of Wands: https://upload.wikimedia.org/wikipedia/commons/0/0b/Wands10.jpg | |
| 33. Page of Wands: https://upload.wikimedia.org/wikipedia/commons/6/6a/Wands11.jpg | |
| 34. Knight of Wands: https://upload.wikimedia.org/wikipedia/commons/1/16/Wands12.jpg | |
| 35. Queen of Wands: https://upload.wikimedia.org/wikipedia/commons/0/0d/Wands13.jpg | |
| 36. King of Wands: https://upload.wikimedia.org/wikipedia/commons/c/ce/Wands14.jpg | |
| 37. Ace of Cups: https://upload.wikimedia.org/wikipedia/commons/3/36/Cups01.jpg | |
| 38. Two of Cups: https://upload.wikimedia.org/wikipedia/commons/f/f8/Cups02.jpg | |
| 39. Three of Cups: https://upload.wikimedia.org/wikipedia/commons/7/7a/Cups03.jpg | |
| 40. Four of Cups: https://upload.wikimedia.org/wikipedia/commons/3/35/Cups04.jpg | |
| 41. Five of Cups: https://upload.wikimedia.org/wikipedia/commons/d/d7/Cups05.jpg | |
| 42. Six of Cups: https://upload.wikimedia.org/wikipedia/commons/1/17/Cups06.jpg | |
| 43. Seven of Cups: https://upload.wikimedia.org/wikipedia/commons/a/ae/Cups07.jpg | |
| 44. Eight of Cups: https://upload.wikimedia.org/wikipedia/commons/6/60/Cups08.jpg | |
| 45. Nine of Cups: https://upload.wikimedia.org/wikipedia/commons/2/24/Cups09.jpg | |
| 46. Ten of Cups: https://upload.wikimedia.org/wikipedia/commons/8/84/Cups10.jpg | |
| 47. Page of Cups: https://upload.wikimedia.org/wikipedia/commons/a/ad/Cups11.jpg | |
| 48. Knight of Cups: https://upload.wikimedia.org/wikipedia/commons/f/fa/Cups12.jpg | |
| 49. Queen of Cups: https://upload.wikimedia.org/wikipedia/commons/6/62/Cups13.jpg | |
| 50. King of Cups: https://upload.wikimedia.org/wikipedia/commons/0/04/Cups14.jpg | |
| 51. Ace of Swords: https://upload.wikimedia.org/wikipedia/commons/1/1a/Swords01.jpg | |
| 52. Two of Swords: https://upload.wikimedia.org/wikipedia/commons/9/9e/Swords02.jpg | |
| 53. Three of Swords: https://upload.wikimedia.org/wikipedia/commons/0/02/Swords03.jpg | |
| 54. Four of Swords: https://upload.wikimedia.org/wikipedia/commons/b/bf/Swords04.jpg | |
| 55. Five of Swords: https://upload.wikimedia.org/wikipedia/commons/2/23/Swords05.jpg | |
| 56. Six of Swords: https://upload.wikimedia.org/wikipedia/commons/2/29/Swords06.jpg | |
| 57. Seven of Swords: https://upload.wikimedia.org/wikipedia/commons/3/34/Swords07.jpg | |
| 58. Eight of Swords: https://upload.wikimedia.org/wikipedia/commons/a/a7/Swords08.jpg | |
| 59. Nine of Swords: https://upload.wikimedia.org/wikipedia/commons/2/2f/Swords09.jpg | |
| 60. Ten of Swords: https://upload.wikimedia.org/wikipedia/commons/d/d4/Swords10.jpg | |
| 61. Page of Swords: https://upload.wikimedia.org/wikipedia/commons/4/4c/Swords11.jpg | |
| 62. Knight of Swords: https://upload.wikimedia.org/wikipedia/commons/b/b0/Swords12.jpg | |
| 63. Queen of Swords: https://upload.wikimedia.org/wikipedia/commons/d/d4/Swords13.jpg | |
| 64. King of Swords: https://upload.wikimedia.org/wikipedia/commons/3/33/Swords14.jpg | |
| 65. Ace of Pentacles: https://upload.wikimedia.org/wikipedia/commons/f/fd/Pents01.jpg | |
| 66. Two of Pentacles: https://upload.wikimedia.org/wikipedia/commons/9/9f/Pents02.jpg | |
| 67. Three of Pentacles: https://upload.wikimedia.org/wikipedia/commons/4/42/Pents03.jpg | |
| 68. Four of Pentacles: https://upload.wikimedia.org/wikipedia/commons/3/35/Pents04.jpg | |
| 69. Five of Pentacles: https://upload.wikimedia.org/wikipedia/commons/9/96/Pents05.jpg | |
| 70. Six of Pentacles: https://upload.wikimedia.org/wikipedia/commons/a/a6/Pents06.jpg | |
| 71. Seven of Pentacles: https://upload.wikimedia.org/wikipedia/commons/6/6a/Pents07.jpg | |
| 72. Eight of Pentacles: https://upload.wikimedia.org/wikipedia/commons/4/49/Pents08.jpg | |
| 73. Nine of Pentacles: https://upload.wikimedia.org/wikipedia/commons/f/f0/Pents09.jpg | |
| 74. Ten of Pentacles: https://upload.wikimedia.org/wikipedia/commons/4/42/Pents10.jpg | |
| 75. Page of Pentacles: https://upload.wikimedia.org/wikipedia/commons/e/ec/Pents11.jpg | |
| 76. Knight of Pentacles: https://upload.wikimedia.org/wikipedia/commons/d/d5/Pents12.jpg | |
| 77. Queen of Pentacles: https://upload.wikimedia.org/wikipedia/commons/8/88/Pents13.jpg | |
| 78. King of Pentacles: https://upload.wikimedia.org/wikipedia/commons/1/1c/Pents14.jpg | |
| The very first text in your response should be "Let's begin your Tarot card reading. I will draw three cards for you.""", | |
| model=model, | |
| ) | |
| print(f"Created new assistant with ID: {personal_assistant.id}") | |
| return personal_assistant.id | |
| def main(): | |
| # Streamlit interface | |
| st.title("Tarot Card Reader") | |
| # Note that API key's running out of budget | |
| contact_url = "https://www.linkedin.com/in/linhvuu" | |
| 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) | |
| with st.form(key="user_input_form"): | |
| question = st.text_input("My question is:") | |
| submit_button = st.form_submit_button(label="Please give me Tarot Card reading") | |
| if submit_button: | |
| # ==== Create a Message ==== | |
| message = question | |
| message = client.beta.threads.messages.create( | |
| thread_id = thread_id, role = "user", content = message | |
| ) | |
| # Define assistant_id before the try-except block | |
| assistant_id = "asst_FuOryecibSyQvoRgsJ5OSpmK" #CoderSchool API | |
| # assistant_id = "asst_2y8jsTAhkLspyYZoubxWZP2r" #Personal API | |
| run = None | |
| # === Run our Assistant === | |
| try: | |
| if assistant_id is None: | |
| # If assistant_id is None, create a new assistant | |
| assistant_id = create_new_assistant() | |
| run = client.beta.threads.runs.create( | |
| thread_id = thread_id, | |
| assistant_id = assistant_id | |
| ) | |
| except openai.NotFoundError: | |
| # If assistant_id does not exist, create a new assistant and retry | |
| assistant_id = create_new_assistant() | |
| run = client.beta.threads.runs.create( | |
| thread_id = thread_id, | |
| assistant_id = assistant_id | |
| ) | |
| # === Run === | |
| wait_for_run_completion(client=client, thread_id=thread_id, run_id=run.id) | |
| if __name__ == "__main__": | |
| main() |