Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,26 +6,25 @@ from data_utils import load_posts, extract_keywords, dedupe_sentences, strip_lab
|
|
| 6 |
from ui_components import quick_controls, pro_controls
|
| 7 |
|
| 8 |
st.set_page_config(page_title="LinkedIn Post Generator — Groq", layout="centered")
|
| 9 |
-
st.title("LinkedIn Post Generator — Quick & Pro")
|
| 10 |
|
| 11 |
-
# Sidebar
|
| 12 |
with st.sidebar:
|
| 13 |
st.subheader("Groq & Decoding")
|
| 14 |
-
model = st.selectbox("Model", [DEFAULT_MODEL, "llama-3.1-8b-instant", "mixtral-8x7b-32768"], index=0)
|
| 15 |
-
temperature = st.slider("Temperature", 0.1, 1.2, 0.6, 0.05)
|
| 16 |
-
top_p = st.slider("Top‑p", 0.1, 1.0, 0.9, 0.05)
|
| 17 |
st.markdown("Set GROQ_API_KEY in Space → Settings → Variables & Secrets.")
|
| 18 |
|
| 19 |
tabs = st.tabs(["Quick Draft", "Pro Mode", "History"])
|
| 20 |
|
| 21 |
-
# Session memory
|
| 22 |
if "history" not in st.session_state:
|
| 23 |
st.session_state.history = []
|
| 24 |
|
| 25 |
# Quick Draft
|
| 26 |
with tabs[0]:
|
| 27 |
idea, tone, words, variations, include_emoji, add_hashtags, language = quick_controls()
|
| 28 |
-
if st.button("Generate"):
|
| 29 |
if not os.getenv("GROQ_API_KEY"):
|
| 30 |
st.error("GROQ_API_KEY missing.")
|
| 31 |
elif not idea.strip():
|
|
@@ -46,14 +45,14 @@ with tabs[0]:
|
|
| 46 |
for i, p in enumerate(posts, start=1):
|
| 47 |
st.markdown(f"#### Post {i}")
|
| 48 |
st.write(p)
|
| 49 |
-
st.download_button(f"Download Post {i}", p, file_name=f"post_{i}.txt")
|
| 50 |
if posts:
|
| 51 |
st.session_state.history.append({"mode":"quick","idea":idea,"tone":tone,"words":words,"posts":posts})
|
| 52 |
|
| 53 |
# Pro Mode
|
| 54 |
with tabs[1]:
|
| 55 |
st.markdown("Upload CSV/JSON of past posts (must include 'text') to auto-extract keywords (optional).")
|
| 56 |
-
uploaded = st.file_uploader("Upload dataset", type=["csv","json"])
|
| 57 |
defaults = {"topic":"AI agent playbooks for startup ops","audience":"SaaS founders in early stage"}
|
| 58 |
topic, purpose, audience, tone2, language2, evidence, style_text = pro_controls(defaults)
|
| 59 |
keywords = []
|
|
@@ -64,22 +63,21 @@ with tabs[1]:
|
|
| 64 |
st.success(f"Loaded {len(df)} posts. Extracted keywords: {', '.join(keywords[:8]) or '—'}")
|
| 65 |
except Exception as e:
|
| 66 |
st.error(f"Dataset error: {e}")
|
| 67 |
-
|
| 68 |
-
if st.button("Suggest 5 hooks"):
|
| 69 |
try:
|
| 70 |
hooks = generate_hooks(topic, audience, tone2, 5, model, temperature, top_p, 200)
|
| 71 |
st.code(hooks)
|
| 72 |
-
chosen_hook = st.text_input("Chosen opening line (optional)", value=chosen_hook)
|
| 73 |
except Exception as e:
|
| 74 |
st.error(f"Hook generation failed: {e}")
|
| 75 |
-
chosen_hook = st.text_input("Chosen opening line (optional)") # visible even if not generated
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
|
|
|
| 79 |
clarifier_notes = "\n".join([f"Outcome: {outcome}" if outcome else "", f"Detail: {extra_detail}" if extra_detail else ""]).strip()
|
| 80 |
style_cues = [s.strip() for s in style_text.splitlines() if s.strip()][:4]
|
| 81 |
|
| 82 |
-
if st.button("Generate Post (Pro)"):
|
| 83 |
if not os.getenv("GROQ_API_KEY"):
|
| 84 |
st.error("GROQ_API_KEY missing.")
|
| 85 |
else:
|
|
@@ -89,22 +87,31 @@ with tabs[1]:
|
|
| 89 |
post = dedupe_sentences(strip_labels(raw))
|
| 90 |
st.success("Post")
|
| 91 |
st.write(post)
|
| 92 |
-
st.download_button("Download (.txt)", post, file_name="linkedin_post.txt")
|
| 93 |
st.session_state.history.append({"mode":"pro","topic":topic,"audience":audience,"post":post})
|
| 94 |
except Exception as e:
|
| 95 |
st.error(f"Generation failed: {e}")
|
| 96 |
|
| 97 |
-
# Refinements
|
| 98 |
-
|
|
|
|
| 99 |
if st.session_state.get("history") and st.session_state.history[-1].get("post"):
|
| 100 |
try:
|
| 101 |
-
instr = transform_instruction(
|
| 102 |
raw = transform_post(instr, st.session_state.history[-1]["post"], model, temperature, top_p, 500)
|
| 103 |
st.session_state.history[-1]["post"] = dedupe_sentences(strip_labels(raw))
|
| 104 |
-
st.write(st.session_state.history[-1]["post"])
|
| 105 |
except Exception as e:
|
| 106 |
st.error(f"Refinement failed: {e}")
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
# History
|
| 109 |
with tabs[2]:
|
| 110 |
if not st.session_state.history:
|
|
|
|
| 6 |
from ui_components import quick_controls, pro_controls
|
| 7 |
|
| 8 |
st.set_page_config(page_title="LinkedIn Post Generator — Groq", layout="centered")
|
| 9 |
+
st.title("LinkedIn Post Generator — Quick & Pro (Groq)")
|
| 10 |
|
| 11 |
+
# Sidebar (unique keys not required here but safe to add if duplicated later)
|
| 12 |
with st.sidebar:
|
| 13 |
st.subheader("Groq & Decoding")
|
| 14 |
+
model = st.selectbox("Model", [DEFAULT_MODEL, "llama-3.1-8b-instant", "mixtral-8x7b-32768"], index=0, key="sb_model")
|
| 15 |
+
temperature = st.slider("Temperature", 0.1, 1.2, 0.6, 0.05, key="sb_temp")
|
| 16 |
+
top_p = st.slider("Top‑p", 0.1, 1.0, 0.9, 0.05, key="sb_topp")
|
| 17 |
st.markdown("Set GROQ_API_KEY in Space → Settings → Variables & Secrets.")
|
| 18 |
|
| 19 |
tabs = st.tabs(["Quick Draft", "Pro Mode", "History"])
|
| 20 |
|
|
|
|
| 21 |
if "history" not in st.session_state:
|
| 22 |
st.session_state.history = []
|
| 23 |
|
| 24 |
# Quick Draft
|
| 25 |
with tabs[0]:
|
| 26 |
idea, tone, words, variations, include_emoji, add_hashtags, language = quick_controls()
|
| 27 |
+
if st.button("Generate", key="qd_generate"):
|
| 28 |
if not os.getenv("GROQ_API_KEY"):
|
| 29 |
st.error("GROQ_API_KEY missing.")
|
| 30 |
elif not idea.strip():
|
|
|
|
| 45 |
for i, p in enumerate(posts, start=1):
|
| 46 |
st.markdown(f"#### Post {i}")
|
| 47 |
st.write(p)
|
| 48 |
+
st.download_button(f"Download Post {i}", p, file_name=f"post_{i}.txt", key=f"qd_dl_{i}")
|
| 49 |
if posts:
|
| 50 |
st.session_state.history.append({"mode":"quick","idea":idea,"tone":tone,"words":words,"posts":posts})
|
| 51 |
|
| 52 |
# Pro Mode
|
| 53 |
with tabs[1]:
|
| 54 |
st.markdown("Upload CSV/JSON of past posts (must include 'text') to auto-extract keywords (optional).")
|
| 55 |
+
uploaded = st.file_uploader("Upload dataset", type=["csv","json"], key="pro_upload")
|
| 56 |
defaults = {"topic":"AI agent playbooks for startup ops","audience":"SaaS founders in early stage"}
|
| 57 |
topic, purpose, audience, tone2, language2, evidence, style_text = pro_controls(defaults)
|
| 58 |
keywords = []
|
|
|
|
| 63 |
st.success(f"Loaded {len(df)} posts. Extracted keywords: {', '.join(keywords[:8]) or '—'}")
|
| 64 |
except Exception as e:
|
| 65 |
st.error(f"Dataset error: {e}")
|
| 66 |
+
|
| 67 |
+
if st.button("Suggest 5 hooks", key="pro_hooks_btn"):
|
| 68 |
try:
|
| 69 |
hooks = generate_hooks(topic, audience, tone2, 5, model, temperature, top_p, 200)
|
| 70 |
st.code(hooks)
|
|
|
|
| 71 |
except Exception as e:
|
| 72 |
st.error(f"Hook generation failed: {e}")
|
|
|
|
| 73 |
|
| 74 |
+
chosen_hook = st.text_input("Chosen opening line (optional)", key="pro_chosen_hook")
|
| 75 |
+
outcome = st.text_input("Desired outcome (e.g., 10 demo requests this week)", value="", key="pro_outcome")
|
| 76 |
+
extra_detail = st.text_input("One concrete detail (e.g., 'onboarding 14→3 days')", value="", key="pro_detail")
|
| 77 |
clarifier_notes = "\n".join([f"Outcome: {outcome}" if outcome else "", f"Detail: {extra_detail}" if extra_detail else ""]).strip()
|
| 78 |
style_cues = [s.strip() for s in style_text.splitlines() if s.strip()][:4]
|
| 79 |
|
| 80 |
+
if st.button("Generate Post (Pro)", key="pro_generate"):
|
| 81 |
if not os.getenv("GROQ_API_KEY"):
|
| 82 |
st.error("GROQ_API_KEY missing.")
|
| 83 |
else:
|
|
|
|
| 87 |
post = dedupe_sentences(strip_labels(raw))
|
| 88 |
st.success("Post")
|
| 89 |
st.write(post)
|
| 90 |
+
st.download_button("Download (.txt)", post, file_name="linkedin_post.txt", key="pro_download")
|
| 91 |
st.session_state.history.append({"mode":"pro","topic":topic,"audience":audience,"post":post})
|
| 92 |
except Exception as e:
|
| 93 |
st.error(f"Generation failed: {e}")
|
| 94 |
|
| 95 |
+
# Refinements
|
| 96 |
+
col1,col2,col3,col4,col5 = st.columns(5)
|
| 97 |
+
def refine(kind, key_suffix):
|
| 98 |
if st.session_state.get("history") and st.session_state.history[-1].get("post"):
|
| 99 |
try:
|
| 100 |
+
instr = transform_instruction(kind)
|
| 101 |
raw = transform_post(instr, st.session_state.history[-1]["post"], model, temperature, top_p, 500)
|
| 102 |
st.session_state.history[-1]["post"] = dedupe_sentences(strip_labels(raw))
|
|
|
|
| 103 |
except Exception as e:
|
| 104 |
st.error(f"Refinement failed: {e}")
|
| 105 |
|
| 106 |
+
if col1.button("Shorter", key="pro_shorter"): refine("shorter","shorter")
|
| 107 |
+
if col2.button("Punchier hook", key="pro_punchy"): refine("punchier","punchy")
|
| 108 |
+
if col3.button("Add data point", key="pro_adddata"): refine("add_data","adddata")
|
| 109 |
+
if col4.button("No emojis", key="pro_noemoji"): refine("less_emoji","noemoji")
|
| 110 |
+
if col5.button("Add hashtags", key="pro_addtags"): refine("add_tags","addtags")
|
| 111 |
+
|
| 112 |
+
if st.session_state.get("history") and st.session_state.history[-1].get("post"):
|
| 113 |
+
st.write(st.session_state.history[-1]["post"])
|
| 114 |
+
|
| 115 |
# History
|
| 116 |
with tabs[2]:
|
| 117 |
if not st.session_state.history:
|