Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,42 +12,117 @@ st.set_page_config(
|
|
| 12 |
)
|
| 13 |
|
| 14 |
# -----------------------
|
| 15 |
-
#
|
| 16 |
# -----------------------
|
| 17 |
st.markdown("""
|
| 18 |
<style>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
.chat-bubble-user {
|
| 20 |
-
background-color: #
|
| 21 |
-
|
|
|
|
| 22 |
border-radius: 15px;
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
}
|
|
|
|
| 27 |
.chat-bubble-bot {
|
| 28 |
-
background-color: #
|
| 29 |
-
|
|
|
|
| 30 |
border-radius: 15px;
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
}
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
}
|
|
|
|
| 40 |
</style>
|
| 41 |
""", unsafe_allow_html=True)
|
| 42 |
|
| 43 |
# -----------------------
|
| 44 |
-
#
|
| 45 |
# -----------------------
|
| 46 |
st.markdown('<div class="title">π¬ Krish AI Chatbot</div>', unsafe_allow_html=True)
|
| 47 |
-
st.
|
| 48 |
|
| 49 |
# -----------------------
|
| 50 |
-
# API KEY (
|
| 51 |
# -----------------------
|
| 52 |
api_key = os.getenv("GROQ_API_KEY")
|
| 53 |
|
|
@@ -55,33 +130,41 @@ if not api_key:
|
|
| 55 |
api_key = st.sidebar.text_input("Enter Groq API Key π", type="password")
|
| 56 |
|
| 57 |
if not api_key:
|
| 58 |
-
st.warning("Please enter your Groq API
|
| 59 |
st.stop()
|
| 60 |
|
| 61 |
client = Groq(api_key=api_key)
|
| 62 |
|
| 63 |
# -----------------------
|
| 64 |
-
# MODEL
|
| 65 |
# -----------------------
|
| 66 |
MODEL = "llama-3.1-8b-instant"
|
| 67 |
|
| 68 |
# -----------------------
|
| 69 |
-
# SESSION STATE
|
| 70 |
# -----------------------
|
| 71 |
if "messages" not in st.session_state:
|
| 72 |
st.session_state.messages = []
|
| 73 |
|
| 74 |
# -----------------------
|
| 75 |
-
# DISPLAY CHAT
|
| 76 |
# -----------------------
|
| 77 |
for msg in st.session_state.messages:
|
| 78 |
if msg["role"] == "user":
|
| 79 |
-
st.markdown(f
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
else:
|
| 81 |
-
st.markdown(f
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
# -----------------------
|
| 84 |
-
#
|
| 85 |
# -----------------------
|
| 86 |
user_input = st.chat_input("Type your message...")
|
| 87 |
|
|
@@ -89,16 +172,17 @@ if user_input:
|
|
| 89 |
# Save user message
|
| 90 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 91 |
|
| 92 |
-
#
|
| 93 |
-
st.markdown(f
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
# Placeholder for
|
| 96 |
-
|
| 97 |
full_response = ""
|
| 98 |
|
| 99 |
-
# -----------------------
|
| 100 |
-
# STREAMING RESPONSE π₯
|
| 101 |
-
# -----------------------
|
| 102 |
try:
|
| 103 |
stream = client.chat.completions.create(
|
| 104 |
model=MODEL,
|
|
@@ -109,18 +193,21 @@ if user_input:
|
|
| 109 |
for chunk in stream:
|
| 110 |
content = chunk.choices[0].delta.content or ""
|
| 111 |
full_response += content
|
| 112 |
-
response_placeholder.markdown(
|
| 113 |
-
f'<div class="chat-bubble-bot">{full_response}β</div>',
|
| 114 |
-
unsafe_allow_html=True
|
| 115 |
-
)
|
| 116 |
-
|
| 117 |
-
# Final response (remove cursor)
|
| 118 |
-
response_placeholder.markdown(
|
| 119 |
-
f'<div class="chat-bubble-bot">{full_response}</div>',
|
| 120 |
-
unsafe_allow_html=True
|
| 121 |
-
)
|
| 122 |
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
st.session_state.messages.append(
|
| 125 |
{"role": "assistant", "content": full_response}
|
| 126 |
)
|
|
|
|
| 12 |
)
|
| 13 |
|
| 14 |
# -----------------------
|
| 15 |
+
# MOBILE RESPONSIVE UI π₯
|
| 16 |
# -----------------------
|
| 17 |
st.markdown("""
|
| 18 |
<style>
|
| 19 |
+
|
| 20 |
+
/* Main background */
|
| 21 |
+
.stApp {
|
| 22 |
+
background-color: #0E1117;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
/* Container padding (mobile fix) */
|
| 26 |
+
.block-container {
|
| 27 |
+
padding-top: 1rem;
|
| 28 |
+
padding-left: 1rem;
|
| 29 |
+
padding-right: 1rem;
|
| 30 |
+
max-width: 100%;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
/* Title */
|
| 34 |
+
.title {
|
| 35 |
+
text-align: center;
|
| 36 |
+
font-size: 28px;
|
| 37 |
+
font-weight: bold;
|
| 38 |
+
color: white;
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
/* Subtitle */
|
| 42 |
+
.subtitle {
|
| 43 |
+
text-align: center;
|
| 44 |
+
color: #AAAAAA;
|
| 45 |
+
font-size: 14px;
|
| 46 |
+
margin-bottom: 15px;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
/* Chat container */
|
| 50 |
+
.chat-container {
|
| 51 |
+
display: flex;
|
| 52 |
+
flex-direction: column;
|
| 53 |
+
gap: 10px;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
/* User bubble */
|
| 57 |
+
.chat-row-user {
|
| 58 |
+
display: flex;
|
| 59 |
+
justify-content: flex-end;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
.chat-bubble-user {
|
| 63 |
+
background-color: #4CAF50;
|
| 64 |
+
color: white;
|
| 65 |
+
padding: 10px 14px;
|
| 66 |
border-radius: 15px;
|
| 67 |
+
font-size: 14px;
|
| 68 |
+
max-width: 85%;
|
| 69 |
+
word-wrap: break-word;
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
/* Bot bubble */
|
| 73 |
+
.chat-row-bot {
|
| 74 |
+
display: flex;
|
| 75 |
+
justify-content: flex-start;
|
| 76 |
}
|
| 77 |
+
|
| 78 |
.chat-bubble-bot {
|
| 79 |
+
background-color: #2A2F3A;
|
| 80 |
+
color: white;
|
| 81 |
+
padding: 10px 14px;
|
| 82 |
border-radius: 15px;
|
| 83 |
+
font-size: 14px;
|
| 84 |
+
max-width: 85%;
|
| 85 |
+
word-wrap: break-word;
|
| 86 |
}
|
| 87 |
+
|
| 88 |
+
/* Input box fix */
|
| 89 |
+
textarea {
|
| 90 |
+
color: white !important;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
/* -----------------------
|
| 94 |
+
MOBILE OPTIMIZATION π±
|
| 95 |
+
----------------------- */
|
| 96 |
+
@media (max-width: 600px) {
|
| 97 |
+
|
| 98 |
+
.title {
|
| 99 |
+
font-size: 22px;
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
.chat-bubble-user,
|
| 103 |
+
.chat-bubble-bot {
|
| 104 |
+
font-size: 13px;
|
| 105 |
+
padding: 8px 12px;
|
| 106 |
+
max-width: 90%;
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
.block-container {
|
| 110 |
+
padding-left: 0.5rem;
|
| 111 |
+
padding-right: 0.5rem;
|
| 112 |
+
}
|
| 113 |
}
|
| 114 |
+
|
| 115 |
</style>
|
| 116 |
""", unsafe_allow_html=True)
|
| 117 |
|
| 118 |
# -----------------------
|
| 119 |
+
# HEADER
|
| 120 |
# -----------------------
|
| 121 |
st.markdown('<div class="title">π¬ Krish AI Chatbot</div>', unsafe_allow_html=True)
|
| 122 |
+
st.markdown('<div class="subtitle">Smart, Fast & Beautiful AI Chat π</div>', unsafe_allow_html=True)
|
| 123 |
|
| 124 |
# -----------------------
|
| 125 |
+
# API KEY (Secrets + fallback)
|
| 126 |
# -----------------------
|
| 127 |
api_key = os.getenv("GROQ_API_KEY")
|
| 128 |
|
|
|
|
| 130 |
api_key = st.sidebar.text_input("Enter Groq API Key π", type="password")
|
| 131 |
|
| 132 |
if not api_key:
|
| 133 |
+
st.warning("Please enter your Groq API key")
|
| 134 |
st.stop()
|
| 135 |
|
| 136 |
client = Groq(api_key=api_key)
|
| 137 |
|
| 138 |
# -----------------------
|
| 139 |
+
# MODEL
|
| 140 |
# -----------------------
|
| 141 |
MODEL = "llama-3.1-8b-instant"
|
| 142 |
|
| 143 |
# -----------------------
|
| 144 |
+
# SESSION STATE
|
| 145 |
# -----------------------
|
| 146 |
if "messages" not in st.session_state:
|
| 147 |
st.session_state.messages = []
|
| 148 |
|
| 149 |
# -----------------------
|
| 150 |
+
# DISPLAY CHAT
|
| 151 |
# -----------------------
|
| 152 |
for msg in st.session_state.messages:
|
| 153 |
if msg["role"] == "user":
|
| 154 |
+
st.markdown(f"""
|
| 155 |
+
<div class="chat-row-user">
|
| 156 |
+
<div class="chat-bubble-user">{msg["content"]}</div>
|
| 157 |
+
</div>
|
| 158 |
+
""", unsafe_allow_html=True)
|
| 159 |
else:
|
| 160 |
+
st.markdown(f"""
|
| 161 |
+
<div class="chat-row-bot">
|
| 162 |
+
<div class="chat-bubble-bot">{msg["content"]}</div>
|
| 163 |
+
</div>
|
| 164 |
+
""", unsafe_allow_html=True)
|
| 165 |
|
| 166 |
# -----------------------
|
| 167 |
+
# INPUT
|
| 168 |
# -----------------------
|
| 169 |
user_input = st.chat_input("Type your message...")
|
| 170 |
|
|
|
|
| 172 |
# Save user message
|
| 173 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 174 |
|
| 175 |
+
# Show instantly
|
| 176 |
+
st.markdown(f"""
|
| 177 |
+
<div class="chat-row-user">
|
| 178 |
+
<div class="chat-bubble-user">{user_input}</div>
|
| 179 |
+
</div>
|
| 180 |
+
""", unsafe_allow_html=True)
|
| 181 |
|
| 182 |
+
# Placeholder for streaming
|
| 183 |
+
placeholder = st.empty()
|
| 184 |
full_response = ""
|
| 185 |
|
|
|
|
|
|
|
|
|
|
| 186 |
try:
|
| 187 |
stream = client.chat.completions.create(
|
| 188 |
model=MODEL,
|
|
|
|
| 193 |
for chunk in stream:
|
| 194 |
content = chunk.choices[0].delta.content or ""
|
| 195 |
full_response += content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
+
placeholder.markdown(f"""
|
| 198 |
+
<div class="chat-row-bot">
|
| 199 |
+
<div class="chat-bubble-bot">{full_response}β</div>
|
| 200 |
+
</div>
|
| 201 |
+
""", unsafe_allow_html=True)
|
| 202 |
+
|
| 203 |
+
# Final message
|
| 204 |
+
placeholder.markdown(f"""
|
| 205 |
+
<div class="chat-row-bot">
|
| 206 |
+
<div class="chat-bubble-bot">{full_response}</div>
|
| 207 |
+
</div>
|
| 208 |
+
""", unsafe_allow_html=True)
|
| 209 |
+
|
| 210 |
+
# Save
|
| 211 |
st.session_state.messages.append(
|
| 212 |
{"role": "assistant", "content": full_response}
|
| 213 |
)
|