File size: 7,745 Bytes
a4bbc06
8313f09
77b8357
6b31d07
 
 
affac96
a1e8d8f
bae5b02
73a141a
37c02da
1cdc167
0039926
 
 
 
5320c7c
 
 
 
 
 
73a141a
e18b966
667b2dc
73a141a
44ab821
a1e8d8f
1cdc167
7c45b28
 
6c42480
5320c7c
c82e3c7
39add81
5320c7c
bc4c76e
56c10d2
 
 
 
bc4c76e
56c10d2
 
 
 
bf150e6
bc4c76e
 
6b31d07
62842f4
6b31d07
2da8950
6b31d07
11b16f9
6b31d07
11b16f9
 
90e81fe
 
 
 
 
9546f1b
90e81fe
9546f1b
6b31d07
 
 
 
 
 
 
b982841
6b31d07
d9be7b0
 
6b31d07
 
33dddeb
0e71847
d9be7b0
 
9546f1b
 
 
5941228
6b31d07
2da8950
6c42480
5320c7c
6a04a92
f478057
6b31d07
 
62842f4
6b31d07
 
 
b0189a2
bc4c76e
56c10d2
 
 
 
 
 
 
b0189a2
6b31d07
 
 
 
a874e7a
6b31d07
acae979
96637c9
d9be7b0
96637c9
 
d9be7b0
f5d4c8d
96637c9
 
 
 
 
 
b890649
62603a5
96637c9
 
45f4b02
 
b890649
 
62842f4
 
 
f9c7c43
62603a5
6b31d07
96637c9
 
 
 
 
 
 
6b31d07
62842f4
25279a1
62603a5
62842f4
d9be7b0
 
6b31d07
 
 
 
d9be7b0
6b31d07
 
 
 
bce19a7
6b31d07
 
 
 
 
 
 
 
 
 
 
d9be7b0
0831a35
bce19a7
eeba367
b77d45c
6b31d07
4459ac3
 
6b31d07
 
 
d9be7b0
aa18bf6
6b31d07
62842f4
6f08e8f
6b31d07
d9be7b0
b982841
6b31d07
d9be7b0
 
 
6b31d07
119f6c6
62842f4
 
 
51b7af6
6b31d07
 
 
 
 
 
 
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


import gradio as gr
import openai
import requests
import csv
import os
import langchain
import chromadb
import glob
import pickle

import huggingface_hub
from huggingface_hub import Repository

from datetime import datetime
from PyPDF2 import PdfReader
from PyPDF2 import PdfWriter
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain import OpenAI
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import PyPDFLoader
from langchain.chains.question_answering import load_qa_chain


openai.api_key = os.environ['openai_key']
os.environ["OPENAI_API_KEY"] = os.environ['openai_key']


prompt_templates = {"All Needs Experts": "Respond as if you are combination of all needs assessment experts."}
actor_description = {"All Needs Experts": "<div style='float: left;margin: 0px 5px 0px 5px;'><img src='https://na.weshareresearch.com/wp-content/uploads/2023/04/experts2.jpg' alt='needs expert image' style='width:70px;align:top;'></div>A combiation of all needs assessment experts."}


prompts_archive_url = "https://huggingface.co/datasets/ryanrwatkins/na_prompts_archive"
prompts_archive_file_name = "prompts_archives.csv"
prompts_archive_file = os.path.join("prompts_archive", prompts_archive_file_name)
print(prompts_archive_file)

HF_TOKEN = os.environ.get("HF_token_write")
repo = Repository(
    local_dir="prompts_archive", clone_from=prompts_archive_url, use_auth_token=HF_TOKEN, git_user="ryanrwatkins", git_email="rwatkins@gwu.edu"
)



def get_empty_state():
    return { "messages": []}


def download_prompt_templates():
    url = "https://huggingface.co/spaces/ryanrwatkins/needs/raw/main/gurus.txt"
    try:
        response = requests.get(url)
        reader = csv.reader(response.text.splitlines())
        next(reader)  # skip the header row
        for row in reader:
            if len(row) >= 2:
                act = row[0].strip('"')
                prompt = row[1].strip('"')
                description = row[2].strip('"')
                prompt_templates[act] = prompt
                actor_description[act] = description

    except requests.exceptions.RequestException as e:
        print(f"An error occurred while downloading prompt templates: {e}")
        return

    choices = list(prompt_templates.keys())
    choices = choices[:1] + sorted(choices[1:])
    return gr.update(value=choices[0], choices=choices)



def on_prompt_template_change(prompt_template):
    if not isinstance(prompt_template, str): return
    return prompt_templates[prompt_template]



def on_prompt_template_change_description(prompt_template):
    if not isinstance(prompt_template, str): return
    return actor_description[prompt_template]



