forked from Nicholas-Polimeni/legislation-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.py
85 lines (69 loc) · 3.35 KB
/
chatbot.py
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
import requests
import json
import ast
import re
import streamlit as st
def query_llm(query):
url = "https://us-east4-legistlation-llm.cloudfunctions.net/llm-backend"
data = {"query": query}
response = requests.post(
url, data=json.dumps(data), headers={"Content-Type": "application/json"}
).json()
return response["answer"], response["context"]
def main():
st.set_page_config(layout="wide", page_title="Legislation Chatbot")
left, right = st.columns(2, gap="large")
answer = None
with left:
st.title("US Legislation Chatbot")
st.subheader("Enter a query about US legislation")
st.warning("""Note: the tool is currently down due to cloud costs and a code overhaul to improve efficiency (it was a hackathon project).
If you'd like to find out more details in the meantime, check [here](https://devpost.com/software/legislation-llm) or contact Nicholas Polimeni.""")
user_input = st.text_input("Input", label_visibility="hidden")
if user_input:
with st.spinner(
"Please wait while we analyze thousands of documents (average response time: <10 sec)..."
):
answer, context = query_llm(user_input)
st.divider()
st.write(
"""
### What is this?
An intelligent, ChatGPT-like chatbot for asking about US legislation, built on
cutting-edge LLMs and a database of thousands of Congressional documents. It is
also accessible as an API.
### Why?
Federal legislation impacts the entire nation, but there are hundreds
of thousands of documents to sift through. Unfortunately, that makes it
possible to hide important changes from the people. Use it to learn more about
issues you care about or even as a research assistant.
### How?
Utilizing the Congress API, sentence transformers, Google Cloud functions, and
built off of Anthropic's Claude-2 (shout-out to Anthropic for
providing an API key!), we've created a complex retrieval-augmented generative AI to answer your
queries.
### How do I use this?
Try to ask about bills related to a topic you care about, ask for details about
a specific bill, or anything you think is important! **Power to the people :fist:**
**More info** [here](https://devpost.com/software/legislation-llm), including potential future features (like notifications
for topics you care about)!
**Authors: [Nicholas Polimeni](https://www.linkedin.com/in/nickpolimeni/),
[Faris Durrani](https://www.linkedin.com/in/farisdurrani/)**
"""
)
with right:
if answer:
context = [ast.literal_eval(item) for item in context]
st.header("Answer")
st.write(answer)
st.write("")
st.subheader("Related Bills and Sub-sections")
for bill in context:
for key, val in bill.items():
key = key.replace(".txt", "").upper()
exp = r"[^\w\n .()]+"
val = re.sub(exp, "", val)
with st.expander(f"BILL {key}"):
st.text(val)
if __name__ == "__main__":
main()