Spaces:
Sleeping
Sleeping
File size: 2,525 Bytes
1af9599 1dec14a 514e9a1 1dec14a 1af9599 1dec14a | 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 | import streamlit as st
import pandas as pd
import numpy as np
import torch
from transformers import pipeline
import os
# Force Transformers to ignore CUDA entirely
os.environ["CUDA_VISIBLE_DEVICES"] = ""
# Load model ONCE (Streamlit caches it)
@st.cache_resource
def load_classifier():
try:
classifier = pipeline(
"text-classification",
"hemitpatel/political_concept_classifer",
device=-1, # Force CPU
model_kwargs={"low_cpu_mem_usage": True, "torch_dtype": torch.float32}
)
return classifier, "π» CPU"
except Exception as e:
st.error(f"Model loading error: {str(e)}")
raise e
classifier, device_info = load_classifier()
def classifyPoliticalConcept(text: str):
return classifier(text)
# Page config
st.set_page_config(page_title="Political Concept Classifier", page_icon="π³οΈ", layout="wide")
# Sidebar with device info
st.sidebar.title("βοΈ System Info")
st.sidebar.metric("Device", "CPU")
st.sidebar.metric("CUDA Available", "β No")
# Title
st.title("π³οΈ Political Concept Classifier App")
st.write("Paste political text and classify it into concepts like **economy**, **immigration**, or **healthcare**.")
# Left: Input | Right: Output
col1, col2 = st.columns(2)
with col1:
st.subheader("βοΈ Enter Text to Classify")
input_text = st.text_area(
"Political text:",
placeholder="Paste any political statement, article excerpt, or policy text here...",
height=250
)
classify_btn = st.button("Classify Text", type="primary")
with col2:
st.subheader("π Classification Result")
if classify_btn:
if not input_text.strip():
st.warning("Please enter text before clicking classify.")
else:
with st.spinner("Classifying..."):
result = classifyPoliticalConcept(input_text)
st.success("Done!")
if result:
top_result = result[0]
st.metric(
label="Predicted Concept",
value=top_result['label'],
delta=f"{top_result['score']:.2%} confidence"
)
st.json(result)
# Extra Sample Tools
st.divider()
st.subheader("π Random Example Data (Just for Demo)")
sample_data = pd.DataFrame(np.random.randn(20, 3), columns=['Series 1', 'Series 2', 'Series 3'])
st.line_chart(sample_data)
st.caption("Built with Streamlit π")
|