AI-Reporter / app.py
Vnmrsharma's picture
Update app.py
2e2f4e0 verified
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()