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("""
""", 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"📤 Share on WhatsApp",
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()