Spaces:
Sleeping
Sleeping
File size: 27,662 Bytes
32a98d6 f6f33c3 b1a0926 21c729c 8b6d1f3 f6f33c3 a289e51 f6f33c3 740fc36 f6f33c3 f932e1e eda36d5 f6f33c3 f932e1e f6f33c3 32a98d6 0809a99 15d3c1c 0809a99 15d3c1c 0809a99 6117d34 15d3c1c 3916947 e697bbd a4edf48 15d3c1c 91c5e04 15d3c1c 91c5e04 42a0968 413edcd 15d3c1c b51a3ec 9e9d1b6 b51a3ec 15d3c1c a44d2b6 287cf0a a44d2b6 0f0f2e6 e697bbd 3916947 b3e62ab 9a2cd44 2c9f375 b3e62ab 2cc9098 9a2cd44 2cc9098 5667d6e 2cc9098 5667d6e 2cc9098 5667d6e e7dc910 9a894ab e6ffb3a 5040d0d d8feb80 5040d0d d8feb80 5040d0d d8feb80 5040d0d 287cf0a 7ca6340 5040d0d a2cf973 287cf0a 5040d0d 9a894ab a2cf973 9a894ab 15fb029 15d3c1c a44d2b6 287cf0a a44d2b6 6117d34 4f4ce1a 6117d34 7ca6340 3866b6b 7ca6340 287cf0a 7ca6340 287cf0a 7ca6340 6117d34 a2cf973 6117d34 4d67d8b 15d3c1c 1a8c55e 15d3c1c f41898f f9462af f41898f 45c6f19 f41898f 45c6f19 f41898f 45c6f19 f41898f c7a1920 7ce74a6 615a4cf 7ce74a6 957a53b 922a732 957a53b 7ce74a6 bf0375b c7a1920 f41898f 45c6f19 f41898f c7a1920 721d199 3810ff6 721d199 8314836 721d199 eb0b54f 721d199 c7a1920 f41898f 45c6f19 107aa83 f41898f 45c6f19 f41898f 45c6f19 f41898f 45c6f19 f41898f 45c6f19 f41898f 05238dd f41898f 05238dd f41898f 1e18d82 f41898f 107aa83 f41898f 0868789 f41898f 1e18d82 4999806 f41898f 107aa83 f41898f 05238dd f41898f 107aa83 f41898f 4999806 f41898f 4999806 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 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
import streamlit as st
import pandas as pd
from openai import OpenAI
from PyPDF2 import PdfReader
from PIL import Image
# 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) Scrape PDFs",
"2) Create CSVs",
"3) Merge CSVs",
"4) Upload Datasets",
"5) Create Chatbot"
]
# Sidebar dropdown for selecting the application
selected_app = st.sidebar.selectbox("Select Step (1-5)", 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) Scrape PDFs":
st.markdown("### On this page, you'll scape the information that you want your chatbot to know")
st.divider()
st.write("1. Go to your organizations webpage")
image = Image.open('Example1.png')
st.image(image, caption="Example for Step 1",use_column_width=True)
st.divider()
st.write("2. Choose an section in the webpage")
image = Image.open('Example2.png')
st.image(image, caption="Example for Step 2",use_column_width=True)
st.divider()
st.write("3. Copy all text on the page")
image = Image.open('Example3.png')
st.image(image, caption="Example for Step 3",use_column_width=True)
st.divider()
st.write("4. Open a new google doc")
image = Image.open('Example4.png')
st.image(image, caption="Example for Step 4",use_column_width=True)
st.divider()
st.write("5. Paste all text into google doc")
image = Image.open('Example5.png')
st.image(image, caption="Example for Step 5",use_column_width=True)
st.divider()
st.write("6. Download the googel doc as a PDF")
image = Image.open('Example6.png')
st.image(image, caption="Example for Step 6",use_column_width=True)
st.divider()
st.write("7. Repeat the steps for all sections in your webpage")
if selected_app == "2) 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:
st.markdown("### On this page, you'll convert your text into potential questions that your chatbot may be asked and their corresponding answers.")
st.divider()
st.write("1. Upload your PDFs here")
uploaded_files = st.file_uploader("", type="pdf", accept_multiple_files=True)
with st.expander("Explain"):
st.write("You can upload more than one PDF at a time, but don't do too many at once.")
st.divider()
st.write("2. Enter your OpenAI API key")
openai_api_key = st.text_input("", type="password")
with st.expander("Explain"):
st.write("Your OpenAI API key allows you to use ChatGPT, the basis of your chatbot. Don't have one? Here's how to get one:")
st.markdown("""
1. Go to [OpenAI](https://openai.com/) --> Products --> API --> API Login.
2. Log in with Google, then click 'API'.
3. In the top right, click Settings, then Billing.
4. Add payment details and add money to the account (evan a small investment lasts a long time).
5. Click Dashboard in the top right, then API keys on the left sidebar.
6. In the top right, click 'Create new secret key' and save the key to a secure place (you won't have access later)
""")
st.write("If you're a nonprofit in need of funding, reach out to me at [email protected] to request a key.")
st.divider()
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, 1)
df = pd.DataFrame(textify_output)
df.columns = ['context']
question_protocol = "Write questions based on the text"
df['questions'] = df.apply(lambda row: get_questions(row['context'], question_protocol), axis=1)
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 == "3) 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 == "4) Upload Datasets":
st.markdown("Go to this [google colab link](https://colab.research.google.com/drive/1eCpk9HUoCKZb--tiNyQSHFW2ojoaA35m) to get started")
if selected_app == "5) 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.\n\nExample Questions\n\n--> What is the purpose of Youth Spirit Artworks?\n\n--> Who created YSA?\n\n--> What is the Advisory Board for Youth Spirit Artworks?\n\n--> 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()
missing_info = []
for i in range(len(domain_info)):
if domain_info[i]['link'] == "":
missing_info.append(f"link to domain {i+1}")
if domain_info[i]['name'] == "":
missing_info.append(f"name for domain {i+1}")
if domain_info[i]['purpose'] == "":
missing_info.append(f"purpose for domain {i+1}")
if domain_info[i]['instructions'] == "":
missing_info.append(f"instructions for domain {i+1}")
if missing_info:
error = "Missing Info: "
for info in missing_info:
error += (info + ', ')
st.session_state.clear()
st.session_state.error = error
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
domain_info = {domain_info}
st.sidebar.markdown('''This is a chatbot to help you learn more about {organization_name}''')
domain = st.sidebar.selectbox("Select a topic", [d["name"] for d in domain_info])
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 = ""
for d in domain_info:
if domain == d['name']:
dataset = load_dataset(d['link'])
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 = {{}}
for d in domain_info:
init_messages[d['name']] = d['purpose']
chatbot_instructions = {{}}
for d in domain_info:
chatbot_instructions[d['name']] = d['instructions']
# 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() |