Alpha108 commited on
Commit
7b65583
·
verified ·
1 Parent(s): 745f535

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -78
app.py CHANGED
@@ -1,79 +1,2 @@
1
- import streamlit as st
2
- import json
3
- import os
4
  from transformers import pipeline
5
-
6
- def load_style_samples(filename="style_samples.json"):
7
- if os.path.exists(filename):
8
- with open(filename, "r") as f:
9
- return json.load(f)
10
- return []
11
-
12
- @st.cache_resource(show_spinner=False)
13
- def load_text_gen_pipeline():
14
- model_id = "google/flan-t5-base" # Change to a larger or different supported model as needed
15
- return pipeline(
16
- "text2text-generation",
17
- model=model_id,
18
- device_map="auto",
19
- trust_remote_code=True
20
- )
21
-
22
- # Load model and style samples
23
- pipe = load_text_gen_pipeline()
24
- style_samples = load_style_samples()
25
-
26
- st.set_page_config(page_title="LinkedIn Post Generator", layout="centered")
27
- st.title("🔗 LinkedIn Post Generator (Hugging Face Space)")
28
- st.write("Generate LinkedIn posts with custom style and parameters.")
29
-
30
- with st.form("generate_form"):
31
- topic = st.text_input("Post topic", "Generative AI for Business")
32
- tone = st.selectbox("Tone", ["Professional", "Friendly", "Inspirational", "Technical", "Concise"])
33
- audience = st.text_input("Audience", "Startup founders")
34
- length = st.slider("Approximate length (words)", 50, 500, 150, 10)
35
- use_sample = st.selectbox(
36
- "Optional: Select style sample",
37
- ["None"] + [f"Sample {i+1}" for i in range(len(style_samples))])
38
- custom_example = st.text_area("Or paste your own style sample (optional)")
39
- submitted = st.form_submit_button("Generate Post")
40
-
41
- style_example_text = ""
42
- if use_sample != "None":
43
- idx = int(use_sample.split()[1]) - 1
44
- style_example_text += f"Example post style (imitate tone & voice): {style_samples[idx]}\n"
45
- if custom_example.strip():
46
- style_example_text += f"Custom example: {custom_example}\n"
47
-
48
- prompt = (
49
- f"Write a LinkedIn post about '{topic}'.\n"
50
- f"Tone: {tone}. Audience: {audience}. Target length: {length} words.\n"
51
- f"{style_example_text}"
52
- "Make it engaging and end with a relevant call to action."
53
- )
54
-
55
- if submitted:
56
- if not topic.strip():
57
- st.warning("Please enter a topic.")
58
- else:
59
- with st.spinner("Generating post..."):
60
- try:
61
- generations = pipe(prompt, max_new_tokens=length + 32)
62
- result = generations[0]["generated_text"].strip()
63
- st.success("Here's your LinkedIn post:")
64
- st.write(result)
65
- st.download_button("Download Post as Text", result, file_name="linkedin_post.txt")
66
- except Exception as e:
67
- st.error(f"Error generating post: {e}")
68
-
69
- st.markdown("---")
70
- st.write("Upload your own style samples to personalize generation (overwrite previous samples):")
71
- file = st.file_uploader("Upload style_samples.json", type=["json"])
72
- if file is not None:
73
- try:
74
- new_samples = json.load(file)
75
- with open("style_samples.json", "w") as f:
76
- json.dump(new_samples, f)
77
- st.success(f"File uploaded successfully! Now {len(new_samples)} style samples saved. Reload app to use.")
78
- except Exception as e:
79
- st.error(f"Upload failed: {e}")
 
 
 
 
1
  from transformers import pipeline
2
+ print(pipeline('sentiment-analysis')('Test'))