Spaces:
Runtime error
Runtime error
File size: 26,235 Bytes
1cd8311 |
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 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 |
# ----------------------Importing libraries----------------------
import streamlit as st
from streamlit_pills import pills
import pandas as pd
import openai
# Imports for AgGrid
from st_aggrid import AgGrid, GridUpdateMode, JsCode
from st_aggrid.grid_options_builder import GridOptionsBuilder
# ----------------------Importing utils.py----------------------
# For Snowflake (from Tony's utils.py)
import io
from utils import (
connect_to_snowflake,
load_data_to_snowflake,
load_data_to_postgres,
connect_to_postgres,
)
# ----------------------Page config--------------------------------------
st.set_page_config(page_title="GPT3 Dataset Generator", page_icon="π€")
# ----------------------Sidebar section--------------------------------
# st.image(
# "Gifs/header.gif",
# )
st.image("Gifs/boat_new.gif")
c30, c31, c32 = st.columns([0.2, 0.1, 3])
with c30:
st.caption("")
st.image("openai.png", width=60)
with c32:
st.title("GPT3 Dataset Generator")
st.write(
"This app generates datasets using GPT3. It was created for the βοΈ Snowflake Snowvation Hackathon"
)
tabMain, tabInfo, tabTo_dos = st.tabs(["Main", "Info", "To-do's"])
with tabInfo:
st.write("")
st.write("")
st.subheader("π€ What is GPT-3?")
st.markdown(
"[GPT-3](https://en.wikipedia.org/wiki/GPT-3) is a large language generation model developed by [OpenAI](https://openai.com/) that can generate human-like text. It has a capacity of 175 billion parameters and is trained on a vast dataset of internet text. It can be used for tasks such as language translation, chatbot language generation, and content generation etc."
)
st.subheader("π What is Streamlit?")
st.markdown(
"[Streamlit](https://streamlit.io) is an open-source Python library that allows users to create interactive, web-based data visualization and machine learning applications without the need for extensive web development knowledge"
)
st.write("---")
st.subheader("π Resources")
st.markdown(
"""
- OpenAI
- [OpenAI Playground](https://beta.openai.com/playground)
- [OpenAI Documentation](https://beta.openai.com/docs)
- Streamlit
- [Documentation](https://docs.streamlit.io/)
- [Gallery](https://streamlit.io/gallery)
- [Cheat sheet](https://docs.streamlit.io/library/cheatsheet)
- [Book](https://www.amazon.com/dp/180056550X) (Getting Started with Streamlit for Data Science)
- Deploy your apps using [Streamlit Community Cloud](https://streamlit.io/cloud) in just a few clicks
"""
)
with tabTo_dos:
with st.expander("To-do", expanded=True):
st.write(
"""
- [p2] Currently, the results are displayed even if the submit button isn't pressed.
- [p2] There is still an issue with the index where the first element from the JSON is not being displayed.
- [Post Hackathon] To limit the number of API calls and costs, let's cap the maximum number - of results to 5. Alternatively, we can consider removing the free API key.
"""
)
st.write("")
with st.expander("Done", expanded=True):
st.write(
"""
- [p2] Check if the Json file is working
- [p2] On Github, remove any unused images and GIFs.
- [p1] Add that for postgress - localhost is required
- [p2] Rename the CSV and JSON as per the st-pills variable
- [p2] Change the color of the small arrow
- [p1] Adjust the size of the Gifs
- Add a streamlit badge in the `ReadMe` file
- Add the message "Please enter your API key or choose the `Free Key` option."
- Include a `ReadMe` file
- Add a section for the Snowflake credentials
- Remove password from the Python file
- Add screenshots to the `ReadMe` file
- Include forms in the snowflake postgres section
- Remove the hashed code in the Python file
- Include additional information in the 'info' tab
- p1] Fix the download issue by sorting it via session state
- [p1] Make the dataframe from this app editable
- Add more gifs to the app
- Change the color scheme to Snowflake Blue
- Include a section for Snowflake credentials
- Change the colors of the arrows, using this tool (https://lottiefiles.com/lottie-to-gif/convert)
- Try new prompts and implement the best ones
- Add a config file for the color scheme
- Include an option menu using this tool (https://github.com/victoryhb/streamlit-option-menu)
- Display a message when the API key is not provided
- Fix the arrow and rearrange the layout for the API key message
- Check and improve the quality of the prompt output
- Send the app to Tony and upload it to GitHub
- Re-arrange the data on the sidebar
- Change the colors of both gifs to match the overall color scheme
- Add context about the app being part of the snowvation project
- Add a button to convert the data to JSON format
- Include the Snowflake logo
- Add a submit button to block API calls unless pressed
- Add a tab with additional information
- Resize the columns in the st.form section
- Add the ability to add the dataset to Snowflake
- Create a section with pills, showcasing examples
- Change the main emoji
- Change the emoji in the tab (page_icon)
- [INFO] Sort out the issue with credits
"""
)
st.write("")
with st.expander("Not needed", expanded=True):
st.write(
"""
- Check index issue in readcsv (not an issue as I've changed the script)
- Add the mouse gif (doesn't fit)
- Ask Lukas - automatically resize the columns of a DataFrame
"""
)
st.write("")
st.write("")
st.write("")
st.write("")
with tabMain:
key_choice = st.sidebar.radio(
"",
(
"Your Key",
"Free Key (capped)",
),
horizontal=True,
)
if key_choice == "Your Key":
API_Key = st.sidebar.text_input(
"First, enter your OpenAI API key", type="password"
)
elif key_choice == "Free Key (capped)":
API_Key = st.secrets["API_KEY"]
image_arrow = st.sidebar.image(
"Gifs/blue_grey_arrow.gif",
)
if key_choice == "Free Key (capped)":
image_arrow.empty()
else:
st.write("")
st.sidebar.caption(
"No OpenAI API key? Get yours [here!](https://openai.com/blog/api-no-waitlist/)"
)
pass
st.write("")
c30, c31, c32 = st.columns([0.2, 0.1, 3])
st.subheader("β Build your dataset")
example = pills(
"",
[
"Sci-fi Movies",
"Animals",
"Pop Songs",
"POTUS's Twitter",
"Blank",
],
[
"πΏ",
"π",
"π΅",
"πΊπΈ",
"π»",
],
label_visibility="collapsed",
)
if "counter" not in st.session_state:
st.session_state.counter = 0
def increment():
st.session_state.counter += 1
if example == "Sci-fi Movies":
with st.form("my_form"):
text_input = st.text_input(
"What is the topic of your dataset?", value="Sci-fi movies"
)
col1, col2, col3 = st.columns(3, gap="small")
with col1:
column_01 = st.text_input("1st column", value="Title")
with col2:
column_02 = st.text_input("2nd column", value="Year")
with col3:
column_03 = st.text_input("3rd column", value="PG rating")
col1, col2 = st.columns(2, gap="medium")
with col1:
number = st.number_input(
"How many rows do you want?",
value=5,
min_value=1,
max_value=20,
step=5,
help="The maximum number of rows is 20.",
)
with col2:
engine = st.radio(
"GPT3 engine",
(
"Davinci",
"Curie",
"Babbage",
),
horizontal=True,
help="Davinci is the most powerful engine, but it's also the slowest. Curie is the fastest, but it's also the least powerful. Babbage is somewhere in the middle.",
)
if engine == "Davinci":
engine = "davinci-instruct-beta-v3"
elif engine == "Curie":
engine = "curie-instruct-beta-v2"
elif engine == "Babbage":
engine = "babbage-instruct-beta"
st.write("")
submitted = st.form_submit_button("Build my dataset! β¨", on_click=increment)
elif example == "Animals":
with st.form("my_form"):
text_input = st.text_input(
"What is the topic of your dataset?", value="Fastest animals on earth"
)
col1, col2, col3 = st.columns(3, gap="small")
with col1:
column_01 = st.text_input("1st column", value="Animal")
with col2:
column_02 = st.text_input("2nd column", value="Speed")
with col3:
column_03 = st.text_input("3rd column", value="Weight")
col1, col2 = st.columns(2, gap="medium")
with col1:
number = st.number_input(
"How many rows do you want?",
value=5,
min_value=1,
max_value=20,
step=5,
help="The maximum number of rows is 50.",
)
with col2:
engine = st.radio(
"GPT3 engine",
(
"Davinci",
"Curie",
"Babbage",
),
horizontal=True,
help="Davinci is the most powerful engine, but it's also the slowest. Curie is the fastest, but it's also the least powerful. Babbage is somewhere in the middle.",
)
if engine == "Davinci":
engine = "davinci-instruct-beta-v3"
elif engine == "Curie":
engine = "curie-instruct-beta-v2"
elif engine == "Babbage":
engine = "babbage-instruct-beta"
st.write("")
submitted = st.form_submit_button("Build my dataset! β¨", on_click=increment)
elif example == "Stocks":
with st.form("my_form"):
text_input = st.text_input(
"What is the topic of your dataset?", value="Stocks"
)
col1, col2, col3 = st.columns(3, gap="small")
with col1:
column_01 = st.text_input("1st column", value="Ticker")
with col2:
column_02 = st.text_input("2nd column", value="Price")
with col3:
column_03 = st.text_input("3rd column", value="Exchange")
col1, col2 = st.columns(2, gap="medium")
with col1:
number = st.number_input(
"How many rows do you want?",
value=5,
min_value=1,
max_value=20,
step=5,
help="The maximum number of rows is 50.",
)
with col2:
engine = st.radio(
"GPT3 engine",
(
"Davinci",
"Curie",
"Babbage",
),
horizontal=True,
help="Davinci is the most powerful engine, but it's also the slowest. Curie is the fastest, but it's also the least powerful. Babbage is somewhere in the middle.",
)
if engine == "Davinci":
engine = "davinci-instruct-beta-v3"
elif engine == "Curie":
engine = "curie-instruct-beta-v2"
elif engine == "Babbage":
engine = "babbage-instruct-beta"
st.write("")
submitted = st.form_submit_button("Build my dataset! β¨", on_click=increment)
elif example == "POTUS's Twitter":
with st.form("my_form"):
text_input = st.text_input(
"What is the topic of your dataset?", value="POTUS's Twitter accounts"
)
col1, col2, col3 = st.columns(3, gap="small")
with col1:
column_01 = st.text_input("1st column", value="Name")
with col2:
column_02 = st.text_input("2nd column", value="Twitter handle")
with col3:
column_03 = st.text_input("3rd column", value="# of followers")
col1, col2 = st.columns(2, gap="medium")
with col1:
number = st.number_input(
"How many rows do you want?",
value=5,
min_value=1,
max_value=20,
step=5,
help="The maximum number of rows is 50.",
)
with col2:
engine = st.radio(
"GPT3 engine",
(
"Davinci",
"Curie",
"Babbage",
),
horizontal=True,
help="Davinci is the most powerful engine, but it's also the slowest. Curie is the fastest, but it's also the least powerful. Babbage is somewhere in the middle.",
)
if engine == "Davinci":
engine = "davinci-instruct-beta-v3"
elif engine == "Curie":
engine = "curie-instruct-beta-v2"
elif engine == "Babbage":
engine = "babbage-instruct-beta"
st.write("")
submitted = st.form_submit_button("Build my dataset! β¨")
elif example == "Pop Songs":
with st.form("my_form"):
text_input = st.text_input(
"What is the topic of your dataset?",
value="Most famous songs of all time",
)
col1, col2, col3 = st.columns(3, gap="small")
with col1:
column_01 = st.text_input("1st column", value="Song")
with col2:
column_02 = st.text_input("2nd column", value="Artist")
with col3:
column_03 = st.text_input("3rd column", value="Genre")
col1, col2 = st.columns(2, gap="medium")
with col1:
number = st.number_input(
"How many rows do you want?",
value=5,
min_value=1,
max_value=20,
step=5,
help="The maximum number of rows is 50.",
)
with col2:
engine = st.radio(
"GPT3 engine",
(
"Davinci",
"Curie",
"Babbage",
),
horizontal=True,
help="Davinci is the most powerful engine, but it's also the slowest. Curie is the fastest, but it's also the least powerful. Babbage is somewhere in the middle.",
)
if engine == "Davinci":
engine = "davinci-instruct-beta-v3"
elif engine == "Curie":
engine = "curie-instruct-beta-v2"
elif engine == "Babbage":
engine = "babbage-instruct-beta"
st.write("")
submitted = st.form_submit_button("Build my dataset! β¨")
elif example == "Blank":
with st.form("my_form"):
text_input = st.text_input("What is the topic of your dataset?", value="")
col1, col2, col3 = st.columns(3, gap="small")
with col1:
column_01 = st.text_input("1st column", value="")
with col2:
column_02 = st.text_input("2nd column", value="")
with col3:
column_03 = st.text_input("3rd column", value="")
col1, col2 = st.columns(2, gap="medium")
with col1:
number = st.number_input(
"How many rows do you want?",
value=5,
min_value=1,
max_value=20,
step=5,
help="The maximum number of rows is 50.",
)
with col2:
engine = st.radio(
"GPT3 engine",
(
"Davinci",
"Curie",
"Babbage",
),
horizontal=True,
help="Davinci is the most powerful engine, but it's also the slowest. Curie is the fastest, but it's also the least powerful. Babbage is somewhere in the middle.",
)
if engine == "Davinci":
engine = "davinci-instruct-beta-v3"
elif engine == "Curie":
engine = "curie-instruct-beta-v2"
elif engine == "Babbage":
engine = "babbage-instruct-beta"
st.write("")
submitted = st.form_submit_button("Build my dataset! β¨")
# ----------------------API key section----------------------------------
number = number + 1
if not API_Key and not submitted:
st.stop()
if not API_Key and submitted:
st.info("Please enter your API key or choose the `Free Key` option.")
st.stop()
if st.session_state.counter >= 100:
pass
# ----------------------API key section----------------------------------
if not submitted and st.session_state.counter == 0:
c30, c31, c32 = st.columns([1, 0.01, 4])
with c30:
st.image("Gifs/arrow_small_new.gif")
st.caption("")
with c32:
st.caption("")
st.caption("")
st.info(
"Enter your dataset's criteria and click the button to generate it."
)
st.stop()
elif st.session_state.counter > 0:
c30, c31, c32 = st.columns([1, 0.9, 3])
openai.api_key = API_Key
# ----------------------API call section----------------------------------
response = openai.Completion.create(
model=engine,
prompt=f"Please provide a list of the top {number} {text_input} along with the following information in a three-column spreadsheet: {column_01}, {column_02}, and {column_03}. The columns should be labeled as follows: {column_01} | {column_02} | {column_03}",
temperature=0.5,
max_tokens=1707,
top_p=1,
best_of=2,
frequency_penalty=0,
presence_penalty=0,
)
st.write("___")
st.subheader("β‘ Check the results")
with st.expander("See the API Json output"):
response
output_code = response["choices"][0]["text"]
# ----------------------Dataframe section----------------------------------
# create pandas DataFrame from string
df = pd.read_csv(io.StringIO(output_code), sep="|")
# get the number of columns in the dataframe
num_columns = len(df.columns)
# create a list of column names
column_names = ["Column {}".format(i) for i in range(1, num_columns + 1)]
# add the header to the dataframe
df.columns = column_names
# specify the mapping of old column names to new column names
column_mapping = {
"Column 1": column_01,
"Column 2": column_02,
"Column 3": column_03,
}
# rename the columns of the dataframe
df = df.rename(columns=column_mapping)
st.write("")
# ----------------------AgGrid section----------------------------------
gd = GridOptionsBuilder.from_dataframe(df)
gd.configure_pagination(enabled=True)
gd.configure_default_column(editable=True, groupable=True)
gd.configure_selection(selection_mode="multiple")
gridoptions = gd.build()
grid_table = AgGrid(
df,
gridOptions=gridoptions,
update_mode=GridUpdateMode.SELECTION_CHANGED,
theme="material",
)
# df
# ----------------------Download section--------------------------------------
c30, c31, c32, c33 = st.columns([1, 0.01, 1, 2.5])
with c30:
@st.cache
def convert_df(df):
return df.to_csv().encode("utf-8")
csv = convert_df(df)
st.download_button(
label="Download CSV",
data=csv,
file_name=f"{example} dataset .csv",
mime="text/csv",
)
with c32:
json_string = df.to_json(orient="records")
st.download_button(
label="Download JSON",
data=json_string,
file_name="data_set_sample.json",
mime="text/csv",
)
st.write("___")
st.subheader("β’ Load data to Databases")
# Data to load to database(s)
# df = pd.read_csv("philox-testset-1.csv")
# Get user input for data storage option
storage_option = st.radio(
"Select data storage option:",
(
"Snowflake",
"PostgreSQL",
),
horizontal=True,
)
# Get user input for data storage option
# Snowflake = st.selectbox(
# "Select data storage option:", ["Snowflake", "Snowflake"]
# )
@st.cache(allow_output_mutation=True)
def reset_form_fields():
user = ""
password = ""
account = ""
warehouse = ""
database = ""
schema = ""
table = ""
host = ""
port = ""
if storage_option == "Snowflake":
st.subheader("`Enter Snowflake Credentials`π")
# Get user input for Snowflake credentials
with st.form("my_form_db"):
col1, col2 = st.columns(2, gap="small")
with col1:
user = st.text_input("Username:", value="TONY")
with col2:
password = st.text_input("Password:", type="password")
with col1:
account = st.text_input("Account:", value="jn27194.us-east4.gcp")
with col2:
warehouse = st.text_input("Warehouse:", value="NAH")
with col1:
database = st.text_input("Database:", value="SNOWVATION")
with col2:
schema = st.text_input("Schema:", value="PUBLIC")
table = st.text_input("Table:")
st.write("")
submitted = st.form_submit_button("Load to Snowflake")
# Load the data to Snowflake
if submitted:
# if st.button("Load data to Snowflake"):
if (
user
and password
and account
and warehouse
and database
and schema
and table
):
conn = connect_to_snowflake(
username=user,
password=password,
account=account,
warehouse=warehouse,
database=database,
schema=schema,
)
if conn:
load_data_to_snowflake(df, conn, table)
else:
st.warning("Please enter all Snowflake credentials")
elif storage_option == "PostgreSQL":
st.subheader("`Enter PostgreSQL Credentials`π")
st.error("Localhost only")
# Get user input for PostgreSQL credentials
with st.form("my_form_db"):
col1, col2 = st.columns(2, gap="small")
with col1:
user = st.text_input("Username:", value="postgres")
with col2:
password = st.text_input("Password:", type="password")
with col1:
host = st.selectbox("Host:", ["localhost", "other"])
if host == "other":
host = st.text_input("Enter host:")
with col2:
port = st.text_input("Port:", value="5432")
with col1:
database = st.text_input("Database:", value="snowvation")
with col2:
table = st.text_input("Table:")
st.write("")
submitted = st.form_submit_button("Load to PostgreSQL")
# Load the data to PostgreSQL
# if st.button("Load data to PostgreSQL"):
if submitted:
if user and password and host and port and database and table:
conn = connect_to_postgres(
username=user,
password=password,
host=host,
port=port,
database=database,
)
if conn:
load_data_to_postgres(df, conn, table)
else:
st.warning("Please enter all PostgreSQL credentials and table name")
# Reset form fields when storage_option changes
reset_form_fields() |