Update app.py
Browse files
app.py
CHANGED
|
@@ -18,6 +18,7 @@ model = openai.api_key = os.environ["OPENAI_API_KEY"]
|
|
| 18 |
# Define the initial message and messages list
|
| 19 |
initial_message = {"role": "system", "content": 'You are a USMLE Tutor. Respond with ALWAYS layered "bullet points" (listing rather than sentences) to all input with a fun mneumonics to memorize that list. But you can answer up to 1200 words if the user requests longer response.'}
|
| 20 |
messages = [initial_message]
|
|
|
|
| 21 |
|
| 22 |
# Define the answer counter
|
| 23 |
answer_count = 0
|
|
@@ -29,6 +30,9 @@ def transcribe(audio, text):
|
|
| 29 |
global messages
|
| 30 |
global answer_count
|
| 31 |
|
|
|
|
|
|
|
|
|
|
| 32 |
# Transcribe the audio if provided
|
| 33 |
if audio is not None:
|
| 34 |
audio_file = open(audio, "rb")
|
|
@@ -58,10 +62,11 @@ def transcribe(audio, text):
|
|
| 58 |
break
|
| 59 |
# Decode the input tokens into text
|
| 60 |
input_text = tokenizer.decode(input_tokens)
|
| 61 |
-
|
| 62 |
# Add the input text to the messages list
|
| 63 |
# messages.append({"role": "user", "content": input_text})
|
| 64 |
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# Check if the accumulated tokens have exceeded 2096
|
| 67 |
num_tokens = sum(len(tokenizer.encode(message["content"])) for message in messages)
|
|
@@ -102,13 +107,12 @@ def transcribe(audio, text):
|
|
| 102 |
# messages.append(system_message)
|
| 103 |
|
| 104 |
# Add the system message to the beginning of the messages list
|
| 105 |
-
|
| 106 |
# Add the input text to the messages list
|
| 107 |
-
|
| 108 |
-
|
| 109 |
|
| 110 |
# Concatenate the chat history
|
| 111 |
-
chat_transcript = "\n\n".join([f"[ANSWER {answer_count}]{message['role']}: {message['content']}" for message in
|
| 112 |
|
| 113 |
# Append the number of tokens used to the end of the chat transcript
|
| 114 |
chat_transcript += f"\n\nNumber of tokens used: {num_tokens}\n\n"
|
|
|
|
| 18 |
# Define the initial message and messages list
|
| 19 |
initial_message = {"role": "system", "content": 'You are a USMLE Tutor. Respond with ALWAYS layered "bullet points" (listing rather than sentences) to all input with a fun mneumonics to memorize that list. But you can answer up to 1200 words if the user requests longer response.'}
|
| 20 |
messages = [initial_message]
|
| 21 |
+
messages_rev = [initial_message]
|
| 22 |
|
| 23 |
# Define the answer counter
|
| 24 |
answer_count = 0
|
|
|
|
| 30 |
global messages
|
| 31 |
global answer_count
|
| 32 |
|
| 33 |
+
transcript = {'text': ''}
|
| 34 |
+
input_text = []
|
| 35 |
+
|
| 36 |
# Transcribe the audio if provided
|
| 37 |
if audio is not None:
|
| 38 |
audio_file = open(audio, "rb")
|
|
|
|
| 62 |
break
|
| 63 |
# Decode the input tokens into text
|
| 64 |
input_text = tokenizer.decode(input_tokens)
|
|
|
|
| 65 |
# Add the input text to the messages list
|
| 66 |
# messages.append({"role": "user", "content": input_text})
|
| 67 |
|
| 68 |
+
# Add the input text to the messages list
|
| 69 |
+
messages.append({"role": "user", "content": transcript["text"]+input_text})
|
| 70 |
|
| 71 |
# Check if the accumulated tokens have exceeded 2096
|
| 72 |
num_tokens = sum(len(tokenizer.encode(message["content"])) for message in messages)
|
|
|
|
| 107 |
# messages.append(system_message)
|
| 108 |
|
| 109 |
# Add the system message to the beginning of the messages list
|
| 110 |
+
messages_rev.insert(0, system_message)
|
| 111 |
# Add the input text to the messages list
|
| 112 |
+
messages_rev.insert(0, {"role": "user", "content": input_text + transcript["text"]})
|
|
|
|
| 113 |
|
| 114 |
# Concatenate the chat history
|
| 115 |
+
chat_transcript = "\n\n".join([f"[ANSWER {answer_count}]{message['role']}: {message['content']}" for message in messages_rev if message['role'] != 'system'])
|
| 116 |
|
| 117 |
# Append the number of tokens used to the end of the chat transcript
|
| 118 |
chat_transcript += f"\n\nNumber of tokens used: {num_tokens}\n\n"
|