ilsa15 commited on
Commit
9a858fd
·
verified ·
1 Parent(s): d8bbb2c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -0
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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.")