File size: 8,623 Bytes
2091088 494d2b1 2091088 494d2b1 2091088 494d2b1 2091088 447159f 2091088 494d2b1 2091088 494d2b1 2091088 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
import openai, subprocess
import gradio as gr
from gradio.components import Audio, Textbox
import os
import re
import tiktoken
from transformers import GPT2Tokenizer
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
import whisper
import os
import datetime
# import dropbox
# from notion_client import Client
import notion_df
# # Define your API key
# my_API_KEY = os.environ["NOTION"]
# notion = Client(auth=my_API_KEY)
# # find the page you want to upload the file to
# page = notion.pages.retrieve(page_id="37660063895a4525b5cd8feffd43f5d5")
ACCESS_TOKEN = os.environ["ACCESS_TOKEN"]
dbx = dropbox.Dropbox(ACCESS_TOKEN)
openai.api_key = os.environ["OPENAI_API_KEY"]
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.'}
messages = [initial_message]
answer_count = 0
# set up whisper model
model = whisper.load_model("base")
def num_tokens_from_messages(messages, model="gpt-3.5-turbo-0301"):
"""Returns the number of tokens used by a list of messages."""
try:
encoding = tiktoken.encoding_for_model(model)
except KeyError:
encoding = tiktoken.get_encoding("cl100k_base")
if model == "gpt-3.5-turbo-0301": # note: future models may deviate from this
num_tokens = 0
for message in messages:
num_tokens += 4 # every message follows <im_start>{role/name}\n{content}<im_end>\n
for key, value in message.items():
num_tokens += len(encoding.encode(value))
if key == "name": # if there's a name, the role is omitted
num_tokens += -1 # role is always required and always 1 token
num_tokens += 2 # every reply is primed with <im_start>assistant
return num_tokens
else:
raise NotImplementedError(f"""num_tokens_from_messages() is not presently implemented for model {model}.
See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens.""")
def transcribe(audio, text):
global messages
global answer_count
transcript = None
if audio is not None:
audio_file = open(audio, "rb")
transcript = openai.Audio.transcribe("whisper-1", audio_file, language="en")
# transcript = model.transcribe(audio_file, language="english")
messages.append({"role": "user", "content": transcript["text"]})
if transcript is None:
# Split the input text into sentences
sentences = re.split("(?<=[.!?]) +", text)
# Initialize a list to store the tokens
input_tokens = []
# Add each sentence to the input_tokens list
for sentence in sentences:
# Tokenize the sentence using the GPT-2 tokenizer
sentence_tokens = tokenizer.encode(sentence)
# Check if adding the sentence would exceed the token limit
if len(input_tokens) + len(sentence_tokens) < 1440:
# Add the sentence tokens to the input_tokens list
input_tokens.extend(sentence_tokens)
else:
# If adding the sentence would exceed the token limit, truncate it
sentence_tokens = sentence_tokens[:1440-len(input_tokens)]
input_tokens.extend(sentence_tokens)
break
# Decode the input tokens into text
input_text = tokenizer.decode(input_tokens)
# Add the input text to the messages list
messages.append({"role": "user", "content": input_text})
# Get the current date and time in the local timezone
now_local = datetime.datetime.now()
# Create a timezone object for Eastern Time (ET)
et_tz = datetime.timezone(datetime.timedelta(hours=-5))
# Adjust the date and time to Eastern Time (ET)
now_et = now_local.astimezone(et_tz)
# Check if the accumulated tokens have exceeded 2096
num_tokens = num_tokens_from_messages(messages)
if num_tokens > 2096:
# Concatenate the chat history
chat_transcript = ""
for message in messages:
if message['role'] != 'system':
chat_transcript += f"[ANSWER {answer_count}]" + message['role'] + ": " + message['content'] + "\n\n"
# Append the number of tokens used to the end of the chat transcript
chat_transcript_copy = chat_transcript
chat_transcript_copy += f"Number of tokens used: {num_tokens}\n\n"
# Get the current UTC time
utc_time = datetime.now(timezone.utc)
# Convert to Eastern Time Zone
eastern_time = utc_time + timedelta(hours=-5)
# Format as string (YY-MM-DD HH:MM)
published_date = eastern_time.strftime('%m-%d-%y %H:%M')
import pandas as pd
# string dataframe?
df = pd.DataFrame([chat_transcript])
notion_df.upload(df, 'https://www.notion.so/page-827360c361f347f7bfefcc6dfbd10e51', title=str(published_date), api_key=API_KEY)
if num_tokens > 2200:
# Reset the messages list and answer counter
messages = [initial_message]
answer_count = 0
input_text = 'Can you click the Submit button one more time? (say Yes)'
# Add the input text to the messages list
messages.append({"role": "user", "content": input_text})
# Increment the answer counter
answer_count += 1
# Add the answer counter to the system message
system_message = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
max_tokens=2000
)["choices"][0]["message"]
# Add the system message to the messages list
messages.append(system_message)
# Concatenate the chat history
chat_transcript = ""
for message in messages:
if message['role'] != 'system':
chat_transcript += f"[ANSWER {answer_count}]" + message['role'] + ": " + message['content'] + "\n\n"
# Append the number of tokens used to the end of the chat transcript
with open("conversation_history.txt", "a") as f:
f.write(chat_transcript)
chat_transcript_copy = chat_transcript
chat_transcript_copy += f"Number of tokens used: {num_tokens}\n\n"
filename = datetime.datetime.now().strftime("%m%d%y_%H:%M_conversation_history.txt")
# dbx.files_upload(chat_transcript_copy.encode('utf-8'), f'/{filename}', mode=dropbox.files.WriteMode.overwrite, autorename=False, client_modified=None, mute=False)
# dbx.files_upload(chat_transcript_copy.encode('utf-8'), '/conversation_history.txt', mode=dropbox.files.WriteMode.overwrite, autorename=False, client_modified=None, mute=False)
# Get the current UTC time
utc_time = datetime.now(timezone.utc)
# Convert to Eastern Time Zone
eastern_time = utc_time + timedelta(hours=-5)
# Format as string (YY-MM-DD HH:MM)
published_date = eastern_time.strftime('%m-%d-%y %H:%M')
import pandas as pd
# string dataframe?
df = pd.DataFrame([chat_transcript_copy])
notion_df.upload(df, 'https://www.notion.so/page-827360c361f347f7bfefcc6dfbd10e51', title=str(chat_transcript_copy), api_key=API_KEY)
return chat_transcript
audio_input = Audio(source="microphone", type="filepath", label="Record your message")
text_input = Textbox(label="Type your message", max_length=4096)
output_text = gr.outputs.Textbox(label="Response")
output_audio = Audio()
iface = gr.Interface(
fn=transcribe,
inputs=[audio_input, text_input],
# outputs=(["audio", "text"]),
outputs="text",
title="Your Excellence Never Abates (YENA)",
description="Talk to the AI Tutor YENA",
capture_session=True,
autoplay=True)
# Launch Gradio interface
iface.launch()
# from transformers import pipeline, T5Tokenizer
# import pyttsx3
# import threading
# import time
# Set up speech engine
# engine = pyttsx3.init()
# def speak(text):
# # Get the current rate of the engine
# rate = engine.getProperty('rate')
# # Calculate the estimated time in seconds based on the length of the message and the current rate
# estimated_time = len(text) / (rate / 10)
# # Speak the text using the text-to-speech engine
# engine.say(text)
# engine.runAndWait()
# if engine._inLoop:
# # Wait for the speech engine to finish speaking
# time.sleep(estimated_time*1.5)
# engine.endLoop()
|