import streamlit as st
from streamlit.components.v1 import html
from groq import Groq
import os
# Load Groq API key from environment variable
groq_api_key = os.getenv('api_key')
if not groq_api_key:
raise ValueError("GROQ_API_KEY environment variable is not set.")
# Initialize Groq client with API key
groq_client = Groq(api_key=groq_api_key)
# Set the title of the app
st.title("Social Media Post Maker for Data Science")
# Custom CSS for a professional look
css = """
"""
html(css)
st.markdown(
"""
Generate Data Science Posts
Create engaging social media content with AI! 🚀💻
""", unsafe_allow_html=True
)
# Select the type of post to generate
post_type = st.selectbox(
"What type of post would you like to create?",
(
"Template-Based Post Generator",
"Data Science Meme/Quote Bot",
"Trend Explainer Bot",
"Data Science Tip Bot",
"Hashtag Suggestion Bot",
"Poll/Question Maker",
"Project Showcase Post Generator",
"Data Visual Explanation Bot"
)
)
# Input for user queries
user_input = st.text_input("Describe your data science topic or idea:")
# Function to handle Groq API call for different tasks
def get_groq_response(post_type, user_input):
if not user_input:
return "Please enter a valid query."
query_content = ""
# Match the post type with specific tasks
if post_type == "Template-Based Post Generator":
query_content = f"Generate a social media post about {user_input}."
elif post_type == "Data Science Meme/Quote Bot":
query_content = f"Generate a data science meme or quote about {user_input}."
elif post_type == "Trend Explainer Bot":
query_content = f"Explain the trend '{user_input}' in data science in a concise way."
elif post_type == "Data Science Tip Bot":
query_content = f"Give a useful data science tip about {user_input}."
elif post_type == "Hashtag Suggestion Bot":
query_content = f"Suggest relevant hashtags for {user_input}."
elif post_type == "Poll/Question Maker":
query_content = f"Generate a poll or question about {user_input} for social media."
elif post_type == "Project Showcase Post Generator":
query_content = f"Create a social media post to showcase my data science project on {user_input}."
elif post_type == "Data Visual Explanation Bot":
query_content = f"Generate a caption or explanation for a data visualization about {user_input}."
try:
# Make API call to Groq LLaMA model
chat_completion = groq_client.chat.completions.create(
messages=[{"role": "user", "content": query_content}],
model="llama-3.1-70b-versatile",
)
return chat_completion.choices[0].message.content
except Exception as e:
return f"An error occurred: {str(e)}"
# Submit button to generate the post
response = None
if st.button("Generate Post"):
if user_input:
with st.spinner("Generating post..."):
response = get_groq_response(post_type, user_input)
st.success("Post generated successfully!")
else:
st.error("Please enter a topic or idea!")
# Allow users to copy the generated post
if response:
st.code(response)
# Add a Copy to Clipboard button using HTML and JavaScript
copy_button_code = f"""
"""
html(copy_button_code, unsafe_allow_html=True)