ilsa15 commited on
Commit
b44b64d
Β·
verified Β·
1 Parent(s): 7da705f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -61
app.py CHANGED
@@ -2,102 +2,85 @@ import streamlit as st
2
  import requests
3
  import os
4
 
5
- # ----------------------------
6
  # Page Config
7
- # ----------------------------
8
  st.set_page_config(
9
  page_title="LinkedIn Post Generator",
10
- page_icon="πŸš€",
11
- layout="centered"
12
  )
13
 
14
- st.title("πŸš€ AI LinkedIn Post Generator")
15
- st.write("Generate viral-style LinkedIn posts using AI")
16
 
17
- # ----------------------------
18
- # Get HF API Key from Secrets
19
- # ----------------------------
20
- HF_API_KEY = os.getenv("HF_API_KEY")
21
 
22
- if not HF_API_KEY:
23
- st.error("⚠️ Please add your HF_API_KEY in Hugging Face Space Secrets.")
24
  st.stop()
25
 
26
- API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
27
 
28
  headers = {
29
- "Authorization": f"Bearer {HF_API_KEY}"
 
30
  }
31
 
32
- # ----------------------------
33
  # User Inputs
34
- # ----------------------------
35
- topic = st.text_input("Enter your LinkedIn post topic")
36
 
37
  tone = st.selectbox(
38
  "Select Tone",
39
- [
40
- "Professional",
41
- "Motivational",
42
- "Storytelling",
43
- "Bold & Controversial",
44
- "Friendly"
45
- ]
46
  )
47
 
48
  generate = st.button("Generate Post")
49
 
50
- # ----------------------------
51
- # Prompt Template
52
- # ----------------------------
53
- def build_prompt(topic, tone):
54
- return f"""
55
- You are a LinkedIn content expert.
 
 
56
 
57
- Write a viral LinkedIn post about: "{topic}"
 
58
 
59
  Tone: {tone}
60
 
61
  Requirements:
62
  - Strong hook in first line
63
- - Short punchy paragraphs
64
- - Use spacing for readability
65
- - Add bullet points if relevant
66
- - End with engagement question
67
- - Add 3-5 relevant hashtags
68
-
69
- Make it authentic and high engagement.
70
  """
71
 
72
-
73
- # ----------------------------
74
- # Generate Response
75
- # ----------------------------
76
- if generate:
77
- if not topic:
78
- st.warning("Please enter a topic.")
79
- else:
80
- with st.spinner("Generating your viral post..."):
81
-
82
- prompt = build_prompt(topic, tone)
83
-
84
  payload = {
85
- "inputs": prompt,
86
- "parameters": {
87
- "max_new_tokens": 400,
88
- "temperature": 0.7,
89
- "return_full_text": False
90
- }
 
91
  }
92
 
93
  response = requests.post(API_URL, headers=headers, json=payload)
94
 
95
  if response.status_code == 200:
96
  result = response.json()
97
- generated_text = result[0]["generated_text"]
98
-
99
- st.success("βœ… Your LinkedIn Post is Ready!")
100
- st.text_area("Generated Post", generated_text, height=400)
101
 
 
 
102
  else:
103
- st.error("Error generating content. Try again later.")
 
 
2
  import requests
3
  import os
4
 
5
+ # ------------------------
6
  # Page Config
7
+ # ------------------------
8
  st.set_page_config(
9
  page_title="LinkedIn Post Generator",
10
+ page_icon="πŸš€"
 
11
  )
12
 
13
+ st.title("πŸš€ AI LinkedIn Post Generator (Groq Powered)")
14
+ st.write("Generate high-engagement LinkedIn posts instantly.")
15
 
16
+ # ------------------------
17
+ # Load Groq API Key from Secrets
18
+ # ------------------------
19
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
20
 
21
+ if not GROQ_API_KEY:
22
+ st.error("Please add GROQ_API_KEY in Hugging Face Space Secrets.")
23
  st.stop()
24
 
25
+ API_URL = "https://api.groq.com/openai/v1/chat/completions"
26
 
27
  headers = {
28
+ "Authorization": f"Bearer {GROQ_API_KEY}",
29
+ "Content-Type": "application/json"
30
  }
31
 
32
+ # ------------------------
33
  # User Inputs
34
+ # ------------------------
35
+ topic = st.text_input("Enter Topic")
36
 
37
  tone = st.selectbox(
38
  "Select Tone",
39
+ ["Professional", "Motivational", "Storytelling", "Bold", "Friendly"]
 
 
 
 
 
 
40
  )
41
 
42
  generate = st.button("Generate Post")
43
 
44
+ # ------------------------
45
+ # Generate Content
46
+ # ------------------------
47
+ if generate:
48
+ if not topic:
49
+ st.warning("Please enter a topic.")
50
+ else:
51
+ with st.spinner("Generating your post..."):
52
 
53
+ prompt = f"""
54
+ Write a high-engagement LinkedIn post about "{topic}".
55
 
56
  Tone: {tone}
57
 
58
  Requirements:
59
  - Strong hook in first line
60
+ - Short readable paragraphs
61
+ - Add spacing for clarity
62
+ - End with an engaging question
63
+ - Include 3 relevant hashtags
 
 
 
64
  """
65
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  payload = {
67
+ "model": "llama3-8b-8192",
68
+ "messages": [
69
+ {"role": "system", "content": "You are a LinkedIn content expert."},
70
+ {"role": "user", "content": prompt}
71
+ ],
72
+ "temperature": 0.7,
73
+ "max_tokens": 500
74
  }
75
 
76
  response = requests.post(API_URL, headers=headers, json=payload)
77
 
78
  if response.status_code == 200:
79
  result = response.json()
80
+ content = result["choices"][0]["message"]["content"]
 
 
 
81
 
82
+ st.success("βœ… Post Generated!")
83
+ st.text_area("Your LinkedIn Post", content, height=400)
84
  else:
85
+ st.error("Error generating content.")
86
+ st.write(response.text)