File size: 4,571 Bytes
dcdbbad c209a82 dcdbbad 6ae9cf7 50aa4eb 1630005 50aa4eb 1630005 c209a82 dcdbbad 50aa4eb c209a82 dcdbbad 1630005 dcdbbad 50aa4eb dcdbbad 50aa4eb c209a82 dcdbbad 50aa4eb c209a82 dcdbbad f85b832 dcdbbad 50aa4eb c209a82 50aa4eb dcdbbad 1630005 c209a82 50aa4eb c209a82 dcdbbad c209a82 46d13ed dcdbbad 46d13ed dcdbbad f85b832 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
import os
import streamlit as st
from groq import Groq
from streamlit.components.v1 import html
# Load Groq API key from environment variable
groq_api_key = os.getenv('GROQ_API_KEY')
if not groq_api_key:
st.error("GROQ_API_KEY environment variable is not set. Please set it and restart the app.")
st.stop()
# Initialize Groq client
groq_client = Groq(api_key=groq_api_key)
# Few-shot examples for inspiration
few_shot_examples = """
π’ **Data Scaling Showdown: Normalization vs Standardization!** π
...
"""
# Function to get Groq response with few-shot examples
def get_groq_response_with_few_shot(post_type, user_input):
if not user_input:
return "Please enter a valid query."
query_content = f"Here are a few examples of fun and engaging social media posts about data science:\n{few_shot_examples}\nNow generate a {post_type} post about {user_input} in a similar style."
try:
# Make the API call with few-shot examples included
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)}"
# Streamlit UI
st.title("Social Media Post Maker for Data Science")
# Initialize response
response = None
# User input
post_type = st.selectbox("Select the type of post:", [
"Template-Based Post",
"Data Science Meme/Quote",
"Trend Explainer",
"Data Science Tip",
"Hashtag Suggestion",
"Poll/Question Maker",
"Project Showcase",
"Data Visual Explanation"
])
user_input = st.text_input("Enter the topic or idea for the post:")
# Generate button
if st.button("Generate Post"):
if user_input:
with st.spinner("Generating post..."):
response = get_groq_response_with_few_shot(post_type, user_input)
if "An error occurred" in response:
st.error(response)
else:
st.success("Post generated successfully!")
else:
st.error("Please enter a topic or idea!")
# Display the result
if response:
st.code(response)
# Add a Copy to Clipboard button using HTML and JavaScript
copy_button_code = f"""
<button class="copy-btn" onclick="copyToClipboard()">Copy to Clipboard</button>
<script>
function copyToClipboard() {{
var text = `{response}`;
var tempInput = document.createElement("textarea");
document.body.appendChild(tempInput);
tempInput.value = text;
tempInput.select();
document.execCommand("copy");
document.body.removeChild(tempInput);
alert("Copied to clipboard!");
}}
</script>
<style>
.copy-btn {{
background-color: #007bff;
color: white;
border: none;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
border-radius: 4px;
}}
.copy-btn:hover {{
background-color: #0056b3;
}}
</style>
"""
html(copy_button_code)
# Example Button
if st.button("Get Example Post"):
example_post_type = "Data Science Tip"
example_user_input = "importance of data preprocessing"
example_response = get_groq_response_with_few_shot(example_post_type, example_user_input)
st.code(example_response)
# Add a Copy to Clipboard button for the example post
copy_example_button_code = f"""
<button class="copy-btn" onclick="copyToClipboard()">Copy Example Post</button>
<script>
function copyToClipboard() {{
var text = `{example_response}`;
var tempInput = document.createElement("textarea");
document.body.appendChild(tempInput);
tempInput.value = text;
tempInput.select();
document.execCommand("copy");
document.body.removeChild(tempInput);
alert("Copied to clipboard!");
}}
</script>
<style>
.copy-btn {{
background-color: #007bff;
color: white;
border: none;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
border-radius: 4px;
}}
.copy-btn:hover {{
background-color: #0056b3;
}}
</style>
"""
html(copy_example_button_code)
|