Final design is complete for now, hope you guys like it
Browse files
app.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
# Hey its your saiyan Utkarsh Shukla. I gonna write my custom comments after each line of code. So even a beginner (previous me) can read and get whats going on.
|
| 2 |
# Bear with me, I got your Back buddy
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
import streamlit as st
|
| 6 |
# The saviour web app creator, easy peasy web app creation by few lines of codes.
|
| 7 |
# No HTML, CSS, or JS needed!
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
@@ -25,6 +27,7 @@ from wordcloud import WordCloud
|
|
| 25 |
# Mostly it avoids our stop words like it, is, are etc
|
| 26 |
# More frequency = more importance → bigger word in the cloud.
|
| 27 |
|
|
|
|
| 28 |
import matplotlib.pyplot as plt
|
| 29 |
# This guy helps us to plot. So wait till you see it.
|
| 30 |
# We’ll use it to show our Word Cloud in style.
|
|
@@ -46,7 +49,7 @@ def load_model_and_tokenizer():
|
|
| 46 |
model_name = "google/gemma-2b" # using gemma-2b for prototype for my GSOC Proposal. Wish me luck.
|
| 47 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 48 |
# Responsible for automatically downloading and loading the tokenizer configuration and vocabulary associated with the specified pre-trained model.
|
| 49 |
-
|
| 50 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 51 |
# As we discussed, this class is designed for loading pre-trained language models for causal (next-token prediction) tasks.
|
| 52 |
# Loads the actual model used for causal (next-word) prediction tasks
|
|
@@ -57,10 +60,11 @@ def load_model_and_tokenizer():
|
|
| 57 |
def generate_text(prompt, tone, max_length, temperature=0.7, top_p=0.9, repetition_penalty=1.0):
|
| 58 |
tokenizer, model = load_model_and_tokenizer()
|
| 59 |
# Adjust prompt based on tone
|
|
|
|
| 60 |
tone_prompts = {
|
| 61 |
-
"Funny": f"Generate a
|
| 62 |
-
"Serious": f"Provide a
|
| 63 |
-
"Poetic": f"Write a poetic response to: {prompt}"
|
| 64 |
}
|
| 65 |
input_text = tone_prompts.get(tone, prompt)
|
| 66 |
|
|
@@ -76,168 +80,369 @@ def generate_text(prompt, tone, max_length, temperature=0.7, top_p=0.9, repetiti
|
|
| 76 |
)
|
| 77 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 78 |
|
| 79 |
-
#
|
|
|
|
| 80 |
st.markdown("""
|
| 81 |
<style>
|
| 82 |
-
/*
|
| 83 |
.stApp {
|
| 84 |
-
background:
|
| 85 |
-
|
| 86 |
-
background-position: center;
|
| 87 |
-
color: #ffffff; /* White text for contrast */
|
| 88 |
}
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
-
|
| 93 |
-
|
| 94 |
-
font-size: 40px;
|
| 95 |
-
font-weight: bold;
|
| 96 |
-
transition: transform 0.3s;
|
| 97 |
}
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
}
|
| 101 |
-
|
|
|
|
| 102 |
.instructions {
|
| 103 |
-
background: rgba(255, 255, 255, 0.
|
| 104 |
-
padding:
|
| 105 |
-
border-radius:
|
| 106 |
-
box-shadow: 0
|
| 107 |
-
font-size:
|
| 108 |
-
color: #
|
|
|
|
|
|
|
|
|
|
| 109 |
}
|
| 110 |
-
|
|
|
|
| 111 |
.output-box {
|
| 112 |
-
background: rgba(
|
| 113 |
-
padding:
|
| 114 |
-
border-radius:
|
| 115 |
-
box-shadow: 0
|
| 116 |
font-family: 'Courier New', monospace;
|
| 117 |
font-size: 16px;
|
| 118 |
color: #ffffff;
|
| 119 |
white-space: pre-wrap;
|
| 120 |
-
|
|
|
|
| 121 |
}
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
}
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
.stButton>button {
|
| 128 |
-
background: #
|
| 129 |
color: white;
|
| 130 |
-
border
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
}
|
| 133 |
.stButton>button:hover {
|
| 134 |
-
background: #
|
| 135 |
transform: translateY(-2px);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
}
|
| 137 |
-
|
|
|
|
| 138 |
.stSlider>div>div>div {
|
| 139 |
-
background: #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
}
|
| 141 |
</style>
|
| 142 |
""", unsafe_allow_html=True)
|
| 143 |
|
| 144 |
-
# Header with GSoC logo
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
| 150 |
|
| 151 |
|
| 152 |
-
#
|
| 153 |
st.markdown("""
|
| 154 |
-
<
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
</
|
| 158 |
""", unsafe_allow_html=True)
|
| 159 |
|
| 160 |
|
| 161 |
-
#
|
| 162 |
-
with st.expander("
|
| 163 |
st.markdown("""
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
if "trigger_example" not in st.session_state:
|
| 173 |
st.session_state.trigger_example = False
|
| 174 |
|
| 175 |
-
col1, col2 = st.columns(2)
|
| 176 |
with col1:
|
| 177 |
-
if st.button("
|
| 178 |
st.session_state.prompt = "The cat hacked my WiFi"
|
| 179 |
st.session_state.tone = "Funny"
|
| 180 |
st.session_state.trigger_example = True
|
| 181 |
with col2:
|
| 182 |
-
if st.button("
|
| 183 |
st.session_state.prompt = "As the sun set on our final day"
|
| 184 |
st.session_state.tone = "Poetic"
|
| 185 |
st.session_state.trigger_example = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
# User input section
|
| 191 |
with st.form(key="input_form"):
|
|
|
|
|
|
|
| 192 |
prompt = st.text_input("Enter a prompt", placeholder="e.g., 'The future of AI is'", value=st.session_state.get("prompt", ""))
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
submit_button = st.form_submit_button(label="Generate")
|
| 200 |
|
| 201 |
# Generate and display output
|
| 202 |
if submit_button or st.session_state.trigger_example:
|
| 203 |
-
st.session_state.trigger_example = False
|
| 204 |
if not prompt:
|
| 205 |
st.error("Please enter a prompt!")
|
| 206 |
else:
|
| 207 |
with st.spinner("Generating text..."):
|
| 208 |
output = generate_text(prompt, tone, max_length, temperature, top_p, repetition_penalty)
|
| 209 |
-
|
|
|
|
| 210 |
st.markdown(f"""
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
|
| 229 |
-
# Generate
|
| 230 |
-
wordcloud = WordCloud(
|
| 231 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
plt.imshow(wordcloud, interpolation="bilinear")
|
| 233 |
plt.axis("off")
|
| 234 |
st.pyplot(plt)
|
|
|
|
| 235 |
|
| 236 |
-
#
|
| 237 |
-
st.markdown("
|
| 238 |
-
|
| 239 |
-
with
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
|
|
|
|
|
|
|
|
| 1 |
# Hey its your saiyan Utkarsh Shukla. I gonna write my custom comments after each line of code. So even a beginner (previous me) can read and get whats going on.
|
| 2 |
# Bear with me, I got your Back buddy
|
| 3 |
+
# And hey don't be overwhelmed by lines of code as most part is just covering design.
|
| 4 |
|
| 5 |
|
| 6 |
import streamlit as st
|
| 7 |
# The saviour web app creator, easy peasy web app creation by few lines of codes.
|
| 8 |
# No HTML, CSS, or JS needed!
|
| 9 |
+
# But I have used a bit to design but avoid it as its not necessary
|
| 10 |
|
| 11 |
|
| 12 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
| 27 |
# Mostly it avoids our stop words like it, is, are etc
|
| 28 |
# More frequency = more importance → bigger word in the cloud.
|
| 29 |
|
| 30 |
+
|
| 31 |
import matplotlib.pyplot as plt
|
| 32 |
# This guy helps us to plot. So wait till you see it.
|
| 33 |
# We’ll use it to show our Word Cloud in style.
|
|
|
|
| 49 |
model_name = "google/gemma-2b" # using gemma-2b for prototype for my GSOC Proposal. Wish me luck.
|
| 50 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 51 |
# Responsible for automatically downloading and loading the tokenizer configuration and vocabulary associated with the specified pre-trained model.
|
| 52 |
+
# Downloads and loads the tokenizer config and vocab for the given model
|
| 53 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 54 |
# As we discussed, this class is designed for loading pre-trained language models for causal (next-token prediction) tasks.
|
| 55 |
# Loads the actual model used for causal (next-word) prediction tasks
|
|
|
|
| 60 |
def generate_text(prompt, tone, max_length, temperature=0.7, top_p=0.9, repetition_penalty=1.0):
|
| 61 |
tokenizer, model = load_model_and_tokenizer()
|
| 62 |
# Adjust prompt based on tone
|
| 63 |
+
# And Bro believe me, Impact: Better prompts = better outputs = a stronger GSoC impression!
|
| 64 |
tone_prompts = {
|
| 65 |
+
"Funny": f"Generate a concise, humorous response to: {prompt}. Use witty wordplay, unexpected twists, or lighthearted exaggeration, avoiding offensive content. Aim for a punchline-style finish, e.g., if the prompt is 'The dog barked,' respond like 'The dog barked so loud, it woke the moon—and now it is howling back!'",
|
| 66 |
+
"Serious": f"Provide a detailed, thoughtful, and professional response to: {prompt}. Offer logical reasoning, depth, and a formal tone, as if explaining to an expert audience. For example, if the prompt is 'The future of AI,' discuss ethical implications and technical challenges in 2-3 sentences.",
|
| 67 |
+
"Poetic": f"Write a vivid, poetic response to: {prompt}. Use metaphor, rhythm, and imagery to create a lyrical flow, as if crafting a short verse. For instance, if the prompt is 'The river flows,' respond with 'The river flows, a silver thread, / Through emerald dreams where silence tread.'"
|
| 68 |
}
|
| 69 |
input_text = tone_prompts.get(tone, prompt)
|
| 70 |
|
|
|
|
| 80 |
)
|
| 81 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 82 |
|
| 83 |
+
# Clean and Solid UI for our Project, keeping the blue theme of gemini.
|
| 84 |
+
# We will continue tutorial after this st.markdown()
|
| 85 |
st.markdown("""
|
| 86 |
<style>
|
| 87 |
+
/* Solid background with minimal decoration */
|
| 88 |
.stApp {
|
| 89 |
+
background-color: #1E1E3A;
|
| 90 |
+
color: #f0f0f0;
|
|
|
|
|
|
|
| 91 |
}
|
| 92 |
+
|
| 93 |
+
/* Clean container styling for all elements */
|
| 94 |
+
.stForm, div.stButton, .stMarkdown, div.stSlider {
|
| 95 |
+
border-radius: 8px;
|
| 96 |
+
transition: all 0.2s ease;
|
|
|
|
|
|
|
|
|
|
| 97 |
}
|
| 98 |
+
|
| 99 |
+
/* Professional title styling */
|
| 100 |
+
.title {
|
| 101 |
+
color: #ffffff;
|
| 102 |
+
font-size: 32px;
|
| 103 |
+
font-weight: 700;
|
| 104 |
+
padding: 15px 0;
|
| 105 |
+
font-family: 'Segoe UI', Arial, sans-serif;
|
| 106 |
+
letter-spacing: 0.5px;
|
| 107 |
+
margin-bottom: 15px;
|
| 108 |
+
border-bottom: 2px solid #4A5BEA;
|
| 109 |
+
display: inline-block;
|
| 110 |
}
|
| 111 |
+
|
| 112 |
+
/* Clean instructions card */
|
| 113 |
.instructions {
|
| 114 |
+
background: rgba(255, 255, 255, 0.05);
|
| 115 |
+
padding: 20px;
|
| 116 |
+
border-radius: 8px;
|
| 117 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 118 |
+
font-size: 16px;
|
| 119 |
+
color: #f0f0f0;
|
| 120 |
+
border: 1px solid rgba(255, 255, 255, 0.1);
|
| 121 |
+
margin-bottom: 20px;
|
| 122 |
+
line-height: 1.5;
|
| 123 |
}
|
| 124 |
+
|
| 125 |
+
/* Professional output box */
|
| 126 |
.output-box {
|
| 127 |
+
background: rgba(20, 20, 40, 0.8);
|
| 128 |
+
padding: 20px;
|
| 129 |
+
border-radius: 8px;
|
| 130 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
|
| 131 |
font-family: 'Courier New', monospace;
|
| 132 |
font-size: 16px;
|
| 133 |
color: #ffffff;
|
| 134 |
white-space: pre-wrap;
|
| 135 |
+
border: 1px solid #4A5BEA;
|
| 136 |
+
margin-top: 1.5rem;
|
| 137 |
}
|
| 138 |
+
|
| 139 |
+
/* Form styling */
|
| 140 |
+
.stTextInput>div>div>input {
|
| 141 |
+
background: rgba(255, 255, 255, 0.05);
|
| 142 |
+
border: 1px solid rgba(255, 255, 255, 0.2);
|
| 143 |
+
color: white;
|
| 144 |
+
border-radius: 6px;
|
| 145 |
+
padding: 10px;
|
| 146 |
+
font-size: 16px;
|
| 147 |
}
|
| 148 |
+
.stTextInput>div>div>input:focus {
|
| 149 |
+
border-color: #4A5BEA;
|
| 150 |
+
box-shadow: 0 0 5px rgba(74, 91, 234, 0.5);
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
/* Clean, professional buttons */
|
| 154 |
.stButton>button {
|
| 155 |
+
background-color: #4A5BEA;
|
| 156 |
color: white;
|
| 157 |
+
border: none;
|
| 158 |
+
border-radius: 6px;
|
| 159 |
+
padding: 10px 15px;
|
| 160 |
+
font-size: 16px;
|
| 161 |
+
font-weight: 500;
|
| 162 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
|
| 163 |
+
transition: all 0.2s;
|
| 164 |
+
text-transform: none;
|
| 165 |
+
letter-spacing: 0.5px;
|
| 166 |
}
|
| 167 |
.stButton>button:hover {
|
| 168 |
+
background-color: #3A4AC0;
|
| 169 |
transform: translateY(-2px);
|
| 170 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
/* Example button styling */
|
| 174 |
+
.example-button {
|
| 175 |
+
background-color: #2D3250;
|
| 176 |
+
color: white;
|
| 177 |
+
border: 1px solid #4A5BEA;
|
| 178 |
+
border-radius: 6px;
|
| 179 |
+
padding: 8px 16px;
|
| 180 |
+
margin: 5px;
|
| 181 |
+
cursor: pointer;
|
| 182 |
+
transition: all 0.2s;
|
| 183 |
+
font-size: 14px;
|
| 184 |
+
font-weight: 500;
|
| 185 |
+
}
|
| 186 |
+
.example-button:hover {
|
| 187 |
+
background-color: #4A5BEA;
|
| 188 |
}
|
| 189 |
+
|
| 190 |
+
/* Enhanced sliders */
|
| 191 |
.stSlider>div>div>div {
|
| 192 |
+
background: #4A5BEA !important;
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
/* Improved slider number values styling */
|
| 196 |
+
.stSlider p {
|
| 197 |
+
color: #f0f0f0 !important;
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
/* Fix for the blue box numbers on sliders */
|
| 201 |
+
.stSlider > div > div > div > div > div > div {
|
| 202 |
+
background-color: #4A5BEA !important;
|
| 203 |
+
color: white !important;
|
| 204 |
+
font-weight: 500 !important;
|
| 205 |
+
border: none !important;
|
| 206 |
+
border-radius: 4px !important;
|
| 207 |
+
padding: 2px 6px !important;
|
| 208 |
+
font-size: 14px !important;
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
/* Number value containers at the ends of sliders */
|
| 212 |
+
.stSlider > div > div > div:first-child,
|
| 213 |
+
.stSlider > div > div > div:last-child {
|
| 214 |
+
background-color: #292952 !important;
|
| 215 |
+
color: #f0f0f0 !important;
|
| 216 |
+
border: 1px solid rgba(255, 255, 255, 0.1) !important;
|
| 217 |
+
border-radius: 4px !important;
|
| 218 |
+
padding: 4px 8px !important;
|
| 219 |
+
font-size: 14px !important;
|
| 220 |
+
font-family: monospace !important;
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
/* Custom selectbox styling */
|
| 224 |
+
.stSelectbox>div>div {
|
| 225 |
+
background: rgba(255, 255, 255, 0.05);
|
| 226 |
+
border: 1px solid rgba(255, 255, 255, 0.2);
|
| 227 |
+
border-radius: 6px;
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
/* Expander styling */
|
| 231 |
+
.streamlit-expanderHeader {
|
| 232 |
+
background: rgba(255, 255, 255, 0.05);
|
| 233 |
+
border-radius: 6px;
|
| 234 |
+
border: 1px solid rgba(255, 255, 255, 0.1);
|
| 235 |
+
color: #f0f0f0 !important;
|
| 236 |
+
font-weight: 500;
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
/* WordCloud container */
|
| 240 |
+
.wordcloud-container {
|
| 241 |
+
background: rgba(255, 255, 255, 0.05);
|
| 242 |
+
border-radius: 8px;
|
| 243 |
+
padding: 15px;
|
| 244 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 245 |
+
margin-top: 20px;
|
| 246 |
+
border: 1px solid rgba(255, 255, 255, 0.1);
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
/* Footer styling */
|
| 250 |
+
.footer {
|
| 251 |
+
background: rgba(0, 0, 0, 0.2);
|
| 252 |
+
border-radius: 8px;
|
| 253 |
+
padding: 15px;
|
| 254 |
+
margin-top: 30px;
|
| 255 |
+
border-top: 1px solid rgba(255, 255, 255, 0.1);
|
| 256 |
+
text-align: center;
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
/* Parameter card styling */
|
| 260 |
+
.parameter-card {
|
| 261 |
+
background: rgba(255, 255, 255, 0.05);
|
| 262 |
+
border-radius: 8px;
|
| 263 |
+
padding: 15px;
|
| 264 |
+
margin-bottom: 15px;
|
| 265 |
+
border: 1px solid rgba(255, 255, 255, 0.1);
|
| 266 |
+
}
|
| 267 |
+
.parameter-card h4 {
|
| 268 |
+
color: #4A5BEA;
|
| 269 |
+
margin-top: 0;
|
| 270 |
+
font-size: 16px;
|
| 271 |
+
font-weight: 500;
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
/* Scrollbar styling */
|
| 275 |
+
::-webkit-scrollbar {
|
| 276 |
+
width: 8px;
|
| 277 |
+
height: 8px;
|
| 278 |
+
}
|
| 279 |
+
::-webkit-scrollbar-track {
|
| 280 |
+
background: rgba(255, 255, 255, 0.05);
|
| 281 |
+
border-radius: 4px;
|
| 282 |
+
}
|
| 283 |
+
::-webkit-scrollbar-thumb {
|
| 284 |
+
background: #4A5BEA;
|
| 285 |
+
border-radius: 4px;
|
| 286 |
+
}
|
| 287 |
+
::-webkit-scrollbar-thumb:hover {
|
| 288 |
+
background: #3A4AC0;
|
| 289 |
}
|
| 290 |
</style>
|
| 291 |
""", unsafe_allow_html=True)
|
| 292 |
|
| 293 |
+
# Its our Header with GSoC logo, keeping simple as deadline is coming. :)
|
| 294 |
+
st.markdown("""
|
| 295 |
+
<div style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 25px;">
|
| 296 |
+
<h1 class="title">Gemma Text Generator</h1>
|
| 297 |
+
<img src="https://huggingface.co/spaces/Kakaarot/Gemma-HuggingFace_QA_Demo/resolve/main/images/GSoC_logo.png" width="100" alt="GSoC 2025" style="margin-top: 5px;">
|
| 298 |
+
</div>
|
| 299 |
+
""", unsafe_allow_html=True)
|
| 300 |
|
| 301 |
|
| 302 |
+
# This part is generally telling how things work
|
| 303 |
st.markdown("""
|
| 304 |
+
<div class="instructions">
|
| 305 |
+
Enter a prompt below to generate text using the Gemma model from DeepMind. Customize the tone and length to see different outputs!<br><br>
|
| 306 |
+
<b>Example:</b> Prompt: "The cat sat on" | Tone: "Funny" | Length: 50 → "The cat sat on my homework and laughed as I cried over my grades."
|
| 307 |
+
</div>
|
| 308 |
""", unsafe_allow_html=True)
|
| 309 |
|
| 310 |
|
| 311 |
+
# While this tells specifically step by step
|
| 312 |
+
with st.expander("How does this work?"):
|
| 313 |
st.markdown("""
|
| 314 |
+
<div style="padding: 10px;">
|
| 315 |
+
<ul>
|
| 316 |
+
<li style="margin-bottom: 8px;">This app uses <b>Gemma-2B</b>, a language model from Google DeepMind.</li>
|
| 317 |
+
<li style="margin-bottom: 8px;">You give it a prompt, and it predicts the next words one-by-one (aka causal language modeling).</li>
|
| 318 |
+
<li style="margin-bottom: 8px;">The <b>tone</b> you choose adds flavor to the prompt before it hits the model.</li>
|
| 319 |
+
<li style="margin-bottom: 8px;">Parameters like <b>temperature</b> control how wild or safe the answers are.</li>
|
| 320 |
+
<li>The output is visualized in a <b>Word Cloud</b> so you can see which words stand out!</li>
|
| 321 |
+
</ul>
|
| 322 |
+
</div>
|
| 323 |
+
""", unsafe_allow_html=True)
|
| 324 |
+
|
| 325 |
+
# Clean example buttons area
|
| 326 |
+
st.markdown("<p style='margin-top: 20px; margin-bottom: 10px;'>Try these examples:</p>", unsafe_allow_html=True)
|
| 327 |
+
# Below we defined one click example, where one click will fill the form generate button will be pressed automatically,
|
| 328 |
+
# Fixed example buttons layout
|
| 329 |
+
col1, col2, col3 = st.columns(3)
|
| 330 |
+
|
| 331 |
+
# Hey! This is for giving user a taste of what would happen, so that he can try out the process on one click
|
| 332 |
if "trigger_example" not in st.session_state:
|
| 333 |
st.session_state.trigger_example = False
|
| 334 |
|
|
|
|
| 335 |
with col1:
|
| 336 |
+
if st.button("✨ Funny Cat Story"):
|
| 337 |
st.session_state.prompt = "The cat hacked my WiFi"
|
| 338 |
st.session_state.tone = "Funny"
|
| 339 |
st.session_state.trigger_example = True
|
| 340 |
with col2:
|
| 341 |
+
if st.button("🌅 Poetic Goodbye"):
|
| 342 |
st.session_state.prompt = "As the sun set on our final day"
|
| 343 |
st.session_state.tone = "Poetic"
|
| 344 |
st.session_state.trigger_example = True
|
| 345 |
+
with col3:
|
| 346 |
+
if st.button("🧠 Serious AI Future"):
|
| 347 |
+
st.session_state.prompt = "The future of AI is"
|
| 348 |
+
st.session_state.tone = "Serious"
|
| 349 |
+
st.session_state.trigger_example = True
|
| 350 |
|
| 351 |
+
# Clean form with better spacing
|
|
|
|
|
|
|
|
|
|
| 352 |
with st.form(key="input_form"):
|
| 353 |
+
st.markdown('<div style="margin-bottom: 15px;"><h3 style="color: #4A5BEA; margin-bottom: 10px;">Generate Your Text</h3></div>', unsafe_allow_html=True)
|
| 354 |
+
|
| 355 |
prompt = st.text_input("Enter a prompt", placeholder="e.g., 'The future of AI is'", value=st.session_state.get("prompt", ""))
|
| 356 |
+
|
| 357 |
+
col1, col2 = st.columns(2)
|
| 358 |
+
with col1:
|
| 359 |
+
tone = st.selectbox("Tone", ["Funny", "Serious", "Poetic"], index=["Funny", "Serious", "Poetic"].index(st.session_state.get("tone", "Funny")))
|
| 360 |
+
with col2:
|
| 361 |
+
max_length = st.slider("Word count", 20, 100, 50)
|
| 362 |
+
|
| 363 |
+
st.markdown('<div class="parameter-card"><h4>Advanced Parameters</h4>', unsafe_allow_html=True)
|
| 364 |
+
|
| 365 |
+
# Adding custom number formatting to make slider values look better
|
| 366 |
+
st.markdown("""
|
| 367 |
+
<style>
|
| 368 |
+
/* Custom number styling for slider values */
|
| 369 |
+
.value-display {
|
| 370 |
+
background-color: #292952;
|
| 371 |
+
padding: 4px 8px;
|
| 372 |
+
border-radius: 4px;
|
| 373 |
+
font-family: monospace;
|
| 374 |
+
border: 1px solid rgba(255, 255, 255, 0.1);
|
| 375 |
+
font-size: 14px;
|
| 376 |
+
display: inline-block;
|
| 377 |
+
margin-left: 5px;
|
| 378 |
+
color: #f0f0f0;
|
| 379 |
+
}
|
| 380 |
+
</style>
|
| 381 |
+
""", unsafe_allow_html=True)
|
| 382 |
+
|
| 383 |
+
col1, col2 = st.columns(2)
|
| 384 |
+
with col1:
|
| 385 |
+
temperature = st.slider("Temperature (Creativity)", 0.2, 1.5, 0.7, help="Higher values make output more random")
|
| 386 |
+
with col2:
|
| 387 |
+
top_p = st.slider("Top-p (Nucleus Sampling)", 0.1, 1.0, 0.9, help="Controls diversity")
|
| 388 |
+
|
| 389 |
+
repetition_penalty = st.slider("Repetition Penalty", 1.0, 2.0, 1.0, help="Higher values discourage repetition")
|
| 390 |
+
|
| 391 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 392 |
+
|
| 393 |
submit_button = st.form_submit_button(label="Generate")
|
| 394 |
|
| 395 |
# Generate and display output
|
| 396 |
if submit_button or st.session_state.trigger_example:
|
| 397 |
+
st.session_state.trigger_example = False
|
| 398 |
if not prompt:
|
| 399 |
st.error("Please enter a prompt!")
|
| 400 |
else:
|
| 401 |
with st.spinner("Generating text..."):
|
| 402 |
output = generate_text(prompt, tone, max_length, temperature, top_p, repetition_penalty)
|
| 403 |
+
|
| 404 |
+
# Display metadata about the generation with improved value styling
|
| 405 |
st.markdown(f"""
|
| 406 |
+
<div style="background: rgba(255, 255, 255, 0.05); border-radius: 8px; padding: 12px;
|
| 407 |
+
margin-bottom: 15px; font-size: 14px; border: 1px solid rgba(255, 255, 255, 0.1);">
|
| 408 |
+
<span style="color: #4A5BEA; font-weight: 500;">Tone:</span>
|
| 409 |
+
<span class="value-display">{tone}</span> |
|
| 410 |
+
<span style="color: #4A5BEA; font-weight: 500;">Temperature:</span>
|
| 411 |
+
<span class="value-display">{temperature:.2f}</span> |
|
| 412 |
+
<span style="color: #4A5BEA; font-weight: 500;">Words:</span>
|
| 413 |
+
<span class="value-display">~{max_length}</span>
|
| 414 |
+
</div>
|
| 415 |
+
""", unsafe_allow_html=True)
|
| 416 |
+
|
| 417 |
+
st.markdown(f'<div class="output-box">{output}</div>', unsafe_allow_html=True)
|
| 418 |
+
|
| 419 |
+
# WordCloud visualization
|
| 420 |
+
st.markdown('<div class="wordcloud-container">', unsafe_allow_html=True)
|
| 421 |
+
st.markdown('<h4 style="color: #4A5BEA; margin-top: 0;">Word Cloud Visualization</h4>', unsafe_allow_html=True)
|
|
|
|
| 422 |
|
| 423 |
+
# Generate a clean wordcloud
|
| 424 |
+
wordcloud = WordCloud(
|
| 425 |
+
width=600,
|
| 426 |
+
height=300,
|
| 427 |
+
background_color="#1E1E3A",
|
| 428 |
+
colormap="viridis",
|
| 429 |
+
max_words=100,
|
| 430 |
+
contour_width=0
|
| 431 |
+
).generate(output)
|
| 432 |
+
|
| 433 |
+
plt.figure(figsize=(10, 5))
|
| 434 |
plt.imshow(wordcloud, interpolation="bilinear")
|
| 435 |
plt.axis("off")
|
| 436 |
st.pyplot(plt)
|
| 437 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 438 |
|
| 439 |
+
# Clean footer
|
| 440 |
+
st.markdown("""
|
| 441 |
+
<div class="footer">
|
| 442 |
+
<p>Built with ❤️ by Utkarsh Shukla for GSoC Proposal 2025 | Powered by Gemma + Hugging Face</p>
|
| 443 |
+
<p style="margin-top: 10px;">
|
| 444 |
+
<img src="https://huggingface.co/spaces/Kakaarot/Gemma-HuggingFace_QA_Demo/resolve/main/images/google-gemini-icon.png" width="100" alt="Gemma by DeepMind">
|
| 445 |
+
</p>
|
| 446 |
+
<p style="margin-top: 10px;">Wish me luck! 🤞</p>
|
| 447 |
+
</div>
|
| 448 |
+
""", unsafe_allow_html=True)
|