File size: 24,746 Bytes
32a98d6
f6f33c3
b1a0926
21c729c
f6f33c3
 
a289e51
f6f33c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
740fc36
f6f33c3
 
 
 
 
 
f932e1e
eda36d5
f6f33c3
f932e1e
 
 
 
f6f33c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32a98d6
0809a99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6117d34
 
 
 
 
 
 
 
a44d2b6
 
 
 
 
 
287cf0a
 
a44d2b6
 
 
 
 
 
 
 
9a894ab
 
 
 
 
 
 
 
e7dc910
9a894ab
 
 
 
 
 
 
e6ffb3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5040d0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287cf0a
7ca6340
5040d0d
a2cf973
 
287cf0a
5040d0d
9a894ab
a2cf973
9a894ab
15fb029
 
 
a44d2b6
 
 
 
 
 
287cf0a
 
a44d2b6
 
 
 
 
 
 
 
6117d34
4f4ce1a
6117d34
 
 
 
 
 
7ca6340
 
3866b6b
7ca6340
 
 
 
 
 
 
 
 
 
 
287cf0a
 
7ca6340
 
 
287cf0a
7ca6340
 
6117d34
a2cf973
6117d34
4d67d8b
 
 
1a8c55e
 
 
f41898f
 
f9462af
f41898f
 
45c6f19
f41898f
 
 
 
 
45c6f19
f41898f
 
 
45c6f19
f41898f
 
c7a1920
7ce74a6
615a4cf
 
7ce74a6
 
957a53b
 
 
225835c
 
 
 
 
615a4cf
7ce74a6
957a53b
7ce74a6
 
 
bf0375b
c7a1920
f41898f
 
 
45c6f19
f41898f
c7a1920
 
 
 
 
f41898f
 
 
 
 
 
45c6f19
 
107aa83
f41898f
 
 
 
 
45c6f19
f41898f
 
 
 
 
 
 
 
 
 
 
 
45c6f19
f41898f
 
45c6f19
f41898f
 
 
 
 
 
 
 
 
 
45c6f19
f41898f
 
 
 
 
 
05238dd
 
f41898f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05238dd
 
f41898f
 
 
 
 
 
 
 
107aa83
f41898f
107aa83
f41898f
 
 
 
 
 
 
 
 
 
45c6f19
 
f41898f
 
 
 
 
 
 
107aa83
f41898f
 
 
 
 
 
 
 
 
 
 
 
 
05238dd
f41898f
 
 
 
107aa83
f41898f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05238dd
f41898f
 
 
 
 
 
05238dd
f41898f
 
 
 
 
 
 
 
 
505ea14
f41898f
 
 
05238dd
f41898f
 
 
 
 
 
 
 
05238dd
f41898f
 
 
 
05238dd
f41898f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05238dd
 
f41898f
 
 
 
 
 
 
 
 
 
 
 
 
05238dd
f41898f
 
 
 
 
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
import streamlit as st
import pandas as pd
from openai import OpenAI
from PyPDF2 import PdfReader

# source: eagle0504/document-search-q-series
def read_and_textify_advanced(files, chunk_size):
    """
    Reads PDF files and extracts text from each page, breaking the text into specified segments.
    This function iterates over a list of uploaded PDF files, extracts text from each page,
    and compiles a list of texts and corresponding source information, segmented into smaller parts
    of approximately 'chunk_size' words each.
    Args:
    files (List[st.uploaded_file_manager.UploadedFile]): A list of uploaded PDF files.
    chunk_size (int): The number of words per text segment. Default is 50.
    Returns: A list of strings, where each string is a segment of text extracted from a PDF page.
    """

    text_list = []  # List to store extracted text segments

    # Iterate over each file
    for file in files:
        pdfReader = PdfReader(file)  # Create a PDF reader object
        # Iterate over each page in the PDF
        for i in range(len(pdfReader.pages)):
            pageObj = pdfReader.pages[i]  # Get the page object
            text = pageObj.extract_text()  # Extract text from the page
            if text:
                # Split text into chunks of approximately 'chunk_size' words
                words = text.split(".")
                for j in range(0, len(words), chunk_size):
                    # Get the chunk of text from j-chunk_size to j+chunk_size
                    # start = max(0, j - chunk_size)
                    # end = min(len(words), j + chunk_size + 1)
                    chunk = ".".join(words[j:j+chunk_size]) + '.'
                    chunk = chunk.strip()
                    text_list.append(chunk)
                    # Create a source identifier for each chunk and add it to the list
            else:
                # If no text extracted, still add a placeholder
                text_list.append("")
            pageObj.clear()  # Clear the page object (optional, for memory management)

    return text_list

