Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,9 +31,6 @@ except Exception as e:
|
|
| 31 |
st.stop()
|
| 32 |
|
| 33 |
def get_font(size=80):
|
| 34 |
-
"""
|
| 35 |
-
Loads a font for image overlay. Downloads if not locally present.
|
| 36 |
-
"""
|
| 37 |
try:
|
| 38 |
return ImageFont.truetype("font.ttf", size)
|
| 39 |
except OSError:
|
|
@@ -45,9 +42,6 @@ def get_font(size=80):
|
|
| 45 |
return ImageFont.load_default()
|
| 46 |
|
| 47 |
def research_topic(topic):
|
| 48 |
-
"""
|
| 49 |
-
Retrieves latest news context using Tavily Search API.
|
| 50 |
-
"""
|
| 51 |
try:
|
| 52 |
search_result = tavily.search(query=topic, topic="news", days=2)
|
| 53 |
results = search_result.get('results', [])
|
|
@@ -61,9 +55,6 @@ def research_topic(topic):
|
|
| 61 |
return f"Research failed: {str(e)}"
|
| 62 |
|
| 63 |
def generate_content(platform, topic, research_data):
|
| 64 |
-
"""
|
| 65 |
-
Generates platform-specific copy using the LLM.
|
| 66 |
-
"""
|
| 67 |
if "Twitter" in platform:
|
| 68 |
system_prompt = (
|
| 69 |
"You are a social media ghostwriter. Write a Twitter thread based on the research provided. "
|
|
@@ -92,10 +83,6 @@ def generate_content(platform, topic, research_data):
|
|
| 92 |
return f"Generation failed: {str(e)}"
|
| 93 |
|
| 94 |
def generate_visual_asset(topic, platform):
|
| 95 |
-
"""
|
| 96 |
-
Generates a background image via Flux.1 and overlays the headline text.
|
| 97 |
-
"""
|
| 98 |
-
# Optimized prompt for clean, abstract backgrounds
|
| 99 |
prompt = (
|
| 100 |
f"Abstract 3D render representing {topic}, dark navy and black gradient background, "
|
| 101 |
"glass texture, soft studio lighting, minimalist, 8k resolution, negative space, "
|
|
@@ -108,35 +95,27 @@ def generate_visual_asset(topic, platform):
|
|
| 108 |
draw = ImageDraw.Draw(image)
|
| 109 |
width, height = image.size
|
| 110 |
|
| 111 |
-
# Configure Typography
|
| 112 |
font_size = 90
|
| 113 |
font = get_font(font_size)
|
| 114 |
|
| 115 |
-
# Text Wrapping Logic
|
| 116 |
-
# Wraps text to fit within the image width
|
| 117 |
lines = textwrap.wrap(topic.upper(), width=15)
|
| 118 |
wrapped_text = "\n".join(lines)
|
| 119 |
|
| 120 |
-
# Calculate text bounding box to position the overlay
|
| 121 |
bbox = draw.textbbox((0, 0), wrapped_text, font=font)
|
| 122 |
text_height = bbox[3] - bbox[1]
|
| 123 |
|
| 124 |
-
# Visual Constants
|
| 125 |
padding = 50
|
| 126 |
box_height = text_height + (padding * 3)
|
| 127 |
box_y = height - box_height - 100
|
| 128 |
|
| 129 |
-
# Draw Background Box (Semi-transparent black)
|
| 130 |
draw.rectangle(
|
| 131 |
[(0, box_y), (width, height)],
|
| 132 |
fill=(0, 0, 0, 240)
|
| 133 |
)
|
| 134 |
|
| 135 |
-
# Draw Main Headline
|
| 136 |
text_y = box_y + padding
|
| 137 |
draw.text((padding, text_y), wrapped_text, font=font, fill="white")
|
| 138 |
|
| 139 |
-
# Draw Branding Footer
|
| 140 |
small_font = get_font(30)
|
| 141 |
draw.text((padding, height - 60), f"GENERATED FOR {platform.upper()}", font=small_font, fill="#00ff00")
|
| 142 |
|
|
@@ -145,8 +124,6 @@ def generate_visual_asset(topic, platform):
|
|
| 145 |
print(f"Visual generation error: {e}")
|
| 146 |
return None
|
| 147 |
|
| 148 |
-
# --- Main Application Interface ---
|
| 149 |
-
|
| 150 |
def main():
|
| 151 |
st.title("NewsAgent Pro")
|
| 152 |
st.markdown("Autonomous Multi-Modal Content Engine")
|
|
@@ -166,23 +143,20 @@ def main():
|
|
| 166 |
st.warning("Please enter a topic.")
|
| 167 |
return
|
| 168 |
|
| 169 |
-
|
|
|
|
| 170 |
|
| 171 |
-
# Step 1: Research
|
| 172 |
status.write("Agent: Researching topic...")
|
| 173 |
research_data = research_topic(topic_input)
|
| 174 |
|
| 175 |
-
# Step 2: Content Generation
|
| 176 |
status.write("Agent: Drafting copy...")
|
| 177 |
content_draft = generate_content(platform_choice, topic_input, research_data)
|
| 178 |
|
| 179 |
-
# Step 3: Visual Generation
|
| 180 |
status.write("Agent: Designing assets...")
|
| 181 |
visual_asset = generate_visual_asset(topic_input, platform_choice)
|
| 182 |
|
| 183 |
status.update(label="Workflow Complete", state="complete")
|
| 184 |
|
| 185 |
-
# Persist state
|
| 186 |
st.session_state['content'] = content_draft
|
| 187 |
st.session_state['image'] = visual_asset
|
| 188 |
|
|
@@ -192,7 +166,6 @@ def main():
|
|
| 192 |
if 'image' in st.session_state and st.session_state['image']:
|
| 193 |
st.image(st.session_state['image'], use_column_width=True, caption="Generated Asset")
|
| 194 |
|
| 195 |
-
# Download Button for Image
|
| 196 |
buf = io.BytesIO()
|
| 197 |
st.session_state['image'].save(buf, format="PNG")
|
| 198 |
st.download_button(
|
|
|
|
| 31 |
st.stop()
|
| 32 |
|
| 33 |
def get_font(size=80):
|
|
|
|
|
|
|
|
|
|
| 34 |
try:
|
| 35 |
return ImageFont.truetype("font.ttf", size)
|
| 36 |
except OSError:
|
|
|
|
| 42 |
return ImageFont.load_default()
|
| 43 |
|
| 44 |
def research_topic(topic):
|
|
|
|
|
|
|
|
|
|
| 45 |
try:
|
| 46 |
search_result = tavily.search(query=topic, topic="news", days=2)
|
| 47 |
results = search_result.get('results', [])
|
|
|
|
| 55 |
return f"Research failed: {str(e)}"
|
| 56 |
|
| 57 |
def generate_content(platform, topic, research_data):
|
|
|
|
|
|
|
|
|
|
| 58 |
if "Twitter" in platform:
|
| 59 |
system_prompt = (
|
| 60 |
"You are a social media ghostwriter. Write a Twitter thread based on the research provided. "
|
|
|
|
| 83 |
return f"Generation failed: {str(e)}"
|
| 84 |
|
| 85 |
def generate_visual_asset(topic, platform):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
prompt = (
|
| 87 |
f"Abstract 3D render representing {topic}, dark navy and black gradient background, "
|
| 88 |
"glass texture, soft studio lighting, minimalist, 8k resolution, negative space, "
|
|
|
|
| 95 |
draw = ImageDraw.Draw(image)
|
| 96 |
width, height = image.size
|
| 97 |
|
|
|
|
| 98 |
font_size = 90
|
| 99 |
font = get_font(font_size)
|
| 100 |
|
|
|
|
|
|
|
| 101 |
lines = textwrap.wrap(topic.upper(), width=15)
|
| 102 |
wrapped_text = "\n".join(lines)
|
| 103 |
|
|
|
|
| 104 |
bbox = draw.textbbox((0, 0), wrapped_text, font=font)
|
| 105 |
text_height = bbox[3] - bbox[1]
|
| 106 |
|
|
|
|
| 107 |
padding = 50
|
| 108 |
box_height = text_height + (padding * 3)
|
| 109 |
box_y = height - box_height - 100
|
| 110 |
|
|
|
|
| 111 |
draw.rectangle(
|
| 112 |
[(0, box_y), (width, height)],
|
| 113 |
fill=(0, 0, 0, 240)
|
| 114 |
)
|
| 115 |
|
|
|
|
| 116 |
text_y = box_y + padding
|
| 117 |
draw.text((padding, text_y), wrapped_text, font=font, fill="white")
|
| 118 |
|
|
|
|
| 119 |
small_font = get_font(30)
|
| 120 |
draw.text((padding, height - 60), f"GENERATED FOR {platform.upper()}", font=small_font, fill="#00ff00")
|
| 121 |
|
|
|
|
| 124 |
print(f"Visual generation error: {e}")
|
| 125 |
return None
|
| 126 |
|
|
|
|
|
|
|
| 127 |
def main():
|
| 128 |
st.title("NewsAgent Pro")
|
| 129 |
st.markdown("Autonomous Multi-Modal Content Engine")
|
|
|
|
| 143 |
st.warning("Please enter a topic.")
|
| 144 |
return
|
| 145 |
|
| 146 |
+
# FIXED VARIABLE NAME HERE
|
| 147 |
+
status = st.status("Initializing Agent Workflow...", expanded=True)
|
| 148 |
|
|
|
|
| 149 |
status.write("Agent: Researching topic...")
|
| 150 |
research_data = research_topic(topic_input)
|
| 151 |
|
|
|
|
| 152 |
status.write("Agent: Drafting copy...")
|
| 153 |
content_draft = generate_content(platform_choice, topic_input, research_data)
|
| 154 |
|
|
|
|
| 155 |
status.write("Agent: Designing assets...")
|
| 156 |
visual_asset = generate_visual_asset(topic_input, platform_choice)
|
| 157 |
|
| 158 |
status.update(label="Workflow Complete", state="complete")
|
| 159 |
|
|
|
|
| 160 |
st.session_state['content'] = content_draft
|
| 161 |
st.session_state['image'] = visual_asset
|
| 162 |
|
|
|
|
| 166 |
if 'image' in st.session_state and st.session_state['image']:
|
| 167 |
st.image(st.session_state['image'], use_column_width=True, caption="Generated Asset")
|
| 168 |
|
|
|
|
| 169 |
buf = io.BytesIO()
|
| 170 |
st.session_state['image'].save(buf, format="PNG")
|
| 171 |
st.download_button(
|