ibrahim313
commited on
Commit
β’
dcdbbad
1
Parent(s):
c202349
Update app.py
Browse files
app.py
CHANGED
@@ -1,116 +1,73 @@
|
|
|
|
1 |
import streamlit as st
|
2 |
-
from streamlit.components.v1 import html
|
3 |
from groq import Groq
|
4 |
-
import
|
5 |
|
6 |
# Load Groq API key from environment variable
|
7 |
groq_api_key = os.getenv('api_key')
|
8 |
if not groq_api_key:
|
9 |
raise ValueError("GROQ_API_KEY environment variable is not set.")
|
10 |
|
11 |
-
# Initialize Groq client
|
12 |
groq_client = Groq(api_key=groq_api_key)
|
13 |
|
14 |
-
#
|
15 |
-
|
|
|
16 |
|
17 |
-
|
18 |
-
css = """
|
19 |
-
<style>
|
20 |
-
body {
|
21 |
-
background-color: #f0f2f6;
|
22 |
-
font-family: 'Arial', sans-serif;
|
23 |
-
}
|
24 |
-
.stTextInput, .stButton {
|
25 |
-
margin-top: 20px;
|
26 |
-
}
|
27 |
-
.custom-box {
|
28 |
-
padding: 20px;
|
29 |
-
background-color: #ffffff;
|
30 |
-
border-radius: 8px;
|
31 |
-
box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1);
|
32 |
-
}
|
33 |
-
.error-msg {
|
34 |
-
color: red;
|
35 |
-
font-weight: bold;
|
36 |
-
}
|
37 |
-
.copy-btn {
|
38 |
-
background-color: #4CAF50;
|
39 |
-
color: white;
|
40 |
-
border: none;
|
41 |
-
padding: 10px 20px;
|
42 |
-
border-radius: 5px;
|
43 |
-
cursor: pointer;
|
44 |
-
}
|
45 |
-
.copy-btn:hover {
|
46 |
-
background-color: #45a049;
|
47 |
-
}
|
48 |
-
</style>
|
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 |
if not user_input:
|
83 |
return "Please enter a valid query."
|
84 |
|
85 |
-
query_content = ""
|
86 |
-
|
87 |
-
# Match the post type with specific tasks
|
88 |
-
if post_type == "Template-Based Post Generator":
|
89 |
-
query_content = f"Generate a social media post about {user_input}."
|
90 |
-
|
91 |
-
elif post_type == "Data Science Meme/Quote Bot":
|
92 |
-
query_content = f"Generate a data science meme or quote about {user_input}."
|
93 |
-
|
94 |
-
elif post_type == "Trend Explainer Bot":
|
95 |
-
query_content = f"Explain the trend '{user_input}' in data science in a concise way."
|
96 |
-
|
97 |
-
elif post_type == "Data Science Tip Bot":
|
98 |
-
query_content = f"Give a useful data science tip about {user_input}."
|
99 |
-
|
100 |
-
elif post_type == "Hashtag Suggestion Bot":
|
101 |
-
query_content = f"Suggest relevant hashtags for {user_input}."
|
102 |
-
|
103 |
-
elif post_type == "Poll/Question Maker":
|
104 |
-
query_content = f"Generate a poll or question about {user_input} for social media."
|
105 |
-
|
106 |
-
elif post_type == "Project Showcase Post Generator":
|
107 |
-
query_content = f"Create a social media post to showcase my data science project on {user_input}."
|
108 |
-
|
109 |
-
elif post_type == "Data Visual Explanation Bot":
|
110 |
-
query_content = f"Generate a caption or explanation for a data visualization about {user_input}."
|
111 |
|
112 |
try:
|
113 |
-
# Make API call
|
114 |
chat_completion = groq_client.chat.completions.create(
|
115 |
messages=[{"role": "user", "content": query_content}],
|
116 |
model="llama-3.1-70b-versatile",
|
@@ -119,17 +76,33 @@ def get_groq_response(post_type, user_input):
|
|
119 |
except Exception as e:
|
120 |
return f"An error occurred: {str(e)}"
|
121 |
|
122 |
-
#
|
123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
if st.button("Generate Post"):
|
125 |
if user_input:
|
126 |
with st.spinner("Generating post..."):
|
127 |
-
response =
|
128 |
st.success("Post generated successfully!")
|
129 |
else:
|
130 |
st.error("Please enter a topic or idea!")
|
131 |
|
132 |
-
#
|
133 |
if response:
|
134 |
st.code(response)
|
135 |
|
@@ -148,6 +121,24 @@ if response:
|
|
148 |
alert("Copied to clipboard!");
|
149 |
}}
|
150 |
</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
"""
|
152 |
-
|
153 |
-
html(copy_button_code)
|
|
|
1 |
+
import os
|
2 |
import streamlit as st
|
|
|
3 |
from groq import Groq
|
4 |
+
from streamlit.components.v1 import html
|
5 |
|
6 |
# Load Groq API key from environment variable
|
7 |
groq_api_key = os.getenv('api_key')
|
8 |
if not groq_api_key:
|
9 |
raise ValueError("GROQ_API_KEY environment variable is not set.")
|
10 |
|
11 |
+
# Initialize Groq client
|
12 |
groq_client = Groq(api_key=groq_api_key)
|
13 |
|
14 |
+
# Few-shot examples for inspiration
|
15 |
+
few_shot_examples = """
|
16 |
+
π’ **Data Scaling Showdown: Normalization vs Standardization!** π
|
17 |
|
18 |
+
Ever felt like your data is on a rollercoaster? π‘ Some values reaching for the stars, others barely off the ground? Time to level the playing field with the dynamic duo of data scaling: **Normalization** and **Standardization!** πͺ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
π **Normalization: The Range Ranger**
|
21 |
+
πΈ What: Squeezes your data into a cozy 0-1 range.
|
22 |
+
πΈ Why: Perfect for when your features are as different as apples and skyscrapers! πποΈ
|
23 |
+
πΈ Best for: k-NN, neural networks, and data that's living its best non-normal life.
|
24 |
+
πΈ Superpower: Taming wild data ranges without breaking a sweat!
|
25 |
|
26 |
+
π **Standardization: The Gaussian Don**
|
27 |
+
π· What: Centers your data around 0, with a standard deviation of 1.
|
28 |
+
π· Why: Ideal when your data follows the bell curve or your algorithm is a normal distribution fan.
|
29 |
+
π· Best for: Logistic regression, SVM, and PCA - the cool kids of the ML world.
|
30 |
+
π· Superpower: Handling outliers like a boss!
|
31 |
+
|
32 |
+
π₯ **The Face-Off**
|
33 |
+
**Normalization:** "Let's all fit in this box!" π¦
|
34 |
+
**Standardization:** "Let's dance around the mean!" π
|
35 |
+
|
36 |
+
Use Normalization for diverse data like a candy store π, and Standardization when your data fits the normal crowd π.
|
37 |
+
|
38 |
+
#DataScience #NormalizationVsStandardization #AITricks
|
39 |
+
|
40 |
+
---
|
41 |
+
π― **Binary Classification or Multiclass? Here's How to Choose!** π―
|
42 |
+
|
43 |
+
Choosing between **binary** and **multiclass classification** can feel like picking the perfect dessertβdo you want the classic **chocolate cake** or an **exotic fruit tart**?
|
44 |
+
|
45 |
+
**Binary Classification:**
|
46 |
+
πΈ Focuses on **two classes**: cat or dog, pass or fail, true or false.
|
47 |
+
πΈ **Simpler** to model, quick to train, and widely used when the task is a straightforward **this or that** decision.
|
48 |
+
|
49 |
+
**Multiclass Classification:**
|
50 |
+
π· Handles **more than two classes**: apple, orange, banana, or even dragon fruit!
|
51 |
+
π· Complex but powerful when you need **multiple predictions** from a single model, often used in tasks like image recognition.
|
52 |
+
|
53 |
+
---
|
54 |
+
|
55 |
+
π§ **How to Ace Hyperparameter Tuning!**
|
56 |
+
|
57 |
+
Tuning hyperparameters can feel like adjusting the knobs on an old radioβget it just right and the music plays beautifully. πΆ
|
58 |
+
|
59 |
+
#AI #DataScienceTips #MLHacks
|
60 |
+
"""
|
61 |
+
|
62 |
+
# Function to get Groq response with few-shot examples
|
63 |
+
def get_groq_response_with_few_shot(post_type, user_input):
|
64 |
if not user_input:
|
65 |
return "Please enter a valid query."
|
66 |
|
67 |
+
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."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
try:
|
70 |
+
# Make the API call with few-shot examples included
|
71 |
chat_completion = groq_client.chat.completions.create(
|
72 |
messages=[{"role": "user", "content": query_content}],
|
73 |
model="llama-3.1-70b-versatile",
|
|
|
76 |
except Exception as e:
|
77 |
return f"An error occurred: {str(e)}"
|
78 |
|
79 |
+
# Streamlit UI
|
80 |
+
st.title("Social Media Post Maker for Data Science")
|
81 |
+
|
82 |
+
# User input
|
83 |
+
post_type = st.selectbox("Select the type of post:", [
|
84 |
+
"Template-Based Post",
|
85 |
+
"Data Science Meme/Quote",
|
86 |
+
"Trend Explainer",
|
87 |
+
"Data Science Tip",
|
88 |
+
"Hashtag Suggestion",
|
89 |
+
"Poll/Question Maker",
|
90 |
+
"Project Showcase",
|
91 |
+
"Data Visual Explanation"
|
92 |
+
])
|
93 |
+
|
94 |
+
user_input = st.text_input("Enter the topic or idea for the post:")
|
95 |
+
|
96 |
+
# Generate button
|
97 |
if st.button("Generate Post"):
|
98 |
if user_input:
|
99 |
with st.spinner("Generating post..."):
|
100 |
+
response = get_groq_response_with_few_shot(post_type, user_input)
|
101 |
st.success("Post generated successfully!")
|
102 |
else:
|
103 |
st.error("Please enter a topic or idea!")
|
104 |
|
105 |
+
# Display the result
|
106 |
if response:
|
107 |
st.code(response)
|
108 |
|
|
|
121 |
alert("Copied to clipboard!");
|
122 |
}}
|
123 |
</script>
|
124 |
+
<style>
|
125 |
+
.copy-btn {{
|
126 |
+
background-color: #007bff;
|
127 |
+
color: white;
|
128 |
+
border: none;
|
129 |
+
padding: 10px 20px;
|
130 |
+
text-align: center;
|
131 |
+
text-decoration: none;
|
132 |
+
display: inline-block;
|
133 |
+
font-size: 16px;
|
134 |
+
margin: 4px 2px;
|
135 |
+
cursor: pointer;
|
136 |
+
border-radius: 4px;
|
137 |
+
}}
|
138 |
+
.copy-btn:hover {{
|
139 |
+
background-color: #0056b3;
|
140 |
+
}}
|
141 |
+
</style>
|
142 |
"""
|
143 |
+
|
144 |
+
html(copy_button_code)
|