def get_questions(context, instructions) -> str:
    """
    Given a text context, generates a list of questions using OpenAI's GPT-3 API.

    Args:
    - context: A string representing the context for which questions should be generated.

    Returns:
    - A string containing the question generated by the API.
    """

    try:
        response = client.chat.completions.create(
            model="gpt-4o-mini",
            messages=[
              {"role": "system", "content": "You are a helpful assistant."},
              {"role": "user", "content": f"{instructions}\n\nText: {context}\n\nQuestions:\n"}
            ]
        )
        # Extract question text from the response
        question_text = response.choices[0].message.content
        return question_text
    except:
        # Return an empty string if there was an error
        return ""

def get_answers(row, instructions) -> str:
    """
    Given a dataframe row containing context and questions, generates an answer using OpenAI's GPT-3 API.

    Args:
    - row: A pandas dataframe row containing 'context' and 'questions' columns.

    Returns:
    - A string containing the answer generated by the API.
    """

    try:
        response = client.chat.completions.create(
            model="gpt-4o-mini",
            messages=[
              {"role": "system", "content": "You are a helpful assistant."},
              {"role": "user", "content": f"{instructions}\n\nText: {row.context}\n\nQuestions:\n{row.questions}\n\nAnswers:\n"}
            ]
        )
        # Extract answer text from the response
        answer_text = response.choices[0].message.content
        return answer_text
    except Exception as e:
        # Print the error message and return an empty string if there was an error
        print (e)
        return ""

st.set_page_config(page_title="ChatbotGuide", layout="wide")
st.title("Chatbot Guide")

# Define the options in the dropdown menu
app_options = [
    "1) Create CSVs",
    "2) Merge CSVs",
    "3) Upload Datasets",
    "4) Create Chatbot"
]

# Sidebar dropdown for selecting the application
selected_app = st.sidebar.selectbox("Select Step (1-4)", app_options)

# Clear session state when switching apps
if 'last_selected_app' in st.session_state:
    if st.session_state.last_selected_app != selected_app:
        st.session_state.clear()

st.session_state.last_selected_app = selected_app

if 'submit' not in st.session_state:
    st.session_state.submit = False
if 'error' not in st.session_state:
    st.session_state.error = ""
if 'success' not in st.session_state:
    st.session_state.success = None

if selected_app == "1) Create CSVs":  
    if st.session_state.error != "":
        st.error(st.session_state.error)
    
    if st.session_state.success != None:
        st.success("Success! Download the Q/A pairs below / Click reset to upload more PDFs")
        st.download_button(
            label=f"Download CSV: length = {st.session_state.success[1]}",
            data=st.session_state.success[0],
            file_name='questions_answers.csv',
            mime='text/csv',
        )
        if st.button('Reset'):
            st.session_state.clear()
            st.rerun()
            
    else:
        uploaded_files = st.file_uploader("Upload PDFs Here", type="pdf", accept_multiple_files=True)
        
        question_protocol = st.text_input("Provide instructions for how questions should be generated", "Write a question based on the text")
        answer_protocol = st.text_input("Provide instructions for how answers should be generated", "Write an answer based on the text")
        
        sentence_chunks = st.number_input("Number sentences per Q/A pair", value=2, step=1, min_value=1, max_value=3)
        
        openai_api_key = st.text_input("Enter your OpenAI API key", type="password")
    
        submit = st.button("Submit")
        if submit:
            st.session_state.submit = True
        
        if st.session_state.submit:
            if uploaded_files:
                client = OpenAI(api_key=openai_api_key)

                with st.spinner("Loading, please be patient with us ... πŸ™"):
                    # test api key
                    try:
                        response = client.chat.completions.create(
                            model="gpt-4o-mini",
                            messages=[
                              {"role": "system", "content": "You are a helpful assistant."},
                              {"role": "user", "content": "Say this is a test"}
                            ]
                        )
                    except:
                        st.session_state.clear()
                        st.session_state.error = "OpenAI API key is invalid"
                        st.rerun()
                    
                with st.spinner("Loading, please be patient with us ... πŸ™"):
                    textify_output = read_and_textify_advanced(uploaded_files, sentence_chunks)
                    
                    df = pd.DataFrame(textify_output)
                    df.columns = ['context']
            
                    if question_protocol == "":
                        question_protocol = "Write questions based on the text"
                    df['questions'] = df.apply(lambda row: get_questions(row['context'], question_protocol), axis=1)
            
                    if answer_protocol == "":
                        answer_protocol = "Write answers based on the text"
                    df['answers'] = df.apply(lambda row: get_answers(row, answer_protocol), axis=1)
            
                    df = df.drop('context', axis=1)
                    length = len(df)
                    
                    csv = df.to_csv(index=False).encode('utf-8')

                    st.session_state.clear()
                    st.session_state.success = (csv, length)
                    st.rerun()
            else:
                st.session_state.clear()
                st.session_state.error = "Please upload at least 1 PDF"
                st.rerun()

