Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,75 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
unsafe_allow_html=True
|
| 13 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
st.
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
topic = st.text_input(label="Topic", placeholder="e.g., Small Business Financing")
|
| 21 |
-
platform = st.radio(label="Select a platform", options=["LinkedIn", "Instagram"])
|
| 22 |
-
submit_button = st.form_submit_button("Generate Content", use_container_width=True)
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
input_data = {
|
| 31 |
-
"topic": topic,
|
| 32 |
-
"platform": platform,
|
| 33 |
-
"tips": "",
|
| 34 |
-
"search_results": [],
|
| 35 |
-
"post": "",
|
| 36 |
-
"engagement_score": 0,
|
| 37 |
-
"tone_score": 0,
|
| 38 |
-
"clarity_score": 0,
|
| 39 |
-
"image_url": ""
|
| 40 |
-
}
|
| 41 |
-
# Run the workflow
|
| 42 |
-
output = ag.graph.invoke(input_data)
|
| 43 |
-
|
| 44 |
-
# Display results
|
| 45 |
-
st.markdown("**Generated Post:**")
|
| 46 |
-
st.markdown(output['post'])
|
| 47 |
-
|
| 48 |
-
# Display image URLs and images
|
| 49 |
-
if output['image_url']:
|
| 50 |
-
st.markdown("**Image URLs:**")
|
| 51 |
-
for i, url in enumerate(output['image_url']):
|
| 52 |
-
st.markdown(f"[Image {i+1}]({url})")
|
| 53 |
-
try:
|
| 54 |
-
st.image(url, caption=f"Image {i+1} from Pexels", use_column_width=True)
|
| 55 |
-
except:
|
| 56 |
-
st.warning(f"Could not display Image {i+1}. Use the URL above.")
|
| 57 |
-
else:
|
| 58 |
-
st.warning("No images were fetched from Pexels.")
|
| 59 |
-
|
| 60 |
-
# Display evaluation scores
|
| 61 |
-
st.markdown("**Evaluation Scores:**")
|
| 62 |
-
st.write(f"Engagement: {output['engagement_score']}")
|
| 63 |
-
st.write(f"Tone: {output['tone_score']}")
|
| 64 |
-
st.write(f"Clarity: {output['clarity_score']}")
|
| 65 |
-
except Exception as e:
|
| 66 |
-
st.error(f"Error generating content: {str(e)}")
|
| 67 |
-
else:
|
| 68 |
-
st.error("Please provide a topic.")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from langchain_huggingface import HuggingFacePipeline
|
| 4 |
+
import requests
|
| 5 |
+
from bs4 import BeautifulSoup
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
from time import sleep
|
| 8 |
+
from dataclasses import dataclass
|
| 9 |
+
from typing import List
|
| 10 |
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
# Setup HuggingFace pipeline
|
| 14 |
+
hf_pipeline = pipeline(
|
| 15 |
+
"text2text-generation",
|
| 16 |
+
model="google/flan-t5-small",
|
| 17 |
+
max_length=512,
|
| 18 |
+
temperature=0.7,
|
|
|
|
| 19 |
)
|
| 20 |
+
model = HuggingFacePipeline(pipeline=hf_pipeline)
|
| 21 |
+
|
| 22 |
+
@dataclass
|
| 23 |
+
class Command:
|
| 24 |
+
update: dict = None
|
| 25 |
+
goto: str = None
|
| 26 |
+
|
| 27 |
+
def scrape_startpage(query: str, max_results: int = 3) -> List[dict]:
|
| 28 |
+
"""Scrape search results from Startpage."""
|
| 29 |
+
url = f"https://www.startpage.com/sp/search?query={query.replace(' ', '+')}"
|
| 30 |
+
headers = {
|
| 31 |
+
"User-Agent": "Mozilla/5.0"
|
| 32 |
+
}
|
| 33 |
+
for attempt in range(3):
|
| 34 |
+
try:
|
| 35 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 36 |
+
response.raise_for_status()
|
| 37 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 38 |
+
results = []
|
| 39 |
+
for result in soup.find_all("div", class_="result")[:max_results]:
|
| 40 |
+
title = result.find("h3") or result.find("a")
|
| 41 |
+
snippet = result.find("p", class_="desc")
|
| 42 |
+
title_text = title.get_text().strip() if title else "No title"
|
| 43 |
+
snippet_text = snippet.get_text().strip() if snippet else "No snippet"
|
| 44 |
+
results.append({"title": title_text, "snippet": snippet_text})
|
| 45 |
+
return results
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"Error: {str(e)}")
|
| 48 |
+
sleep(2 ** attempt)
|
| 49 |
+
continue
|
| 50 |
+
return []
|
| 51 |
+
|
| 52 |
+
def generate_post(platform, topic, search_results):
|
| 53 |
+
prompt = f"""
|
| 54 |
+
You are a social media strategist for a B2B bank. Generate a {platform} post.
|
| 55 |
+
The post should:
|
| 56 |
+
- Be engaging but professional.
|
| 57 |
+
- Provide value to corporate clients.
|
| 58 |
+
- Focus on {topic}.
|
| 59 |
+
- Incorporate information from {search_results if search_results else "general knowledge"}.
|
| 60 |
+
Output as plain text.
|
| 61 |
+
"""
|
| 62 |
+
return model.invoke(prompt)
|
| 63 |
|
| 64 |
+
# Streamlit UI
|
| 65 |
+
st.title("Social Media Post Generator for B2B Bank")
|
| 66 |
|
| 67 |
+
platform = st.selectbox("Select Platform", ["LinkedIn", "Twitter", "Facebook"])
|
| 68 |
+
topic = st.text_input("Enter Topic", "digital transformation in banking")
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
if st.button("Generate Post"):
|
| 71 |
+
with st.spinner("Scraping data and generating post..."):
|
| 72 |
+
results = scrape_startpage(topic)
|
| 73 |
+
post = generate_post(platform, topic, results)
|
| 74 |
+
st.success("Post Generated!")
|
| 75 |
+
st.text_area("Generated Post", post, height=200)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|