Spaces:
Build error
Build error
File size: 31,384 Bytes
1a08523 bd9fae2 e375940 94a67ea 8cd1f1e 94a67ea bd9fae2 c5e4524 9c49e99 bd9fae2 6a79fd2 c5e4524 5482130 c5e4524 244a3e0 c5e4524 a7fc504 bd9fae2 c5e4524 1532bd6 bd9fae2 c5e4524 1a08523 8b61059 c5e4524 6627aee 0175cb6 6627aee 0175cb6 6627aee 1a08523 4b19adc 1a08523 9c49e99 c5e4524 f9da573 e375940 f9da573 1a08523 bd9fae2 f9da573 c5e4524 8cd1f1e ff5dcc7 a7b0635 9975133 b19bb41 8cd1f1e 9975133 9c49e99 0175cb6 bd9fae2 0f10e25 1a08523 4b19adc 0175cb6 26a2734 bd9fae2 244a3e0 0175cb6 9c49e99 a7fc504 9975133 1a08523 6627aee 4b19adc c5f41e6 a7fc504 1a08523 c5f41e6 1a08523 6627aee 1a08523 4b19adc 1a08523 4b19adc 1a08523 4b19adc 1a08523 4b19adc 1a08523 4b19adc 1a08523 c5f41e6 9975133 1a08523 4b19adc 1a08523 8cd1f1e 1532bd6 9975133 8cd1f1e 1a08523 9975133 8cd1f1e e375940 8cd1f1e e375940 8cd1f1e aa91fc5 1532bd6 aa91fc5 bd9fae2 1532bd6 bd9fae2 1532bd6 aa91fc5 e375940 8cd1f1e 9975133 1a08523 4b19adc 1a08523 9975133 e514fa8 26a2734 fbd690d 1a08523 1532bd6 1a08523 18468cb 1a08523 bd9fae2 1a08523 a7fc504 bd9fae2 1a08523 e375940 1a08523 bb343a5 1a08523 176e1ef bd9fae2 1a08523 e375940 1a08523 e514fa8 1a08523 4b19adc 1532bd6 4b19adc 18468cb 4b19adc 176e1ef bd9fae2 4b19adc 1a08523 4b19adc bb343a5 4b19adc 176e1ef bd9fae2 4b19adc 1a08523 4b19adc 1a08523 1532bd6 4b19adc 18468cb 4b19adc 176e1ef bd9fae2 4b19adc 176e1ef bd9fae2 4b19adc 1a08523 4b19adc bb343a5 4b19adc 176e1ef bd9fae2 4b19adc 8cd1f1e 4b19adc 176e1ef bd9fae2 4b19adc 1a08523 4b19adc 1a08523 4b19adc 8cd1f1e 9975133 e375940 816093e e375940 9975133 1a08523 9c49e99 1a08523 9c49e99 8cd1f1e 0175cb6 8cd1f1e 9975133 9c49e99 816093e 9c49e99 8cd1f1e 6627aee 8cd1f1e 9975133 6627aee 0175cb6 6627aee 9c49e99 816093e 9c49e99 6627aee 0175cb6 6627aee 0175cb6 6627aee 0175cb6 6627aee 0175cb6 6627aee 0175cb6 6627aee 816093e 6627aee 9c49e99 bd9fae2 8cd1f1e f9da573 1a08523 4b19adc 1a08523 4b19adc 1532bd6 4b19adc 1532bd6 4b19adc 1532bd6 4b19adc 1532bd6 4b19adc 1532bd6 4b19adc 1532bd6 4b19adc 1532bd6 4b19adc 1532bd6 4b19adc |
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 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 |
import re
import numpy as np
import openai
import streamlit_scrollable_textbox as stx
import pinecone
import streamlit as st
st.set_page_config(layout="wide") # isort: split
from utils import nltkmodules
from utils.entity_extraction import (
clean_entities,
extract_keywords,
extract_quarter_year,
extract_ticker_spacy,
format_entities_flan_alpaca,
generate_alpaca_ner_prompt,
)
from utils.models import (
generate_entities_flan_alpaca_checkpoint,
generate_entities_flan_alpaca_inference_api,
generate_text_flan_t5,
get_alpaca_model,
get_data,
get_flan_alpaca_xl_model,
get_flan_t5_model,
get_instructor_embedding_model,
get_instructor_embedding_model_api,
get_bm25_model,
preprocess_text,
get_mpnet_embedding_model,
get_sgpt_embedding_model,
get_spacy_model,
get_splade_sparse_embedding_model,
get_t5_model,
gpt_turbo_model,
save_key,
)
from utils.prompts import (
generate_flant5_prompt_instruct_chunk_context,
generate_flant5_prompt_instruct_chunk_context_single,
generate_flant5_prompt_instruct_complete_context,
generate_flant5_prompt_summ_chunk_context,
generate_flant5_prompt_summ_chunk_context_single,
generate_gpt_j_two_shot_prompt_1,
generate_gpt_j_two_shot_prompt_2,
generate_gpt_prompt_alpaca,
generate_gpt_prompt_alpaca_multi_doc,
generate_gpt_prompt_alpaca_multi_doc_multi_company,
generate_gpt_prompt_original,
generate_multi_doc_context,
get_context_list_prompt,
)
from utils.retriever import (
format_query,
query_pinecone,
query_pinecone_sparse,
sentence_id_combine,
text_lookup,
year_quarter_range,
get_bm25_search_hits,
)
from utils.transcript_retrieval import retrieve_transcript
from utils.vector_index import (
create_dense_embeddings,
create_sparse_embeddings,
hybrid_score_norm,
)
st.title("Question Answering on Earnings Call Transcripts")
st.write(
"The app uses the quarterly earnings call transcripts for 10 companies (Apple, AMD, Amazon, Cisco, Google, Microsoft, Nvidia, ASML, Intel, Micron) for the years 2016 to 2020."
)
col1, col2 = st.columns([3, 3], gap="medium")
with st.sidebar:
use_bm25 = st.checkbox("Use BM25 for filtering results")
ner_choice = st.selectbox("Select NER Model", ["Spacy", "Alpaca"])
document_type = st.selectbox(
"Select Query Type", ["Single-Document", "Multi-Document"]
)
if document_type == "Multi-Document":
multi_company_choice = st.selectbox(
"Select Company Query Type",
["Single-Company", "Compare Companies"],
)
data = get_data()
corpus, bm25 = get_bm25_model(data)
tokenized_query = preprocess_text(query_text).split()
sparse_scores = np.argsort(bm25.get_scores(tokenized_query), axis=0)[::-1]
indices_hits = get_bm25_search_hits(corpus, sparse_scores, 50)
if use_bm25 == True:
indices = indices_hits
else:
indices = None
if ner_choice == "Spacy":
ner_model = get_spacy_model()
alpaca_model = get_alpaca_model()
with col1:
st.subheader("Question")
if document_type == "Single-Document":
query_text = st.text_area(
"Input Query",
value="What was discussed regarding Wearables revenue performance?",
)
else:
if multi_company_choice == "Single-Company":
query_text = st.text_area(
"Input Query",
value="What was the reported revenue for Wearables over the last 2 years?",
)
else:
query_text = st.text_area(
"Input Query",
value="How was AAPL's capex spend compared to GOOGL?",
)
# Extract keywords from query
keywords = extract_keywords(query_text, alpaca_model)
years_choice = ["2020", "2019", "2018", "2017", "2016", "All"]
quarters_choice = ["Q1", "Q2", "Q3", "Q4", "All"]
ticker_choice = [
"AAPL",
"CSCO",
"MSFT",
"ASML",
"NVDA",
"GOOGL",
"MU",
"INTC",
"AMZN",
"AMD",
]
if document_type == "Single-Document":
if ner_choice == "Alpaca":
ner_prompt = generate_alpaca_ner_prompt(query_text)
entity_text = generate_entities_flan_alpaca_inference_api(ner_prompt)
company_ent, quarter_ent, year_ent = format_entities_flan_alpaca(
entity_text
)
else:
company_ent = extract_ticker_spacy(query_text, ner_model)
quarter_ent, year_ent = extract_quarter_year(query_text)
ticker_index, quarter_index, year_index = clean_entities(
company_ent, quarter_ent, year_ent
)
with col1:
# Hardcoding the defaults for a question without metadata
if (
query_text
== "What was discussed regarding Wearables revenue performance?"
):
year = st.selectbox("Year", years_choice)
quarter = st.selectbox("Quarter", quarters_choice)
ticker = st.selectbox("Company", ticker_choice)
else:
year = st.selectbox("Year", years_choice, index=year_index)
quarter = st.selectbox(
"Quarter", quarters_choice, index=quarter_index
)
ticker = st.selectbox("Company", ticker_choice, ticker_index)
participant_type = st.selectbox(
"Speaker", ["Company Speaker", "Analyst"]
)
else:
# Multi-Document Case
with col1:
# Single Company Summary
if multi_company_choice == "Single-Company":
# Hardcoding the defaults for a question without metadata
if (
query_text
== "What was the reported revenue for Wearables over the last 2 years?"
):
start_year = st.selectbox("Start Year", years_choice, index=2)
start_quarter = st.selectbox(
"Start Quarter", quarters_choice, index=0
)
end_year = st.selectbox("End Year", years_choice, index=0)
end_quarter = st.selectbox(
"End Quarter", quarters_choice, index=0
)
ticker = st.selectbox("Company", ticker_choice, index=0)
else:
start_year = st.selectbox("Start Year", years_choice, index=2)
start_quarter = st.selectbox(
"Start Quarter", quarters_choice, index=0
)
end_year = st.selectbox("End Year", years_choice, index=0)
end_quarter = st.selectbox(
"End Quarter", quarters_choice, index=0
)
ticker = st.selectbox("Company", ticker_choice, index=0)
# Single Company Summary
if multi_company_choice == "Compare Companies":
# Hardcoding the defaults for a question without metadata
if query_text == "How was AAPL's capex spend compared to GOOGL?":
start_year = st.selectbox("Start Year", years_choice, index=1)
start_quarter = st.selectbox(
"Start Quarter", quarters_choice, index=0
)
end_year = st.selectbox("End Year", years_choice, index=0)
end_quarter = st.selectbox(
"End Quarter", quarters_choice, index=0
)
ticker_first = st.selectbox(
"First Company", ticker_choice, index=0
)
ticker_second = st.selectbox(
"Second Company", ticker_choice, index=5
)
else:
start_year = st.selectbox("Start Year", years_choice, index=2)
start_quarter = st.selectbox(
"Start Quarter", quarters_choice, index=0
)
end_year = st.selectbox("End Year", years_choice, index=0)
end_quarter = st.selectbox(
"End Quarter", quarters_choice, index=0
)
ticker_first = st.selectbox(
"First Company", ticker_choice, index=0
)
ticker_second = st.selectbox(
"Second Company", ticker_choice, index=1
)
participant_type = st.selectbox(
"Speaker", ["Company Speaker", "Analyst"]
)
with st.sidebar:
st.subheader("Select Options:")
if document_type == "Single-Document":
num_results = int(
st.number_input("Number of Results to query", 1, 15, value=5)
)
else:
num_results = int(
st.number_input("Number of Results to query", 1, 15, value=4)
)
# Choose encoder model
encoder_models_choice = [
"MPNET",
"Instructor",
"Hybrid Instructor - SPLADE",
"SGPT",
"Hybrid MPNET - SPLADE",
]
with st.sidebar:
encoder_model = st.selectbox("Select Encoder Model", encoder_models_choice)
# Choose decoder model
# Restricting multi-document to only GPT-3
if document_type == "Single-Document":
decoder_models_choice = ["GPT-3.5 Turbo", "T5", "FLAN-T5", "GPT-J"]
else:
decoder_models_choice = ["GPT-3.5 Turbo"]
with st.sidebar:
decoder_model = st.selectbox("Select Decoder Model", decoder_models_choice)
if encoder_model == "MPNET":
# Connect to pinecone environment
pinecone.init(
api_key=st.secrets["pinecone_mpnet"], environment="us-east1-gcp"
)
pinecone_index_name = "week2-all-mpnet-base"
pinecone_index = pinecone.Index(pinecone_index_name)
retriever_model = get_mpnet_embedding_model()
elif encoder_model == "SGPT":
# Connect to pinecone environment
pinecone.init(
api_key=st.secrets["pinecone_sgpt"], environment="us-east1-gcp"
)
pinecone_index_name = "week2-sgpt-125m"
pinecone_index = pinecone.Index(pinecone_index_name)
retriever_model = get_sgpt_embedding_model()
elif encoder_model == "Instructor":
# Connect to pinecone environment
pinecone.init(
api_key=st.secrets["pinecone_instructor"],
environment="us-west4-gcp-free",
)
pinecone_index_name = "week13-instructor-xl"
pinecone_index = pinecone.Index(pinecone_index_name)
retriever_model = get_instructor_embedding_model_api()
instruction = (
"Represent the financial question for retrieving supporting documents:"
)
elif encoder_model == "Hybrid Instructor - SPLADE":
# Connect to pinecone environment
pinecone.init(
api_key=st.secrets["pinecone_instructor_splade"],
environment="us-west4-gcp-free",
)
pinecone_index_name = "week13-splade-instructor-xl"
pinecone_index = pinecone.Index(pinecone_index_name)
retriever_model = get_instructor_embedding_model_api()
(
sparse_retriever_model,
sparse_retriever_tokenizer,
) = get_splade_sparse_embedding_model()
instruction = (
"Represent the financial question for retrieving supporting documents:"
)
elif encoder_model == "Hybrid MPNET - SPLADE":
pinecone.init(
api_key=st.secrets["pinecone_hybrid_splade_mpnet"],
environment="us-central1-gcp",
)
pinecone_index_name = "splade-mpnet"
pinecone_index = pinecone.Index(pinecone_index_name)
retriever_model = get_mpnet_embedding_model()
(
sparse_retriever_model,
sparse_retriever_tokenizer,
) = get_splade_sparse_embedding_model()
with st.sidebar:
if document_type == "Single-Document":
window = int(st.number_input("Sentence Window Size", 0, 10, value=1))
threshold = float(
st.number_input(
label="Similarity Score Threshold",
step=0.05,
format="%.2f",
value=0.25,
)
)
else:
window = int(st.number_input("Sentence Window Size", 0, 10, value=1))
threshold = float(
st.number_input(
label="Similarity Score Threshold",
step=0.05,
format="%.2f",
value=0.6,
)
)
if document_type == "Single-Document":
if encoder_model in ["Hybrid SGPT - SPLADE", "Hybrid Instructor - SPLADE"]:
if encoder_model == "Hybrid Instructor - SPLADE":
dense_query_embedding = create_dense_embeddings(
query_text, retriever_model, instruction
)
else:
dense_query_embedding = create_dense_embeddings(
query_text, retriever_model
)
sparse_query_embedding = create_sparse_embeddings(
query_text, sparse_retriever_model, sparse_retriever_tokenizer
)
dense_query_embedding, sparse_query_embedding = hybrid_score_norm(
dense_query_embedding, sparse_query_embedding, 0.3
)
query_results = query_pinecone_sparse(
dense_query_embedding,
sparse_query_embedding,
num_results,
pinecone_index,
year,
quarter,
ticker,
participant_type,
keywords,
indices,
threshold,
)
else:
if encoder_model == "Instructor":
dense_query_embedding = create_dense_embeddings(
query_text, retriever_model, instruction
)
else:
dense_query_embedding = create_dense_embeddings(
query_text, retriever_model
)
query_results = query_pinecone(
dense_query_embedding,
num_results,
pinecone_index,
year,
quarter,
ticker,
participant_type,
keywords,
indices,
threshold,
)
if threshold <= 0.90:
context_list = sentence_id_combine(data, query_results, lag=window)
else:
context_list = format_query(query_results)
else:
# Multi-Document Retreival
# Single Company
if multi_company_choice == "Single-Company":
if encoder_model in [
"Hybrid SGPT - SPLADE",
"Hybrid Instructor - SPLADE",
]:
if encoder_model == "Hybrid Instructor - SPLADE":
dense_query_embedding = create_dense_embeddings(
query_text, retriever_model, instruction
)
else:
dense_query_embedding = create_dense_embeddings(
query_text, retriever_model
)
sparse_query_embedding = create_sparse_embeddings(
query_text, sparse_retriever_model, sparse_retriever_tokenizer
)
dense_query_embedding, sparse_query_embedding = hybrid_score_norm(
dense_query_embedding, sparse_query_embedding, 0.3
)
year_quarter_list = year_quarter_range(
start_quarter, start_year, end_quarter, end_year
)
context_group = []
for year, quarter in year_quarter_list:
query_results = query_pinecone_sparse(
dense_query_embedding,
sparse_query_embedding,
num_results,
pinecone_index,
year,
quarter,
ticker,
participant_type,
keywords,
indices,
threshold,
)
results_list = sentence_id_combine(
data, query_results, lag=window
)
context_group.append((results_list, year, quarter, ticker))
else:
if encoder_model == "Instructor":
dense_query_embedding = create_dense_embeddings(
query_text, retriever_model, instruction
)
else:
dense_query_embedding = create_dense_embeddings(
query_text, retriever_model
)
year_quarter_list = year_quarter_range(
start_quarter, start_year, end_quarter, end_year
)
context_group = []
for year, quarter in year_quarter_list:
query_results = query_pinecone(
dense_query_embedding,
num_results,
pinecone_index,
year,
quarter,
ticker,
participant_type,
keywords,
indices,
threshold,
)
results_list = sentence_id_combine(
data, query_results, lag=window
)
context_group.append((results_list, year, quarter, ticker))
multi_doc_context = generate_multi_doc_context(context_group)
# Companies Comparison
else:
if encoder_model in [
"Hybrid SGPT - SPLADE",
"Hybrid Instructor - SPLADE",
]:
if encoder_model == "Hybrid Instructor - SPLADE":
dense_query_embedding = create_dense_embeddings(
query_text, retriever_model, instruction
)
else:
dense_query_embedding = create_dense_embeddings(
query_text, retriever_model
)
sparse_query_embedding = create_sparse_embeddings(
query_text, sparse_retriever_model, sparse_retriever_tokenizer
)
dense_query_embedding, sparse_query_embedding = hybrid_score_norm(
dense_query_embedding, sparse_query_embedding, 0.3
)
year_quarter_list = year_quarter_range(
start_quarter, start_year, end_quarter, end_year
)
# First Company Context
context_group_first = []
for year, quarter in year_quarter_list:
query_results = query_pinecone_sparse(
dense_query_embedding,
sparse_query_embedding,
num_results,
pinecone_index,
year,
quarter,
ticker_first,
participant_type,
keywords,
indices,
threshold,
)
results_list = sentence_id_combine(
data, query_results, lag=window
)
context_group_first.append(
(results_list, year, quarter, ticker_first)
)
# Second Company Context
context_group_second = []
for year, quarter in year_quarter_list:
query_results = query_pinecone_sparse(
dense_query_embedding,
sparse_query_embedding,
num_results,
pinecone_index,
year,
quarter,
ticker_second,
participant_type,
keywords,
indices,
threshold,
)
results_list = sentence_id_combine(
data, query_results, lag=window
)
context_group_second.append(
(results_list, year, quarter, ticker_second)
)
else:
if encoder_model == "Instructor":
dense_query_embedding = create_dense_embeddings(
query_text, retriever_model, instruction
)
else:
dense_query_embedding = create_dense_embeddings(
query_text, retriever_model
)
year_quarter_list = year_quarter_range(
start_quarter, start_year, end_quarter, end_year
)
# First Company Context
context_group_first = []
for year, quarter in year_quarter_list:
query_results = query_pinecone(
dense_query_embedding,
num_results,
pinecone_index,
year,
quarter,
ticker_first,
participant_type,
keywords,
indices,
threshold,
)
results_list = sentence_id_combine(
data, query_results, lag=window
)
context_group_first.append(
(results_list, year, quarter, ticker_first)
)
# Second Company Context
context_group_second = []
for year, quarter in year_quarter_list:
query_results = query_pinecone(
dense_query_embedding,
num_results,
pinecone_index,
year,
quarter,
ticker_second,
participant_type,
keywords,
indices,
threshold,
)
results_list = sentence_id_combine(
data, query_results, lag=window
)
context_group_second.append(
(results_list, year, quarter, ticker_second)
)
multi_doc_context_first = generate_multi_doc_context(
context_group_first
)
multi_doc_context_second = generate_multi_doc_context(
context_group_second
)
if decoder_model == "GPT-3.5 Turbo":
if document_type == "Single-Document":
prompt = generate_gpt_prompt_alpaca(query_text, context_list)
else:
if multi_company_choice == "Single-Company":
prompt = generate_gpt_prompt_alpaca_multi_doc(
query_text, context_group
)
else:
prompt = generate_gpt_prompt_alpaca_multi_doc_multi_company(
query_text, context_group_first, context_group_second
)
with col2:
with st.form("my_form"):
edited_prompt = st.text_area(
label="Model Prompt", value=prompt, height=400
)
openai_key = st.text_input(
"Enter OpenAI key",
value="",
type="password",
)
submitted = st.form_submit_button("Submit")
if submitted:
api_key = save_key(openai_key)
openai.api_key = api_key
generated_text = gpt_turbo_model(edited_prompt)
st.subheader("Answer:")
regex_pattern_sentences = (
"(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?)\s"
)
generated_text_list = re.split(
regex_pattern_sentences, generated_text
)
for answer_text in generated_text_list:
answer_text = f"""{answer_text}"""
st.write(
f"<ul><li><p>{answer_text}</p></li></ul>",
unsafe_allow_html=True,
)
elif decoder_model == "T5":
prompt = generate_flant5_prompt_instruct_complete_context(
query_text, context_list
)
t5_pipeline = get_t5_model()
output_text = []
with col2:
with st.form("my_form"):
edited_prompt = st.text_area(
label="Model Prompt", value=prompt, height=400
)
context_list = get_context_list_prompt(edited_prompt)
submitted = st.form_submit_button("Submit")
if submitted:
for context_text in context_list:
output_text.append(
t5_pipeline(context_text)[0]["summary_text"]
)
st.subheader("Answer:")
for text in output_text:
st.markdown(f"- {text}")
elif decoder_model == "FLAN-T5":
flan_t5_model, flan_t5_tokenizer = get_flan_t5_model()
output_text = []
with col2:
prompt_type = st.selectbox(
"Select prompt type",
["Complete Text QA", "Chunkwise QA", "Chunkwise Summarize"],
)
if prompt_type == "Complete Text QA":
prompt = generate_flant5_prompt_instruct_complete_context(
query_text, context_list
)
elif prompt_type == "Chunkwise QA":
st.write("The following prompt is not editable.")
prompt = generate_flant5_prompt_instruct_chunk_context(
query_text, context_list
)
elif prompt_type == "Chunkwise Summarize":
st.write("The following prompt is not editable.")
prompt = generate_flant5_prompt_summ_chunk_context(
query_text, context_list
)
else:
prompt = ""
with st.form("my_form"):
edited_prompt = st.text_area(
label="Model Prompt", value=prompt, height=400
)
submitted = st.form_submit_button("Submit")
if submitted:
if prompt_type == "Complete Text QA":
output_text_string = generate_text_flan_t5(
flan_t5_model, flan_t5_tokenizer, prompt
)
st.subheader("Answer:")
st.write(output_text_string)
elif prompt_type == "Chunkwise QA":
for context_text in context_list:
model_input = generate_flant5_prompt_instruct_chunk_context_single(
query_text, context_text
)
output_text.append(
generate_text_flan_t5(
flan_t5_model, flan_t5_tokenizer, model_input
)
)
st.subheader("Answer:")
for text in output_text:
if "(iii)" not in text:
st.markdown(f"- {text}")
elif prompt_type == "Chunkwise Summarize":
for context_text in context_list:
model_input = (
generate_flant5_prompt_summ_chunk_context_single(
query_text, context_text
)
)
output_text.append(
generate_text_flan_t5(
flan_t5_model, flan_t5_tokenizer, model_input
)
)
st.subheader("Answer:")
for text in output_text:
if "(iii)" not in text:
st.markdown(f"- {text}")
if decoder_model == "GPT-J":
if ticker in ["AAPL", "AMD"]:
prompt = generate_gpt_j_two_shot_prompt_1(query_text, context_list)
elif ticker in ["NVDA", "INTC", "AMZN"]:
prompt = generate_gpt_j_two_shot_prompt_2(query_text, context_list)
else:
prompt = generate_gpt_j_two_shot_prompt_1(query_text, context_list)
with col2:
with st.form("my_form"):
edited_prompt = st.text_area(
label="Model Prompt", value=prompt, height=400
)
st.write(
"The app currently just shows the prompt. The app does not load the model due to memory limitations."
)
submitted = st.form_submit_button("Submit")
tab1, tab2 = st.tabs(["Retrieved Text", "Retrieved Documents"])
with tab1:
if document_type == "Single-Document":
with st.expander("See Retrieved Text"):
st.subheader("Retrieved Text:")
for context_text in context_list:
context_text = f"""{context_text}"""
st.write(
f"<ul><li><p>{context_text}</p></li></ul>",
unsafe_allow_html=True,
)
else:
with st.expander("See Retrieved Text"):
st.subheader("Retrieved Text:")
if multi_company_choice == "Compare Companies":
multi_doc_context = (
multi_doc_context_first + multi_doc_context_second
)
sections = [
s.strip()
for s in multi_doc_context.split("Document: ")
if s.strip()
]
# Add "Document: " back to the beginning of each section
context_list = [
"Document: " + s[0:7] + "\n" + s[7:] for s in sections
]
for context_text in context_list:
context_text = f"""{context_text}"""
st.write(
f"<ul><li><p>{context_text}</p></li></ul>",
unsafe_allow_html=True,
)
with tab2:
if document_type == "Single-Document":
file_text = retrieve_transcript(data, year, quarter, ticker)
with st.expander("See Transcript"):
st.subheader("Earnings Call Transcript:")
stx.scrollableTextbox(
file_text, height=700, border=False, fontFamily="Helvetica"
)
else:
if multi_company_choice == "Single-Company":
for year, quarter in year_quarter_list:
file_text = retrieve_transcript(data, year, quarter, ticker)
with st.expander(f"See Transcript - {quarter} {year}"):
st.subheader(
"Earnings Call Transcript - {quarter} {year}:"
)
stx.scrollableTextbox(
file_text,
height=700,
border=False,
fontFamily="Helvetica",
)
else:
for year, quarter in year_quarter_list:
file_text = retrieve_transcript(
data, year, quarter, ticker_first
)
with st.expander(f"See Transcript - {quarter} {year}"):
st.subheader(
"Earnings Call Transcript - {quarter} {year}:"
)
stx.scrollableTextbox(
file_text,
height=700,
border=False,
fontFamily="Helvetica",
)
for year, quarter in year_quarter_list:
file_text = retrieve_transcript(
data, year, quarter, ticker_second
)
with st.expander(f"See Transcript - {quarter} {year}"):
st.subheader(
"Earnings Call Transcript - {quarter} {year}:"
)
stx.scrollableTextbox(
file_text,
height=700,
border=False,
fontFamily="Helvetica",
)
|