import streamlit as st
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
import torch
st.set_page_config(
page_title="kindify",
page_icon="logo.png",
layout="wide"
)
st.markdown(
"""
""",
unsafe_allow_html=True
)
st.title("Toxicity to Kindness Converter")
with open("logo.svg", "r") as f:
svg_content = f.read()
top_cut = 120
right_cut = 50
bottom_cut = 30
left_cut = 280
scale_factor = 0.65
st.sidebar.markdown('
', unsafe_allow_html=True)
st.sidebar.markdown(
f'''
''',
unsafe_allow_html=True
)
with st.sidebar:
st.markdown("", unsafe_allow_html=True)
st.markdown("""
Your AI-Powered Rose-Colored Glasses
We transform negativity into positive communication
1. Paste the harsh text
2. Click "✨ Kindify"
3. Save the kinder version
""", unsafe_allow_html=True)
@st.cache_resource
def load_model(model_name):
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
return model, tokenizer
model_name = "avo-milas/tox2kind"
model, tokenizer = load_model(model_name)
if 'history' not in st.session_state:
st.session_state.history = []
col1, col2 = st.columns([3, 1])
with col1:
query = st.text_area(
"Your message:",
value="This is absolute garbage. Do you even know what you’re doing? Rewrite it",
placeholder="Enter the message here...",
help="We’ll give this a cheerful twist",
max_chars=300,
height=100,
key="user_input"
)
with col2:
st.markdown("", unsafe_allow_html=True)
if st.button("✨ Kindify",
use_container_width=True,
type="secondary"):
with st.spinner("Transforming..."):
try:
inputs = tokenizer(query, return_tensors="pt", padding=True, truncation=True, max_length=32)
with torch.no_grad():
outputs = model.generate(
inputs['input_ids'],
max_length=32,
num_beams=5,
do_sample=True,
top_p=0.92,
temperature=0.9,
early_stopping=True
)
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
st.session_state.history.append(
(query, result)
)
# st.text("Transformed successfully!")
except Exception as e:
st.error(f"Error: {str(e)}")
if st.session_state.history:
latest = st.session_state.history[-1]
st.divider()
st.subheader("Transformation Results")
col1, col2 = st.columns(2)
with col1:
st.markdown("**Original Message**")
st.warning(latest[0])
with col2:
st.markdown("**Kind Version**")
st.success(latest[1])
if st.button("📋 Copy Kind Version", use_container_width=True):
st.session_state.clipboard = latest[1]
st.toast("Copied to clipboard!", icon="✔️")