if selected_app == "2) Merge CSVs":
    if st.session_state.error != "":
        st.error(st.session_state.error)
    
    if st.session_state.success != None:
        st.success("Success! Download the merged CSV with Q/A pairs below / Reset to merge more CSVs")
        st.download_button(
            label=f"Download CSV: length = {st.session_state.success[1]}",
            data=st.session_state.success[0],
            file_name='questions_answers.csv',
            mime='text/csv',
        )
        if st.button('Reset'):
            st.session_state.clear()
            st.rerun()
            
    else:
        uploaded_files = st.file_uploader("Upload CSV files to merge", accept_multiple_files=True, type="csv")
    
        submit = st.button("Submit")
        if submit:
            st.session_state.submit = True
        
        if st.session_state.submit:
            if len(uploaded_files) > 1:
                dfs = []
                for file in uploaded_files:
                    df = pd.read_csv(file)
                    
                    if "questions" in df.columns and "answers" in df.columns:
                        df = df[["questions", "answers"]]
                        dfs.append(df)
                
                    else:
                        st.session_state.clear()
                        st.session_state.error = "Please upload CSVs that have been generated from 1) Create CSV"
                        st.rerun()
                        
                df = pd.concat(dfs, ignore_index=True)
                length = len(df)
                
                csv = df.to_csv(index=False).encode('utf-8')
                
                st.session_state.clear()
                st.session_state.success = (csv, length)
                st.rerun()
                
            else:
                st.session_state.clear()
                st.session_state.error = "Please upload at least 2 CSVs to merge"
                st.rerun()

if selected_app == "3) Upload Datasets":
    st.markdown("Go to this [google colab link](https://colab.research.google.com/drive/1eCpk9HUoCKZb--tiNyQSHFW2ojoaA35m) to get started")

