Spaces:
Sleeping
Sleeping
updated files
Browse files
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import spacy
|
| 5 |
+
from collections import Counter
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
|
| 8 |
+
# Load AI models ONCE at startup to cache them and avoid reloading on every interaction
|
| 9 |
+
@st.cache_resource
|
| 10 |
+
def load_summarizer():
|
| 11 |
+
"""Load the text summarization model"""
|
| 12 |
+
return pipeline("summarization", model="facebook/bart-large-cnn")
|
| 13 |
+
|
| 14 |
+
@st.cache_resource
|
| 15 |
+
def load_ner_model():
|
| 16 |
+
"""Load the Named Entity Recognition model"""
|
| 17 |
+
return spacy.load("en_core_web_sm")
|
| 18 |
+
|
| 19 |
+
# Initialize the models
|
| 20 |
+
summarizer = load_summarizer()
|
| 21 |
+
nlp = load_ner_model()
|
| 22 |
+
|
| 23 |
+
def summarize_text(text):
|
| 24 |
+
"""Function to summarize long text"""
|
| 25 |
+
# Limit input text to avoid model limits
|
| 26 |
+
input_text = text[:2000]
|
| 27 |
+
summary = summarizer(input_text, max_length=150, min_length=30, do_sample=False)
|
| 28 |
+
return summary[0]['summary_text']
|
| 29 |
+
|
| 30 |
+
def extract_entities(text):
|
| 31 |
+
"""Function to find people, orgs, money, and laws"""
|
| 32 |
+
doc = nlp(text)
|
| 33 |
+
entities = []
|
| 34 |
+
for ent in doc.ents:
|
| 35 |
+
# Filter for only the entity types we care about
|
| 36 |
+
if ent.label_ in ['PERSON', 'ORG', 'GPE', 'MONEY', 'LAW']:
|
| 37 |
+
entities.append((ent.text, ent.label_))
|
| 38 |
+
return entities
|
| 39 |
+
|
| 40 |
+
# app.py (continued)
|
| 41 |
+
# Configure the page
|
| 42 |
+
st.set_page_config(page_title="Policy Lens", page_icon="π", layout="wide")
|
| 43 |
+
st.title("π Policy Lens")
|
| 44 |
+
st.markdown("**AI-Powered Legislative Analysis** - Paste a bill or policy below to get a plain language summary and key insights.")
|
| 45 |
+
|
| 46 |
+
# Input section
|
| 47 |
+
input_text = st.text_area("Paste Legislative Text Here:", height=250, placeholder="Paste the text of a bill, policy, or news article here...")
|
| 48 |
+
|
| 49 |
+
if st.button("Analyze", type="primary") and input_text:
|
| 50 |
+
with st.spinner("Analyzing text with AI..."):
|
| 51 |
+
|
| 52 |
+
# Create a layout with columns
|
| 53 |
+
col1, col2 = st.columns(2)
|
| 54 |
+
|
| 55 |
+
with col1:
|
| 56 |
+
st.header("π Summary")
|
| 57 |
+
summary = summarize_text(input_text)
|
| 58 |
+
st.success(summary)
|
| 59 |
+
|
| 60 |
+
with col2:
|
| 61 |
+
st.header("π§ Key Entities")
|
| 62 |
+
entities = extract_entities(input_text)
|
| 63 |
+
|
| 64 |
+
# Categorize the entities
|
| 65 |
+
people = [text for text, label in entities if label == 'PERSON']
|
| 66 |
+
organizations = [text for text, label in entities if label == 'ORG']
|
| 67 |
+
money = [text for text, label in entities if label == 'MONEY']
|
| 68 |
+
locations = [text for text, label in entities if label == 'GPE']
|
| 69 |
+
|
| 70 |
+
# Display the entities in an organized way
|
| 71 |
+
if people:
|
| 72 |
+
st.write("**People:**", ", ".join(set(people))) # Use set() to remove duplicates
|
| 73 |
+
if organizations:
|
| 74 |
+
st.write("**Organizations:**", ", ".join(set(organizations)))
|
| 75 |
+
if money:
|
| 76 |
+
st.write("**Financials:**", ", ".join(set(money)))
|
| 77 |
+
if locations:
|
| 78 |
+
st.write("**Locations:**", ", ".join(set(locations)))
|
| 79 |
+
|
| 80 |
+
# Visualization section (optional but impressive)
|
| 81 |
+
st.header("π Entity Frequency")
|
| 82 |
+
if entities:
|
| 83 |
+
# Count the most common entities
|
| 84 |
+
entity_counts = Counter([label for text, label in entities])
|
| 85 |
+
|
| 86 |
+
# Create a simple bar chart
|
| 87 |
+
fig, ax = plt.subplots()
|
| 88 |
+
ax.bar(entity_counts.keys(), entity_counts.values())
|
| 89 |
+
ax.set_ylabel('Frequency')
|
| 90 |
+
ax.set_title('Most Common Entity Types')
|
| 91 |
+
plt.xticks(rotation=45)
|
| 92 |
+
st.pyplot(fig)
|
| 93 |
+
else:
|
| 94 |
+
st.info("No significant entities found to display.")
|
| 95 |
+
|
| 96 |
+
else:
|
| 97 |
+
st.info("π Please paste some text to analyze. For demo purposes, you can find text on sites like congress.gov")
|
| 98 |
+
|
| 99 |
+
# Add a footer
|
| 100 |
+
st.markdown("---")
|
| 101 |
+
st.caption("Policy Lens uses Facebook's BART model for summarization and spaCy for entity recognition.")
|