Alpha108 commited on
Commit
2094c2c
·
verified ·
1 Parent(s): af379e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -1
app.py CHANGED
@@ -1,2 +1,80 @@
 
 
 
1
  from transformers import pipeline
2
- print(pipeline('sentiment-analysis')('Test'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import json
3
+ import os
4
  from transformers import pipeline
5
+
6
+ STYLE_SAMPLES_FILE = "style_samples.json"
7
+
8
+ def load_style_samples():
9
+ if os.path.exists(STYLE_SAMPLES_FILE):
10
+ with open(STYLE_SAMPLES_FILE, "r") as f:
11
+ return json.load(f)
12
+ return []
13
+
14
+ @st.cache_resource(show_spinner=False)
15
+ def load_pipeline():
16
+ # Replace model ID with a more powerful public LLM if you want
17
+ model_id = "google/flan-t5-base" # For English text generation
18
+ gen_pipe = pipeline(
19
+ "text2text-generation",
20
+ model=model_id,
21
+ device_map="auto",
22
+ trust_remote_code=True
23
+ )
24
+ return gen_pipe
25
+
26
+ pipe = load_pipeline()
27
+ style_samples = load_style_samples()
28
+
29
+ st.set_page_config(page_title="LinkedIn Post Generator", layout="centered")
30
+ st.title("🔗 LinkedIn Post Generator (Hugging Face)")
31
+ st.write("Generate LinkedIn posts in your writing style.")
32
+
33
+ with st.form("gen_form"):
34
+ topic = st.text_input("Post Topic", "Generative AI for Business")
35
+ tone = st.selectbox("Tone", ["Professional", "Friendly", "Inspirational", "Technical", "Concise"])
36
+ audience = st.text_input("Audience", "Startup founders")
37
+ length = st.slider("Length (approx words)", 30, 400, 120, 10)
38
+ use_sample = st.selectbox(
39
+ "Style Sample (optional)", ["None"] + [f"Sample {i+1}" for i in range(len(style_samples))])
40
+ custom_style = st.text_area("Or paste your own style sample (optional)")
41
+ submitted = st.form_submit_button("Generate Post")
42
+
43
+ prompt_style = ""
44
+ if use_sample != "None":
45
+ sample_idx = int(use_sample.split()[1]) - 1
46
+ prompt_style += f"Sample style: {style_samples[sample_idx]}\n"
47
+ if custom_style.strip():
48
+ prompt_style += f"User style: {custom_style}\n"
49
+
50
+ prompt = (
51
+ f"Write a LinkedIn post on '{topic}'.\n"
52
+ f"Tone: {tone}. Audience: {audience}. Length: about {length} words.\n"
53
+ f"{prompt_style}"
54
+ "Write in engaging, natural language, and end with a strong call to action."
55
+ )
56
+
57
+ if submitted:
58
+ if not topic.strip():
59
+ st.warning("Please enter a topic before generating.")
60
+ else:
61
+ with st.spinner("Generating..."):
62
+ try:
63
+ result = pipe(prompt, max_new_tokens=length + 32)[0]["generated_text"].strip()
64
+ st.success("Here's your LinkedIn post:")
65
+ st.write(result)
66
+ st.download_button("Download post as .txt", result, file_name="linkedin_post.txt")
67
+ except Exception as e:
68
+ st.error(f"Error generating post: {e}")
69
+
70
+ st.markdown("---")
71
+ st.write("Upload a new list of style samples (JSON array of strings, replaces existing):")
72
+ file = st.file_uploader("Upload style_samples.json", type=["json"])
73
+ if file:
74
+ try:
75
+ data = json.load(file)
76
+ with open(STYLE_SAMPLES_FILE, "w") as f:
77
+ json.dump(data, f)
78
+ st.success(f"Uploaded and saved {len(data)} samples. Reload Space to use.")
79
+ except Exception as e:
80
+ st.error(f"Upload failed: {e}")