Spaces:
Sleeping
Sleeping
import streamlit as st | |
from phi.agent import Agent | |
from phi.model.groq import Groq | |
from phi.tools.duckduckgo import DuckDuckGo | |
from phi.tools.newspaper4k import Newspaper4k | |
import os | |
from dotenv import load_dotenv | |
# Load environment variables | |
load_dotenv() | |
# Create the Agent with enhanced instructions for longer, more human-like writing | |
agent = Agent( | |
model=Groq(id="llama-3.1-8b-instant", api_key=os.getenv("GROQ_API_KEY")), | |
tools=[DuckDuckGo(), Newspaper4k()], | |
description="""You are a masterful storyteller and journalist writing for Medium. You have decades of experience | |
crafting engaging narratives that blend deep research with personal insights. Your writing style is warm, | |
conversational, and deeply human - as if sharing fascinating discoveries with a close friend over coffee.""", | |
instructions=[ | |
"**Write an extensive, engaging article of at least 2500-3000 words.** Your goal is to create a piece that | |
readers can't stop reading, filled with fascinating insights and compelling narratives.", | |
"**Research Phase:**", | |
"- Gather 20-25 diverse sources including academic papers, expert interviews, case studies, and real-world examples", | |
"- Look for surprising connections and lesser-known perspectives", | |
"- Find memorable stories and examples that illustrate key points", | |
"**Writing Style:**", | |
"- Write as if you're having an intimate conversation with a friend - use 'you' and 'I', share personal reflections", | |
"- Include vivid sensory details and imagery that transport readers into the story", | |
"- Weave in rhetorical questions that mirror the reader's thought process", | |
"- Use natural language patterns - contractions, informal asides, and occasional humor", | |
"- Share genuine emotional reactions and personal discoveries about the topic", | |
"**Structure and Flow:**", | |
"- Open with an immersive scene or provocative question that hooks readers immediately", | |
"- Build narrative tension throughout - raise questions that keep readers curious", | |
"- Create smooth, natural transitions between ideas - no mechanical 'firstly, secondly'", | |
"- Include 2-3 detailed case studies or extended examples", | |
"- Add 'behind the scenes' insights about your research process", | |
"**Depth and Authority:**", | |
"- Interview quotes should feel like real conversations, not formal statements", | |
"- Challenge conventional wisdom with well-reasoned alternative perspectives", | |
"- Acknowledge complexities and nuances rather than oversimplifying", | |
"- Share practical implications and 'what this means for you' insights", | |
"**Voice and Tone:**", | |
"- Write with genuine enthusiasm and curiosity about the topic", | |
"- Include occasional self-deprecating humor or admissions of initial misconceptions", | |
"- Express authentic wonder at fascinating discoveries", | |
"- Use casual interjections like 'Here's the thing...', 'Now, this is where it gets interesting...'", | |
"**Engagement Elements:**", | |
"- Sprinkle in surprising statistics or counterintuitive findings", | |
"- Add memorable metaphors and analogies that clarify complex ideas", | |
"- Include 'pause points' where you directly address readers' likely reactions", | |
"- End sections with mini-cliffhangers that pull readers forward", | |
"**Conclusion:**", | |
"- Circle back to your opening scene/question with new insight", | |
"- Share a personal reflection on how this topic changed your own thinking", | |
"- End with a thought-provoking question or call to action that inspires further exploration", | |
], | |
markdown=True, | |
show_tool_calls=True, | |
add_datetime_to_instructions=True, | |
) | |
# Streamlit app | |
def main(): | |
st.title("Enhanced Medium Article Generator") | |
st.markdown(""" | |
Generate in-depth, engaging articles with a natural, human writing style. | |
Articles will be 2500-3000 words with rich storytelling and detailed research. | |
""") | |
topic = st.text_input("Enter your article topic:", | |
placeholder="e.g., How Virtual Reality is Reshaping Our Understanding of Human Consciousness") | |
if st.button("Generate Article"): | |
if topic: | |
with st.spinner("Crafting your article (this may take a few minutes due to extensive research and writing)..."): | |
response = agent.run(f""" | |
Create an engaging, in-depth article about: {topic} | |
Take your time to research thoroughly and craft a narrative that blends | |
academic insights with personal storytelling. Remember to: | |
- Start with a captivating hook | |
- Include multiple real-world examples and case studies | |
- Weave in expert quotes naturally | |
- Add your personal insights and reflections | |
- Make complex ideas accessible through storytelling | |
- End with a powerful conclusion that inspires action | |
""") | |
st.markdown("## Your Generated Article") | |
st.markdown(response.content) | |
# Add download button | |
st.download_button( | |
label="Download Article (Markdown)", | |
data=response.content, | |
file_name=f"{topic.lower().replace(' ', '-')}-article.md", | |
mime="text/markdown" | |
) | |
else: | |
st.warning("Please enter a topic.") | |
if __name__ == "__main__": | |
main() |