Update app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,35 @@
|
|
| 1 |
-
import
|
| 2 |
-
from
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
"
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
temperature=0.7,
|
| 19 |
-
)
|
| 20 |
-
model = HuggingFacePipeline(pipeline=hf_pipeline)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
class Command:
|
| 24 |
-
update: dict = None
|
| 25 |
-
goto: str = None
|
| 26 |
|
| 27 |
-
|
| 28 |
-
"
|
| 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 |
-
|
| 53 |
-
|
| 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 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
| 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)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from agent import graph, State
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
def generate_post(topic, platform):
|
| 5 |
+
initial_state: State = {
|
| 6 |
+
"topic": topic,
|
| 7 |
+
"platform": platform
|
| 8 |
+
}
|
| 9 |
+
final_state = graph.run(initial_state)
|
| 10 |
|
| 11 |
+
post = final_state.get("post", "No post generated")
|
| 12 |
+
engagement = final_state.get("engagement_score", "N/A")
|
| 13 |
+
tone = final_state.get("tone_score", "N/A")
|
| 14 |
+
clarity = final_state.get("clarity_score", "N/A")
|
| 15 |
+
images = "\n".join(final_state.get("image_url", [])) or "No images found"
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
return post, engagement, tone, clarity, images
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
with gr.Blocks() as demo:
|
| 20 |
+
gr.Markdown("# Social Media Post Generator for B2B Banking")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
topic = gr.Textbox(label="Topic", value="digital transformation in banking")
|
| 23 |
+
platform = gr.Dropdown(choices=["LinkedIn", "Twitter", "Facebook"], value="LinkedIn", label="Platform")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
btn = gr.Button("Generate Post")
|
| 26 |
+
output_post = gr.Textbox(label="Generated Post", lines=10)
|
| 27 |
+
output_engagement = gr.Textbox(label="Engagement Score")
|
| 28 |
+
output_tone = gr.Textbox(label="Tone Score")
|
| 29 |
+
output_clarity = gr.Textbox(label="Clarity Score")
|
| 30 |
+
output_images = gr.Textbox(label="Image URLs")
|
| 31 |
|
| 32 |
+
btn.click(generate_post, inputs=[topic, platform],
|
| 33 |
+
outputs=[output_post, output_engagement, output_tone, output_clarity, output_images])
|
| 34 |
|
| 35 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|