Spaces:
Sleeping
Sleeping
File size: 5,125 Bytes
a8f7c32 5ac3cdd 2e2f4e0 fcc3543 a8f7c32 fcc3543 5ac3cdd a8f7c32 5ac3cdd 2e2f4e0 1f39e22 5ac3cdd 2e2f4e0 48fbb42 2e2f4e0 48fbb42 2e2f4e0 1f39e22 2e2f4e0 48fbb42 2e2f4e0 48fbb42 2e2f4e0 48fbb42 80db8ac 48fbb42 5ac3cdd 80db8ac 5ac3cdd 48fbb42 80db8ac a8f7c32 0b10bb6 48fbb42 a8f7c32 80db8ac 5ac3cdd 48fbb42 80db8ac 5ac3cdd 80db8ac 5ac3cdd 80db8ac 5ac3cdd 80db8ac a8f7c32 80db8ac a8f7c32 80db8ac 5ac3cdd 80db8ac a8f7c32 48fbb42 a8f7c32 80db8ac 0b10bb6 a8f7c32 5ac3cdd 80db8ac 5ac3cdd a8f7c32 5ac3cdd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
import os
import urllib.parse
import streamlit as st
import requests
# Load configuration from environment variables
FRONTEND_PASSWORD = os.environ.get("FE_PASS")
HF_TOKEN = os.environ.get("HF_TOKEN")
API_URL = os.environ.get("END_POINT_URL")
if not all([FRONTEND_PASSWORD, HF_TOKEN, API_URL]):
raise RuntimeError("Please set FE_PASS, HF_TOKEN, and END_POINT_URL environment variables.")
def main():
# Page config
st.set_page_config(
page_title="Automated Reporter",
page_icon="π€",
layout="centered",
)
# Custom CSS for enhanced styling
st.markdown("""
<style>
/* Page background and container */
.stApp { background-color: #f7f9fc; }
.block-container { padding: 2rem 1rem; max-width: 800px; margin: auto; }
/* Title styling */
.stMarkdown h1, .stMarkdown h2, .stMarkdown h3 { color: #333; }
/* Input fields styling */
div.stTextInput>div>div>input, div.stTextArea>div>div>textarea {
border-radius: 8px !important;
border: 1px solid #ccc !important;
padding: 0.6rem !important;
font-size: 1rem !important;
}
/* Button styling */
div.stButton>button {
background-color: #4caf50 !important;
color: white !important;
padding: 0.75rem 1.5rem !important;
border-radius: 8px !important;
font-size: 1rem !important;
transition: background-color 0.3s ease;
}
div.stButton>button:hover {
background-color: #45a049 !important;
}
/* Error and success message styling */
.stError, .stSuccess { font-weight: bold; }
/* WhatsApp share link styling */
a.whatsapp-share {
display: inline-block;
margin-top: 0.5rem;
color: #25D366;
font-weight: bold;
text-decoration: none;
}
a.whatsapp-share:hover { text-decoration: underline; }
</style>
""", unsafe_allow_html=True)
# Password prompt before showing UI
password = st.text_input("Enter password", type="password", placeholder="UI Access Password")
if password != FRONTEND_PASSWORD:
if password:
st.error("Invalid password. Please try again.")
return
# Header
st.title("π€ Automated Reporter")
st.markdown("Publish your news now β an advanced AI by Vinamra.")
# User inputs
title = st.text_input("Post Title", max_chars=150, placeholder="Enter a descriptive title")
news_input = st.text_area("Raw News Text", height=200, placeholder="Paste your raw news text here")
image = st.file_uploader("Featured Image", type=["png", "jpg", "jpeg", "gif"])
# Publish button
if st.button("Publish News"):
# Validation
if not title:
st.error("Please provide a post title.")
return
if not news_input:
st.error("Please provide the raw news text.")
return
if image is None:
st.error("Please upload a featured image.")
return
# Send request
with st.spinner("Publishing..."):
try:
files = {"image": (image.name, image, image.type)}
data = {"title": title, "news_input": news_input}
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
response = requests.post(
API_URL,
data=data,
files=files,
headers=headers,
timeout=60
)
if response.status_code == 201:
try:
result = response.json()
except ValueError:
st.error("Failed to parse JSON response.")
return
raw_url = result.get('post_url')
# Decode percent-encoded URL to display properly
decoded_url = urllib.parse.unquote(raw_url)
st.success("π Post published successfully!")
# Display clickable decoded URL text
st.markdown(f"**Post URL:** [Click here]({decoded_url})")
# Share via WhatsApp: encode decoded URL for message text
share_text = decoded_url
encoded_text = urllib.parse.quote_plus(share_text)
whatsapp_link = f"https://wa.me/?text={encoded_text}"
st.markdown(
f"<a class=\"whatsapp-share\" href=\"{whatsapp_link}\" target=\"_blank\">π€ Share on WhatsApp</a>",
unsafe_allow_html=True
)
else:
try:
err = response.json().get('error', response.text)
except ValueError:
err = response.text or 'No response body.'
st.error(f"Failed to publish post: {response.status_code} β {err}")
except requests.exceptions.RequestException as e:
st.error(f"Request error: {e}")
if __name__ == "__main__":
main() |