if selected_app == "4) Create Chatbot":
    if st.session_state.error != "":
        st.error(st.session_state.error)

    if st.session_state.success != None:
        st.success("Success! Copy/paste the requirements.txt and app.py files into your HuggingFace Space")
        
        st.write('requirements.txt')
        st.code(st.session_state.success[0], language='python')

        st.write('app.py')
        st.code(st.session_state.success[1], language='python')
    
        if st.button('Reset'):
            st.session_state.clear()
            st.rerun()
    
    else:
        organization_name = st.text_input("What is the name of your organization", "")
        num_domains = st.number_input("How many datasets do you have uploaded", value=1, step=1, min_value=1, max_value=10)

        st.divider()

        domain_info = []
        for i in range(num_domains):
            domain_link = st.text_input(f"Please enter link to dataset {i+1} with the format username/dataset_name", "Example: KeshavRa/About_YSA_Database")
            domain_name = st.text_input(f"What should domain {i+1} be called in the chatbot itself", "Example: About YSA")
            domain_purpose = st.text_area(f"What is the purpose of domain {i+1}, provide example questions (this will be visible to users of the chatbot)", '''Example: On this page, you can learn about what YSA does, how YSA was started, the advisory board, and the programs we offer.
            Example Questions
            --> What is the purpose of Youth Spirit Artworks?
            --> Who created YSA?
            --> What is the Advisory Board for Youth Spirit Artworks?
            --> What are the three empowerment-focused program areas of YSA?
            '''
            )
            domain_instructions = st.text_input(f"What baseline instructions/specifications should be sent to ChatGPT to answer questions in domain {i+1}", "Example: You are an assistant to help the user learn more about Youth Spirit Artworks")
            
            domain = {"link": domain_link, "name": domain_name, "purpose": domain_purpose, "instructions": domain_instructions}
            domain_info.append(domain)
            st.divider()
        
        submit = st.button("Submit")
        if submit:
            st.session_state.submit = True
        
        if st.session_state.submit:
            if organization_name == "":
                st.session_state.clear()
                st.session_state.error = "Please enter an organization name"
                st.rerun()
            
            requirements = '''
                openai
                scipy
                streamlit
                chromadb
                datasets
            '''
        
            app = f"""
                import os
                import streamlit as st
                from datasets import load_dataset
                import chromadb
                import string
            
                from openai import OpenAI
                
                import numpy as np
                import pandas as pd
                
                from scipy.spatial.distance import cosine
                
                from typing import Dict, List
                
                def merge_dataframes(dataframes):
                    # Concatenate the list of dataframes
                    combined_dataframe = pd.concat(dataframes, ignore_index=True)
            
                    # Ensure that the resulting dataframe only contains the columns "context", "questions", "answers"
                    combined_dataframe = combined_dataframe[['context', 'questions', 'answers']]
                
                    return combined_dataframe
            
                def call_chatgpt(prompt: str, directions: str) -> str:
                    '''
                    Uses the OpenAI API to generate an AI response to a prompt.
                    Args:
                        prompt: A string representing the prompt to send to the OpenAI API.
                    Returns:
                        A string representing the AI's generated response.
                    '''
                
                    # Use the OpenAI API to generate a response based on the input prompt.
                    client = OpenAI(api_key = os.environ["OPENAI_API_KEY"])
                
                    completion = client.chat.completions.create(
                    model="gpt-3.5-turbo-0125",
                    messages=[
                        {{"role": "system", "content": directions}},
                        {{"role": "user", "content": prompt}}
                    ]
                    )
                
                    # Extract the text from the first (and only) choice in the response output.
                    ans = completion.choices[0].message.content
                
                    # Return the generated AI response.
                    return ans
                
                def openai_text_embedding(prompt: str) -> str:
                    return openai.Embedding.create(input=prompt, model="text-embedding-ada-002")[
                        "data"
                    ][0]["embedding"]
                
                def calculate_sts_openai_score(sentence1: str, sentence2: str) -> float:
                    # Compute sentence embeddings
                    embedding1 = openai_text_embedding(sentence1)  # Flatten the embedding array
                    embedding2 = openai_text_embedding(sentence2)  # Flatten the embedding array
                
                    # Convert to array
                    embedding1 = np.asarray(embedding1)
                    embedding2 = np.asarray(embedding2)
            
                    # Calculate cosine similarity between the embeddings
                    similarity_score = 1 - cosine(embedding1, embedding2)
                
                    return similarity_score
            
                def add_dist_score_column(
                    dataframe: pd.DataFrame, sentence: str,
                ) -> pd.DataFrame:
                    dataframe["stsopenai"] = dataframe["questions"].apply(
                            lambda x: calculate_sts_openai_score(str(x), sentence)
                    )
                    
                    sorted_dataframe = dataframe.sort_values(by="stsopenai", ascending=False)
                
                
                    return sorted_dataframe.iloc[:5, :]
            
                def convert_to_list_of_dict(df: pd.DataFrame) -> List[Dict[str, str]]:
                    '''
                    Reads in a pandas DataFrame and produces a list of dictionaries with two keys each, 'question' and 'answer.'
                    Args:
                        df: A pandas DataFrame with columns named 'questions' and 'answers'.
                    Returns:
                        A list of dictionaries, with each dictionary containing a 'question' and 'answer' key-value pair.
                    '''
                
                    # Initialize an empty list to store the dictionaries
                    result = []
                
                    # Loop through each row of the DataFrame
                    for index, row in df.iterrows():
                        # Create a dictionary with the current question and answer
                        qa_dict_quest = {{"role": "user", "content": row["questions"]}}
                        qa_dict_ans = {{"role": "assistant", "content": row["answers"]}}
            
                        # Add the dictionary to the result list
                        result.append(qa_dict_quest)
                        result.append(qa_dict_ans)
            
                    # Return the list of dictionaries
                    return result
                
                st.sidebar.markdown('''This is a chatbot to help you learn more about {organization_name}''')
                
                domain = st.sidebar.selectbox("Select a topic", "")
                
                special_threshold = 0.3
                
                n_results = 3
                
                clear_button = st.sidebar.button("Clear Conversation", key="clear")
                
                if clear_button:
                    st.session_state.messages = []
                    st.session_state.curr_domain = ""
        
        
                
                ### 
                ###
                ### Load the dataset from a provided source.
                ###
                ###  
                  
                initial_input = "Tell me about {organization_name}"
            
                # Initialize a new client for ChromeDB.
                client = chromadb.Client()
                
                # Generate a random number between 1 billion and 10 billion.
                random_number: int = np.random.randint(low=1e9, high=1e10)
                
                # Generate a random string consisting of 10 uppercase letters and digits.
                random_string: str = "".join(
                    np.random.choice(list(string.ascii_uppercase + string.digits), size=10)
                )
                
                # Combine the random number and random string into one identifier.
                combined_string: str = f"{{random_number}}{{random_string}}"
            
                # Create a new collection in ChromeDB with the combined string as its name.
                collection = client.create_collection(combined_string)
                
                st.title("{organization_name} Chatbot")
                
                # Initialize chat history
                if "messages" not in st.session_state:
                    st.session_state.messages = []
            
                if "curr_domain" not in st.session_state:
                    st.session_state.curr_domain = ""
                
                ### 
                ###
                ### init_messages dict (one key per domain)
                ###
                ###
            
                ### 
                ###
                ### chatbot_instructions dict (one key per domain)
                ###
                ###
                
                # Embed and store the first N supports for this demo
                with st.spinner("Loading, please be patient with us ... πŸ™"):
                    L = len(dataset["train"]["questions"])
                    
                    collection.add(
                        ids=[str(i) for i in range(0, L)],  # IDs are just strings
                        documents=dataset["train"]["questions"],  # Enter questions here
                        metadatas=[{{"type": "support"}} for _ in range(0, L)],
                    )
                    
                    if st.session_state.curr_domain != domain:
                        st.session_state.messages = []
                        
                        init_message = init_messages[domain]
                        st.session_state.messages.append({{"role": "assistant", "content": init_message}})
                        
                        st.session_state.curr_domain = domain
                
                # Display chat messages from history on app rerun
                for message in st.session_state.messages:
                    with st.chat_message(message["role"]):
                        st.markdown(message["content"])
                
                # React to user input
                if prompt := st.chat_input("Tell me about {organization_name}"):
                    # Display user message in chat message container
                    st.chat_message("user").markdown(prompt)
                    # Add user message to chat history
                    st.session_state.messages.append({{"role": "user", "content": prompt}})
                
                    question = prompt
                
                    results = collection.query(query_texts=question, n_results=n_results)
                
                    idx = results["ids"][0]
                    idx = [int(i) for i in idx]
                    ref = pd.DataFrame(
                        {{
                            "idx": idx,
                            "questions": [dataset["train"]["questions"][i] for i in idx],
                            "answers": [dataset["train"]["answers"][i] for i in idx],
                            "distances": results["distances"][0],
                        }}
                    )
                    # special_threshold = st.sidebar.slider('How old are you?', 0, 0.6, 0.1) # 0.3
                    # special_threshold = 0.3
                    filtered_ref = ref[ref["distances"] < special_threshold]
                    if filtered_ref.shape[0] > 0:
                        # st.success("There are highly relevant information in our database.")
                        ref_from_db_search = filtered_ref["answers"].str.cat(sep=" ")
                        final_ref = filtered_ref
                    else:
                        # st.warning(
                        #     "The database may not have relevant information to help your question so please be aware of hallucinations."
                        # )
                        ref_from_db_search = ref["answers"].str.cat(sep=" ")
                        final_ref = ref
                
                    engineered_prompt = f'''
                        Based on the context: {{ref_from_db_search}},
                        answer the user question: {{question}}.
                    '''
                
                    directions = chatbot_instructions[domain]
                
                    answer = call_chatgpt(engineered_prompt, directions)
                
                    response = answer
                    # Display assistant response in chat message container
                    with st.chat_message("assistant"):
                        st.markdown(response)
                        with st.expander("See reference:"):
                            st.table(final_ref) 
                    # Add assistant response to chat history
                    st.session_state.messages.append({{"role": "assistant", "content": response}})
            """
            
            st.session_state.clear()
            st.session_state.success = (requirements, app)
            st.rerun()