File size: 6,296 Bytes
6e8f0e8
 
 
cefae98
6e8f0e8
 
cefae98
6e8f0e8
cefae98
6e8f0e8
 
 
 
 
 
b62a22a
6e8f0e8
 
7b74a47
1b5a25e
7b74a47
75e6d4c
6e8f0e8
01002c8
6e8f0e8
 
 
 
 
b62a22a
6e8f0e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01002c8
6e8f0e8
 
 
 
 
 
 
 
 
 
 
 
cbd0eac
6e8f0e8
 
 
 
 
a046b3b
6e8f0e8
 
 
75e6d4c
6e8f0e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01002c8
8314ae4
 
01002c8
8314ae4
01002c8
8314ae4
6e8f0e8
b25fbe8
6e8f0e8
a046b3b
6e8f0e8
a046b3b
6e8f0e8
 
cbd0eac
6e8f0e8
a046b3b
 
6e8f0e8
cefae98
 
6e8f0e8
7297744
 
 
cefae98
 
58a3edb
6e8f0e8
 
 
 
 
b5494d1
a850b83
6e8f0e8
 
 
 
 
 
cefae98
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
import openai
import gradio as gr
from gradio.components import Audio, Textbox
import os
import re
import tiktoken
from transformers import GPT2Tokenizer
import whisper
import pandas as pd
from datetime import datetime, timezone, timedelta
import notion_df
import concurrent.futures

# Define the tokenizer and model
tokenizer = GPT2Tokenizer.from_pretrained('gpt2-medium')
model = openai.api_key = os.environ["OAPI_KEY"]

# Define the initial message and messages list
initialt = 'If user asked COLORIZE, don't need to do anything but present the input as it is with organized tabs (layers). 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. \
                    You are going to keep answer and also challenge the student to learn USMLE anatomy, phsysiology, and pathology.'
initial_message = {"role": "system", "content": initialt}
messages = [initial_message]
messages_rev = [initial_message]

# Define the answer counter
answer_count = 0

# Define the Notion API key
API_KEY = os.environ["NAPI_KEY"]

def transcribe(audio, text):
    global messages
    global answer_count
    transcript = {'text': ''} 
    input_text = []
    # Transcribe the audio if provided
    if audio is not None:
        audio_file = open(audio, "rb")
        transcript = openai.Audio.transcribe("whisper-1", audio_file, language="en")

    # Tokenize the text input
    if text is not 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": transcript["text"]+input_text})


    # Check if the accumulated tokens have exceeded 2096
    num_tokens = sum(len(tokenizer.encode(message["content"])) for message in messages)
    if num_tokens > 2096:
        # Concatenate the chat history
        chat_transcript = "\n\n".join([f"[ANSWER {answer_count}]{message['role']}: {message['content']}" for message in messages if message['role'] != 'system'])

        # Append the number of tokens used to the end of the chat transcript
        chat_transcript += f"\n\nNumber of tokens used: {num_tokens}\n\n"

        # Get the current time in Eastern Time (ET)
        now_et = datetime.now(timezone(timedelta(hours=-4)))
        # Format the time as string (YY-MM-DD HH:MM)
        published_date = now_et.strftime('%m-%d-%y %H:%M')

        # Upload the chat transcript to Notion
        df = pd.DataFrame([chat_transcript])
        notion_df.upload(df, 'https://www.notion.so/YENA-be569d0a40c940e7b6e0679318215790?pvs=4', title=str(published_date+'back_up'), api_key=API_KEY)

        # Reset the messages list and answer counter
        messages = [initial_message]
        messages.append({"role": "user", "content": initialt})
        answer_count = 0
        # Add the input text to the messages list
        messages.append({"role": "user", "content": input_text})
    else:
        # Increment the answer counter
        answer_count += 1

    # Generate the system message using the OpenAI API
    with concurrent.futures.ThreadPoolExecutor() as executor:
        prompt = [{"text": f"{message['role']}: {message['content']}\n\n"} for message in messages]
        system_message = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=messages,
            max_tokens=2000
        )["choices"][0]["message"]
    # Wait for the completion of the OpenAI API call
        
    # Add the system message to the messages list
    messages.append(system_message)
        
    # Add the system message to the beginning of the messages list
    messages_rev.insert(0, system_message)
    # Add the input text to the messages list
    messages_rev.insert(0, {"role": "user", "content": input_text + transcript["text"]})

    # Concatenate the chat history
    chat_transcript = "\n\n".join([f"[ANSWER {answer_count}]{message['role']}: {message['content']}" for message in messages_rev if message['role'] != 'system'])

    # chat_transcript_copy = chat_transcript
    # Append the number of tokens used to the end of the chat transcript
    chat_transcript += f"\n\nNumber of tokens used: {num_tokens}\n\n"
    
    # Upload the chat transcript to Notion
    now_et = datetime.now(timezone(timedelta(hours=-4)))
    published_date = now_et.strftime('%m-%d-%y %H:%M')
    df = pd.DataFrame([chat_transcript])
    notion_df.upload(df, 'https://www.notion.so/YENA-be569d0a40c940e7b6e0679318215790?pvs=4', title=str(published_date), api_key=API_KEY)

    # Return the chat transcript
    return system_message['content']
    
# Define the input and output components for Gradio
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()

# Define the Gradio interface
iface = gr.Interface(
    fn=transcribe,
    inputs=[audio_input, text_input],
    outputs=[output_text],
    title="Hold On, Pain Ends (HOPE)",
    description="Talk to Your USMLE Tutor HOPE",
    theme="compact",
    layout="vertical",
    allow_flagging=False
    )

# Run the Gradio interface
iface.launch()