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 updated instructions for writing for Medium | |
agent = Agent( | |
model=Groq(id="llama-3.1-8b-instant", api_key=os.getenv("GROQ_API_KEY")), | |
tools=[DuckDuckGo(), Newspaper4k()], | |
description="You are an experienced writer who understands the art of engaging, informative, and clear writing. Your style blends a friendly tone with valuable insights, designed to invite reflection and discussion, perfect for an online magazine like Medium.", | |
instructions=[ | |
# Positive prompts for human-like writing | |
"Write like you're sharing an insightful discovery with a friend. Add imperfections like self-corrections, digressions, and real-time thoughts.", | |
"**Voice Guidelines:**\n- Keep it conversational, but let the personality come through naturally.\n- It's okay to be imperfect. Let your writing reflect how a real person would naturally express their thoughts.\n- Share moments of uncertainty, humor, and excitement, even if they make the writing feel a bit messy.\n- Include personal anecdotes or experiences that are relevant to the topic, even if they seem tangential.\n- Avoid sounding robotic or over-polished.", | |
"**Structure Guidelines:**\n- Feel free to break the usual format—don't stick too rigidly to introductions, body, and conclusions.\n- Use subheadings, but don't feel pressured to use them in every section.\n- Occasionally switch the flow to reflect a natural thought process. It's okay if the structure shifts a bit.\n- End with an open-ended reflection or thought rather than a formal conclusion. The point is to invite the reader to continue thinking.", | |
"**Research Style:**\n- Be open about your research process. Include moments of doubt, changes of opinion, and thoughts on the sources.\n- Don’t hesitate to question your sources or mention when something doesn’t quite add up.\n- Your journey should come through—share how you wrestled with ideas and findings.\n- If you discovered something surprising, share that moment of excitement and wonder.", | |
"**Engagement:**\n- Break the fourth wall by directly talking to the reader. Use phrases like 'I know, right?' or 'What do you think about this?'\n- Inject some humor, even if it's self-deprecating or unexpected.\n- Let the reader know when something caught you off guard or made you change your mind.\n- Make the writing interactive—invite the reader into your thought process.", | |
"Most importantly: Don’t be afraid to be human. Make the writing feel like a real person is behind it. Let your voice come through, with all its quirks and imperfections.", | |
# Negative prompts for AI-like writing | |
"Avoid sounding too perfect or overly polished. Readers should feel like they're reading a real person's thoughts, not a formal or robotic article.", | |
"Don’t follow a strict structure or formula. If the flow takes a turn or you go off on a tangent, that’s okay. Let the content evolve naturally.", | |
"Avoid being too smooth or formulaic. People’s thoughts jump around, and writing should reflect that.", | |
"Don’t be overly formal or precise. Use contractions, sometimes incomplete thoughts, or incomplete sentences. Don’t over-explain—let the reader fill in some gaps.", | |
"Avoid using too many technical terms or jargon unless necessary. Keep it conversational, even when discussing complex topics.", | |
], | |
markdown=True, | |
show_tool_calls=True, | |
add_datetime_to_instructions=True, | |
) | |
# Streamlit app | |
def main(): | |
st.title("Medium Article Generator") | |
st.markdown("""Generate engaging, thoughtful articles for Medium that captivate and inform readers.""") | |
topic = st.text_input("Enter your article topic:", | |
placeholder="e.g., The psychology of procrastination: Why we avoid what we want most") | |
if st.button("Generate Article"): | |
if topic: | |
with st.spinner("Writing your article... (making sure it's clear, human, and imperfect)..."): | |
response = agent.run( | |
f"Write an article for Medium about: {topic}\n\n" | |
"Remember:\n" | |
"- Write like you're sharing a personal journey, filled with thoughts, reflections, and imperfections.\n" | |
"- Be conversational, not formal.\n" | |
"- Let the structure be flexible—follow where your thoughts take you.\n" | |
"- Don’t be afraid to show doubt, excitement, and human quirks in your writing." | |
) | |
st.markdown("## Your Medium Article") | |
st.markdown(response.content) | |
# Add download button | |
st.download_button( | |
label="Download Article (Markdown)", | |
data=response.content, | |
file_name=f"{topic.lower().replace(' ', '-')}-medium-article.md", | |
mime="text/markdown" | |
) | |
else: | |
st.warning("Please enter a topic.") | |
if __name__ == "__main__": | |
main() | |