Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel | |
| # Initialize the blog-writing agent with DuckDuckGo tool | |
| blog_agent = CodeAgent( | |
| tools=[DuckDuckGoSearchTool()], | |
| model=HfApiModel() | |
| ) | |
| # Function to display agent activity | |
| def display_agent_activity(prompt, result): | |
| st.write("### Agent Activity:") | |
| st.write("**Prompt Sent to Agent:**") | |
| st.code(prompt, language="text") | |
| st.write("**Agent Output:**") | |
| st.code(result, language="text") | |
| # Function to save generated content to a file | |
| def save_to_file(content, filename="generated_blog.txt"): | |
| with open(filename, "w") as file: | |
| file.write(content) | |
| return filename | |
| # Function to enhance generated content | |
| def enhance_content(content): | |
| return f"{content}\n\n---\nGenerated with insights and enhanced formatting." | |
| # Streamlit app title | |
| st.title("AI-Powered Blog Writer") | |
| # App description | |
| st.write("Generate high-quality blog content enriched with real-time insights.") | |
| # Input field for blog topic or prompt | |
| blog_prompt = st.text_area("Enter your blog topic or prompt:", placeholder="E.g., The Future of AI in Healthcare") | |
| # Input field for choosing content tone | |
| tone = st.selectbox("Choose the tone of the content:", ["Professional", "Casual", "Persuasive", "Informative"]) | |
| # Option to include a summary | |
| include_summary = st.checkbox("Include a summary of the generated content") | |
| # Button to trigger blog content generation | |
| if st.button("Generate Blog Content"): | |
| if blog_prompt: | |
| with st.spinner("Generating blog content, please wait..."): | |
| try: | |
| # Generate blog content using the agent | |
| blog_result = blog_agent.run(f"{blog_prompt}\nTone: {tone}") | |
| # Optionally enhance the content | |
| blog_result = enhance_content(blog_result) | |
| # Display the generated blog content | |
| st.subheader("Generated Blog Content") | |
| st.write(blog_result) | |
| # Display a summary if requested | |
| if include_summary: | |
| summary_prompt = f"Summarize this content:\n{blog_result}" | |
| summary_result = blog_agent.run(summary_prompt) | |
| st.subheader("Content Summary") | |
| st.write(summary_result) | |
| # Log and display agent activity | |
| display_agent_activity(blog_prompt, blog_result) | |
| # Save the content to a file | |
| filename = save_to_file(blog_result) | |
| st.success(f"Content saved to {filename}") | |
| except Exception as e: | |
| st.error("An error occurred while generating content. Please try again.") | |
| else: | |
| st.warning("Please enter a valid blog topic or prompt.") | |