Commit
·
6e8f0e8
0
Parent(s):
Duplicate from son9john/USMLE
Browse files- install_espeak_ng.sh +11 -0
- .gitattributes +34 -0
- README.md +14 -0
- app.py +146 -0
- packages.txt +2 -0
- requirements.txt +14 -0
install_espeak_ng.sh
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
(
|
| 2 |
+
mkdir -p espeak
|
| 3 |
+
cd espeak
|
| 4 |
+
wget https://github.com/espeak-ng/espeak-ng/archive/refs/tags/1.51.zip
|
| 5 |
+
unzip -qq 1.51.zip
|
| 6 |
+
cd espeak-ng-1.51
|
| 7 |
+
./autogen.sh
|
| 8 |
+
./configure --prefix=`pwd`/../usr
|
| 9 |
+
make
|
| 10 |
+
make install
|
| 11 |
+
)
|
.gitattributes
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: USMLE
|
| 3 |
+
emoji: 📈
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: gray
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 3.19.1
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: bigscience-openrail-m
|
| 11 |
+
duplicated_from: son9john/USMLE
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from gradio.components import Audio, Textbox
|
| 4 |
+
import os
|
| 5 |
+
import re
|
| 6 |
+
import tiktoken
|
| 7 |
+
from transformers import GPT2Tokenizer
|
| 8 |
+
import whisper
|
| 9 |
+
import pandas as pd
|
| 10 |
+
from datetime import datetime, timezone, timedelta
|
| 11 |
+
import notion_df
|
| 12 |
+
import concurrent.futures
|
| 13 |
+
|
| 14 |
+
# Define the tokenizer and model
|
| 15 |
+
tokenizer = GPT2Tokenizer.from_pretrained('gpt2-medium')
|
| 16 |
+
model = openai.api_key = os.environ["OPENAI_API_KEY"]
|
| 17 |
+
|
| 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
|
| 24 |
+
|
| 25 |
+
# Define the Notion API key
|
| 26 |
+
API_KEY = os.environ["API_KEY"]
|
| 27 |
+
|
| 28 |
+
def transcribe(audio, text):
|
| 29 |
+
global messages
|
| 30 |
+
global answer_count
|
| 31 |
+
transcript = {'text': ''}
|
| 32 |
+
input_text = []
|
| 33 |
+
# Transcribe the audio if provided
|
| 34 |
+
if audio is not None:
|
| 35 |
+
audio_file = open(audio, "rb")
|
| 36 |
+
transcript = openai.Audio.transcribe("whisper-1", audio_file, language="en")
|
| 37 |
+
|
| 38 |
+
# Tokenize the text input
|
| 39 |
+
if text is not None:
|
| 40 |
+
# Split the input text into sentences
|
| 41 |
+
sentences = re.split("(?<=[.!?]) +", text)
|
| 42 |
+
|
| 43 |
+
# Initialize a list to store the tokens
|
| 44 |
+
input_tokens = []
|
| 45 |
+
|
| 46 |
+
# Add each sentence to the input_tokens list
|
| 47 |
+
for sentence in sentences:
|
| 48 |
+
# Tokenize the sentence using the GPT-2 tokenizer
|
| 49 |
+
sentence_tokens = tokenizer.encode(sentence)
|
| 50 |
+
# Check if adding the sentence would exceed the token limit
|
| 51 |
+
if len(input_tokens) + len(sentence_tokens) < 1440:
|
| 52 |
+
# Add the sentence tokens to the input_tokens list
|
| 53 |
+
input_tokens.extend(sentence_tokens)
|
| 54 |
+
else:
|
| 55 |
+
# If adding the sentence would exceed the token limit, truncate it
|
| 56 |
+
sentence_tokens = sentence_tokens[:1440-len(input_tokens)]
|
| 57 |
+
input_tokens.extend(sentence_tokens)
|
| 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": transcript["text"]+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)
|
| 68 |
+
if num_tokens > 2096:
|
| 69 |
+
# Concatenate the chat history
|
| 70 |
+
chat_transcript = "\n\n".join([f"[ANSWER {answer_count}]{message['role']}: {message['content']}" for message in messages if message['role'] != 'system'])
|
| 71 |
+
|
| 72 |
+
# Append the number of tokens used to the end of the chat transcript
|
| 73 |
+
chat_transcript += f"\n\nNumber of tokens used: {num_tokens}\n\n"
|
| 74 |
+
|
| 75 |
+
# Get the current time in Eastern Time (ET)
|
| 76 |
+
now_et = datetime.now(timezone(timedelta(hours=-5)))
|
| 77 |
+
# Format the time as string (YY-MM-DD HH:MM)
|
| 78 |
+
published_date = now_et.strftime('%m-%d-%y %H:%M')
|
| 79 |
+
|
| 80 |
+
# Upload the chat transcript to Notion
|
| 81 |
+
df = pd.DataFrame([chat_transcript])
|
| 82 |
+
notion_df.upload(df, 'https://www.notion.so/page-827360c361f347f7bfefcc6dfbd10e51', title=str(published_date+'back_up'), api_key=API_KEY)
|
| 83 |
+
|
| 84 |
+
# Reset the messages list and answer counter
|
| 85 |
+
messages = [initial_message]
|
| 86 |
+
answer_count = 0
|
| 87 |
+
input_text = 'Can you click the Submit button one more time? (say Yes)'
|
| 88 |
+
# Add the input text to the messages list
|
| 89 |
+
messages.append({"role": "user", "content": input_text})
|
| 90 |
+
else:
|
| 91 |
+
# Increment the answer counter
|
| 92 |
+
answer_count += 1
|
| 93 |
+
|
| 94 |
+
# Generate the system message using the OpenAI API
|
| 95 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 96 |
+
prompt = [{"text": f"{message['role']}: {message['content']}\n\n"} for message in messages]
|
| 97 |
+
system_message = openai.ChatCompletion.create(
|
| 98 |
+
model="gpt-3.5-turbo",
|
| 99 |
+
messages=messages,
|
| 100 |
+
max_tokens=2000
|
| 101 |
+
)["choices"][0]["message"]
|
| 102 |
+
# Wait for the completion of the OpenAI API call
|
| 103 |
+
|
| 104 |
+
# Add the system message to the messages list
|
| 105 |
+
messages.append(system_message)
|
| 106 |
+
|
| 107 |
+
# Concatenate the chat history
|
| 108 |
+
chat_transcript = "\n\n".join([f"[ANSWER {answer_count}]{message['role']}: {message['content']}" for message in messages if message['role'] != 'system'])
|
| 109 |
+
|
| 110 |
+
chat_transcript_copy = chat_transcript
|
| 111 |
+
# Append the number of tokens used to the end of the chat transcript
|
| 112 |
+
chat_transcript_copy += f"\n\nNumber of tokens used: {num_tokens}\n\n"
|
| 113 |
+
|
| 114 |
+
# Save the chat transcript to a file
|
| 115 |
+
with open("conversation_history.txt", "a") as f:
|
| 116 |
+
f.write(chat_transcript_copy)
|
| 117 |
+
|
| 118 |
+
# Upload the chat transcript to Notion
|
| 119 |
+
now_et = datetime.now(timezone(timedelta(hours=-5)))
|
| 120 |
+
published_date = now_et.strftime('%m-%d-%y %H:%M')
|
| 121 |
+
df = pd.DataFrame([chat_transcript_copy])
|
| 122 |
+
notion_df.upload(df, 'https://www.notion.so/page-827360c361f347f7bfefcc6dfbd10e51', title=str(published_date), api_key=API_KEY)
|
| 123 |
+
|
| 124 |
+
# Return the chat transcript
|
| 125 |
+
return chat_transcript
|
| 126 |
+
|
| 127 |
+
# Define the input and output components for Gradio
|
| 128 |
+
audio_input = Audio(source="microphone", type="filepath", label="Record your message")
|
| 129 |
+
text_input = Textbox(label="Type your message", max_length=4096)
|
| 130 |
+
output_text = gr.outputs.Textbox(label="Response")
|
| 131 |
+
output_audio = Audio()
|
| 132 |
+
|
| 133 |
+
# Define the Gradio interface
|
| 134 |
+
iface = gr.Interface(
|
| 135 |
+
fn=transcribe,
|
| 136 |
+
inputs=[audio_input, text_input],
|
| 137 |
+
outputs=[output_text],
|
| 138 |
+
title="Your Excellence Never Abates (YENA)",
|
| 139 |
+
description="Talk to the AI Tutor YENA",
|
| 140 |
+
theme="compact",
|
| 141 |
+
layout="vertical",
|
| 142 |
+
allow_flagging=False
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
# Run the Gradio interface
|
| 146 |
+
iface.launch()
|
packages.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
espeak
|
| 2 |
+
ffmpeg
|
requirements.txt
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
openai
|
| 3 |
+
transformers
|
| 4 |
+
sentencepiece
|
| 5 |
+
pipeline
|
| 6 |
+
py-espeak-ng
|
| 7 |
+
pyttsx3
|
| 8 |
+
nltk
|
| 9 |
+
tiktoken
|
| 10 |
+
openai-whisper
|
| 11 |
+
dropbox
|
| 12 |
+
notion-client
|
| 13 |
+
notion-df
|
| 14 |
+
pandas
|