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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import random
|
| 3 |
+
import time
|
| 4 |
+
|
| 5 |
from datasets import load_dataset
|
| 6 |
+
from openai import OpenAI
|
| 7 |
import pandas as pd
|
| 8 |
import streamlit as st
|
| 9 |
|
| 10 |
+
st.set_page_config(layout="wide")
|
| 11 |
+
|
| 12 |
+
CONGRESS_GOV_TYPE_MAP = {
|
| 13 |
+
"hconres": "house-concurrent-resolution",
|
| 14 |
+
"hjres": "house-joint-resolution",
|
| 15 |
+
"hr": "house-bill",
|
| 16 |
+
"hres": "house-resolution",
|
| 17 |
+
"s": "senate-bill",
|
| 18 |
+
"sconres": "senate-concurrent-resolution",
|
| 19 |
+
"sjres": "senate-joint-resolution",
|
| 20 |
+
"sres": "senate-resolution",
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
@st.cache_data(show_spinner="Fetching HF data from Hub ...")
|
| 25 |
+
def get_data():
|
| 26 |
+
dsd = load_dataset("hyperdemocracy/us-congress", "unified_v1")
|
| 27 |
+
df = pd.concat([ds.to_pandas() for ds in dsd.values()])
|
| 28 |
+
df["text"] = df["textversions"].apply(lambda x: x[0]["text_v1"] if len(x) > 0 else "")
|
| 29 |
+
df = df[df["text"].str.len() > 0]
|
| 30 |
+
df1 = df[df["legis_id"]=="118-s-3207"]
|
| 31 |
+
return pd.concat([df1, df.sample(n=100)])
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def escape_markdown(text):
|
| 35 |
+
MD_SPECIAL_CHARS = "\`*_{}[]()#+-.!$"
|
| 36 |
+
for char in MD_SPECIAL_CHARS:
|
| 37 |
+
text = text.replace(char, "\\"+char)
|
| 38 |
+
return text
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def get_sponsor_url(bioguide_id):
|
| 42 |
+
return f"https://bioguide.congress.gov/search/bio/{bioguide_id}"
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def get_congress_gov_url(congress_num, legis_type, legis_num):
|
| 46 |
+
lt = CONGRESS_GOV_TYPE_MAP[legis_type]
|
| 47 |
+
return f"https://www.congress.gov/bill/{congress_num}th-congress/{lt}/{legis_num}"
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def show_bill(bdict):
|
| 51 |
+
bill_url = get_congress_gov_url(
|
| 52 |
+
bdict["congress_num"],
|
| 53 |
+
bdict["legis_type"],
|
| 54 |
+
bdict["legis_num"],
|
| 55 |
+
)
|
| 56 |
+
sponsor_url = get_sponsor_url(
|
| 57 |
+
bdict["metadata"]["sponsors"][0]["bioguide_id"]
|
| 58 |
+
)
|
| 59 |
+
st.header("Metadata")
|
| 60 |
+
st.write("**Bill ID**: [{}]({})".format(bdict["legis_id"], bill_url))
|
| 61 |
+
st.write("**Sponsor**: [{}]({})".format(bdict["metadata"]["sponsors"][0]["full_name"], sponsor_url))
|
| 62 |
+
st.write("**Title**: {}".format(bdict["metadata"]["title"]))
|
| 63 |
+
st.write("**Introduced**: {}".format(bdict["metadata"]["introduced_date"]))
|
| 64 |
+
st.write("**Policy Area**: {}".format(bdict["metadata"]["policy_area"]))
|
| 65 |
+
st.write("**Subjects**: {}".format(bdict["metadata"]["subjects"]))
|
| 66 |
+
st.write("**Character Count**: {}".format(len(bdict["text"])))
|
| 67 |
+
st.write("**Estimated Tokens**: {}".format(len(bdict["text"])/4))
|
| 68 |
+
|
| 69 |
+
st.header("Summary")
|
| 70 |
+
if len(bdict["metadata"]["summaries"]) > 0:
|
| 71 |
+
st.write(bdict["metadata"]["summaries"][0])
|
| 72 |
+
# st.markdown(bdict["metadata"]["summaries"][0]["text"], unsafe_allow_html=True)
|
| 73 |
+
else:
|
| 74 |
+
st.write("Not Available")
|
| 75 |
+
|
| 76 |
+
st.header("Text")
|
| 77 |
+
st.markdown(escape_markdown(bdict["text"]))
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
if "messages" not in st.session_state:
|
| 84 |
+
st.session_state["messages"] = []
|
| 85 |
+
if "openai_model" not in st.session_state:
|
| 86 |
+
st.session_state["openai_model"] = "gpt-3.5-turbo-0125"
|
| 87 |
+
if "openai_api_key" not in st.session_state:
|
| 88 |
+
st.session_state["openai_api_key"] = None
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
df = get_data()
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
with st.sidebar:
|
| 95 |
+
|
| 96 |
+
st.header("Configuration")
|
| 97 |
+
|
| 98 |
+
openai_api_key = st.text_input(
|
| 99 |
+
label = "OpenAI API Key:",
|
| 100 |
+
help="Required for OpenAI Models",
|
| 101 |
+
type="password",
|
| 102 |
+
key="openai_api_key",
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
MODELS = ["gpt-3.5-turbo-0125", "gpt-4-0125-preview"]
|
| 106 |
+
st.selectbox("Model Name", MODELS, key="openai_model")
|
| 107 |
+
|
| 108 |
+
LEGIS_IDS = df["legis_id"].to_list()
|
| 109 |
+
st.selectbox("Legis ID", LEGIS_IDS, key="legis_id")
|
| 110 |
+
bdict = df[df["legis_id"] == st.session_state["legis_id"]].iloc[0].to_dict()
|
| 111 |
+
|
| 112 |
+
if st.button("Clear Messages"):
|
| 113 |
+
st.session_state["messages"] = []
|
| 114 |
+
|
| 115 |
+
st.header("Debug")
|
| 116 |
+
|
| 117 |
+
with st.expander("Show Messages"):
|
| 118 |
+
st.write(st.session_state["messages"])
|
| 119 |
+
|
| 120 |
+
with st.expander("Show Bill Dictionary"):
|
| 121 |
+
st.write(bdict)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
system_message = {
|
| 125 |
+
"role": "system",
|
| 126 |
+
"content": "You are a helpful legislative question answering assistant. Use the following legislative text to help answer user questions.\n\n---" + bdict["text"],
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
with st.expander("Show Bill Details"):
|
| 131 |
+
with st.container(height=600):
|
| 132 |
+
show_bill(bdict)
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
for message in st.session_state["messages"]:
|
| 136 |
+
with st.chat_message(message["role"]):
|
| 137 |
+
st.markdown(message["content"])
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
if prompt := st.chat_input("How can I help you understand this bill?"):
|
| 141 |
+
|
| 142 |
+
if st.session_state["openai_api_key"] is None:
|
| 143 |
+
st.warning("Enter API key to chat")
|
| 144 |
+
st.stop()
|
| 145 |
+
else:
|
| 146 |
+
client = OpenAI(api_key=openai_api_key)
|
| 147 |
|
| 148 |
+
with st.chat_message("user"):
|
| 149 |
+
st.markdown(prompt)
|
| 150 |
+
st.session_state["messages"].append({"role": "user", "content": prompt})
|
| 151 |
|
| 152 |
+
with st.chat_message("assistant"):
|
| 153 |
+
stream = client.chat.completions.create(
|
| 154 |
+
model=st.session_state["openai_model"],
|
| 155 |
+
messages=[system_message] + [
|
| 156 |
+
{"role": msg["role"], "content": msg["content"]}
|
| 157 |
+
for msg in st.session_state.messages
|
| 158 |
+
],
|
| 159 |
+
temperature=0.0,
|
| 160 |
+
stream=True,
|
| 161 |
+
)
|
| 162 |
+
response = st.write_stream(stream)
|
| 163 |
|
| 164 |
+
st.session_state["messages"].append({"role": "assistant", "content": response})
|