import streamlit as st from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification, AutoModelForCausalLM import torch # ---------------------------------- # Page Configuration # ---------------------------------- st.set_page_config(page_title="AI Sentiment Essay Generator", page_icon="π§ ", layout="wide") st.markdown(""" """, unsafe_allow_html=True) st.title("π§ AI Sentiment Essay Generator") st.caption("Analyze your topicβs sentiment and let AI write an expressive essay aligned with it π") # ---------------------------------- # Model Initialization (cached) # ---------------------------------- @st.cache_resource def load_models(): sentiment_model_name = "cardiffnlp/twitter-roberta-base-sentiment-latest" sentiment_analyzer = pipeline("sentiment-analysis", model=sentiment_model_name) text_gen_model_name = "Qwen/Qwen1.5-1.8B" tokenizer = AutoTokenizer.from_pretrained(text_gen_model_name) text_model = AutoModelForCausalLM.from_pretrained(text_gen_model_name) return sentiment_analyzer, tokenizer, text_model sentiment_analyzer, tokenizer, text_model = load_models() # ---------------------------------- # User Input # ---------------------------------- user_prompt = st.text_area("π¬ Enter a topic or sentence:", placeholder="e.g., The future of Artificial Intelligence...") generate_btn = st.button("β¨ Generate Essay") # ---------------------------------- # Processing # ---------------------------------- if generate_btn and user_prompt.strip(): with st.spinner("Analyzing sentiment..."): sentiment_result = sentiment_analyzer(user_prompt)[0] label_map = { "LABEL_0": "Negative", "LABEL_1": "Neutral", "LABEL_2": "Positive" } sentiment_label = label_map.get(sentiment_result["label"], sentiment_result["label"]) confidence = round(sentiment_result["score"] * 100, 2) # Sentiment Display sentiment_emoji = {"Positive": "π", "Negative": "π ", "Neutral": "π"} sentiment_class = sentiment_label.lower() st.markdown(f"