-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
105 lines (90 loc) · 3.22 KB
/
app.py
File metadata and controls
105 lines (90 loc) · 3.22 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# app.py
import streamlit as st
from sentiment import detect_sentiment, get_sentiment_pipeline
from generator import generate_text
from transformers import pipeline
# ---------------------------
# Streamlit Page Setup
# ---------------------------
st.set_page_config(
page_title="Sentiment-Aligned Text Generator",
page_icon="🧠",
layout="wide",
)
st.title("🧠 AI Text Generator — Sentiment-Aligned")
st.markdown(
"This tool analyzes the **sentiment** of your input and generates text "
"that matches the detected emotion using a GPT model."
)
st.divider()
# ---------------------------
# Cache Models
# ---------------------------
@st.cache_resource
def load_sentiment():
return get_sentiment_pipeline()
@st.cache_resource
def load_model(model_name="gpt2"):
return pipeline("text-generation", model=model_name)
# ---------------------------
# Sidebar Controls
# ---------------------------
with st.sidebar:
st.header("⚙️ Settings")
model_choice = st.selectbox("Model", ["gpt2", "distilgpt2"], index=0)
max_words = st.slider("Max words (approx.)", 30, 400, 120, step=10)
seed_value = st.number_input("Seed (for reproducibility)", min_value=0, value=42)
manual_override = st.checkbox("Manually select sentiment")
manual_sentiment = None
if manual_override:
manual_sentiment = st.radio("Select sentiment", ["positive", "neutral", "negative"])
# ---------------------------
# User Input
# ---------------------------
st_prompt = st.text_area(
"💬 Enter your prompt",
height=150,
placeholder="e.g. A small town by the sea, childhood memory, or the value of teamwork...",
)
# ---------------------------
# Main Action
# ---------------------------
if st.button("🚀 Generate Text"):
if not st_prompt.strip():
st.warning("Please enter a prompt before generating.")
else:
try:
# Sentiment Detection
with st.spinner("🔍 Detecting sentiment..."):
nlp = load_sentiment()
detection = detect_sentiment(st_prompt, nlp=nlp)
detected = manual_sentiment if manual_override else detection["sentiment"]
st.info(
f"**Detected Sentiment:** `{detection['label']}` "
f"(score: {detection['score']:.2f}) → **Using:** `{detected}`"
)
# Text Generation
with st.spinner("✍️ Generating text (this may take a few seconds)..."):
text = generate_text(
st_prompt,
detected,
max_new_tokens=max_words,
model_name=model_choice,
seed=seed_value,
)
# Output
st.success("✅ Generation Complete!")
st.subheader("🧾 Generated Text")
st.write(text)
st.download_button(
"📥 Download Result (.txt)",
data=text,
file_name="generated_text.txt",
)
except Exception as e:
st.error(f"⚠️ Something went wrong: {e}")
# ---------------------------
# Footer
# ---------------------------
st.divider()
st.caption("Built with ❤️ using Streamlit and Hugging Face Transformers.")