def submit_message(prompt, prompt_template, temperature, max_tokens, context_length, state):
    
    
    history = state['messages']

    if not prompt:
        return gr.update(value=''), [(history[i]['content'], history[i+1]['content']) for i in range(0, len(history)-1, 2)], state
    
    prompt_template = prompt_templates[prompt_template]

    
    
    with open(prompts_archive_file, "a") as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=["prompt", "time"])
        writer.writerow(
            {"prompt": str(prompt), "time": str(datetime.now())}
        )
    commit_url = repo.push_to_hub()
    print(commit_url)

    system_prompt = []
    if prompt_template:
        system_prompt = [{ "role": "system", "content": prompt_template }]

    prompt_msg = { "role": "user", "content": prompt }


    #try:

    with open("embeddings.pkl", 'rb') as f: 
        new_docsearch = pickle.load(f)
        

    query = str(system_prompt + history +  [prompt_msg])
    
    docs = new_docsearch.similarity_search(query)

    chain = load_qa_chain(ChatOpenAI(temperature=temperature, max_tokens=max_tokens, model_name="gpt-3.5-turbo"), chain_type="stuff")
    completion = chain.run(input_documents=docs, question=query)
    

    
    get_empty_state()
    state['content'] = completion
    #state.append(completion.copy())
    
    completion = { "content": completion }
    
    
    #state['total_tokens'] += completion['usage']['total_tokens']


    
    #except Exception as e:
    #    history.append(prompt_msg.copy())
    #    error  = {
    #        "role": "system",
    #        "content": f"Error: {e}"
    #    }
    #    history.append(error.copy())

    #total_tokens_used_msg = f"Total tokens used: {state['total_tokens']}"

    chat_messages = [(prompt_msg['content'], completion['content'])]    
    return '', chat_messages,  state    # total_tokens_used_msg,


def clear_conversation():
    return gr.update(value=None, visible=True), None, "", get_empty_state()



css = """
      #col-container {max-width: 80%; margin-left: auto; margin-right: auto;}
      #chatbox {min-height: 400px;}
      #header {text-align: center;}
      #prompt_template_preview {padding: 1em; border-width: 1px; border-style: solid; border-color: #e0e0e0; border-radius: 4px; min-height: 150px;}
      #total_tokens_str {text-align: right; font-size: 0.8em; color: #666;}
      #label {font-size: 0.8em; padding: 0.5em; margin: 0;}
      .message { font-size: 1.2em; }
      """

with gr.Blocks(css=css) as demo:
    
    state = gr.State(get_empty_state())


    with gr.Column(elem_id="col-container"):

           
        gr.Markdown("""## Ask questions of *needs assessment* experts,  
                    ## get responses from a *needs assessment experts* version of ChatGPT.  
                    Ask questions of all of them, or pick your expert below.""" ,
                    elem_id="header")
        
        
        with gr.Row():
            with gr.Column():
                chatbot = gr.Chatbot(elem_id="chatbox")
                input_message = gr.Textbox(show_label=False, placeholder="Enter your needs assessment question", visible=True).style(container=False)

                btn_submit = gr.Button("Submit")
                #total_tokens_str = gr.Markdown(elem_id="total_tokens_str")
                btn_clear_conversation = gr.Button("Start New Conversation")
            with gr.Column():
                prompt_template = gr.Dropdown(label="Choose an Expert:", choices=list(prompt_templates.keys()))
                prompt_template_preview = gr.Markdown(elem_id="prompt_template_preview")
                with gr.Accordion("Advanced parameters", open=False):
                    temperature = gr.Slider(minimum=0, maximum=2.0, value=0.7, step=0.1, label="Flexibility", info="Higher = More AI, Lower = More Expert")
                    max_tokens = gr.Slider(minimum=100, maximum=400, value=200, step=1, label="Length of Response.")
                    context_length = gr.Slider(minimum=1, maximum=5, value=2, step=1, label="Context Length", info="Number of previous questions you have asked.")

   
    btn_submit.click(submit_message, [ input_message, prompt_template, temperature, max_tokens, context_length, state], [input_message, chatbot,  state])
    input_message.submit(submit_message, [ input_message, prompt_template, temperature, max_tokens, context_length, state], [input_message, chatbot,  state])
    btn_clear_conversation.click(clear_conversation, [], [input_message, chatbot,  state])
    prompt_template.change(on_prompt_template_change_description, inputs=[prompt_template], outputs=[prompt_template_preview])

    
    demo.load(download_prompt_templates, inputs=None, outputs=[prompt_template], queur=False)


demo.queue(concurrency_count=10)
demo.launch(height='800px')