Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from groq import Groq
|
| 3 |
+
|
| 4 |
+
# -----------------------
|
| 5 |
+
# PAGE CONFIG
|
| 6 |
+
# -----------------------
|
| 7 |
+
st.set_page_config(
|
| 8 |
+
page_title="Krish AI Chatbot 🤖",
|
| 9 |
+
page_icon="🤖",
|
| 10 |
+
layout="centered"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# -----------------------
|
| 14 |
+
# CUSTOM CSS (Beautiful UI)
|
| 15 |
+
# -----------------------
|
| 16 |
+
st.markdown("""
|
| 17 |
+
<style>
|
| 18 |
+
.chat-bubble-user {
|
| 19 |
+
background-color: #DCF8C6;
|
| 20 |
+
padding: 10px 15px;
|
| 21 |
+
border-radius: 12px;
|
| 22 |
+
margin: 5px 0;
|
| 23 |
+
text-align: right;
|
| 24 |
+
}
|
| 25 |
+
.chat-bubble-bot {
|
| 26 |
+
background-color: #F1F0F0;
|
| 27 |
+
padding: 10px 15px;
|
| 28 |
+
border-radius: 12px;
|
| 29 |
+
margin: 5px 0;
|
| 30 |
+
text-align: left;
|
| 31 |
+
}
|
| 32 |
+
.title {
|
| 33 |
+
text-align: center;
|
| 34 |
+
font-size: 32px;
|
| 35 |
+
font-weight: bold;
|
| 36 |
+
}
|
| 37 |
+
</style>
|
| 38 |
+
""", unsafe_allow_html=True)
|
| 39 |
+
|
| 40 |
+
# -----------------------
|
| 41 |
+
# TITLE
|
| 42 |
+
# -----------------------
|
| 43 |
+
st.markdown('<div class="title">💬 Krish AI Chatbot</div>', unsafe_allow_html=True)
|
| 44 |
+
st.write("Ask anything and get beautiful responses ✨")
|
| 45 |
+
|
| 46 |
+
# -----------------------
|
| 47 |
+
# API KEY INPUT
|
| 48 |
+
# -----------------------
|
| 49 |
+
api_key = st.sidebar.text_input("Enter Groq API Key 🔑", type="password")
|
| 50 |
+
|
| 51 |
+
# -----------------------
|
| 52 |
+
# CLIENT INIT
|
| 53 |
+
# -----------------------
|
| 54 |
+
if api_key:
|
| 55 |
+
client = Groq(api_key=api_key)
|
| 56 |
+
else:
|
| 57 |
+
st.warning("Please enter your Groq API Key in sidebar ⚠️")
|
| 58 |
+
st.stop()
|
| 59 |
+
|
| 60 |
+
# -----------------------
|
| 61 |
+
# SESSION STATE
|
| 62 |
+
# -----------------------
|
| 63 |
+
if "messages" not in st.session_state:
|
| 64 |
+
st.session_state.messages = []
|
| 65 |
+
|
| 66 |
+
# -----------------------
|
| 67 |
+
# USER INPUT
|
| 68 |
+
# -----------------------
|
| 69 |
+
user_input = st.chat_input("Type your message...")
|
| 70 |
+
|
| 71 |
+
if user_input:
|
| 72 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 73 |
+
|
| 74 |
+
# -----------------------
|
| 75 |
+
# GROQ API CALL
|
| 76 |
+
# -----------------------
|
| 77 |
+
response = client.chat.completions.create(
|
| 78 |
+
model="llama3-8b-8192",
|
| 79 |
+
messages=st.session_state.messages
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
bot_reply = response.choices[0].message.content
|
| 83 |
+
|
| 84 |
+
st.session_state.messages.append({"role": "assistant", "content": bot_reply})
|
| 85 |
+
|
| 86 |
+
# -----------------------
|
| 87 |
+
# DISPLAY CHAT
|
| 88 |
+
# -----------------------
|
| 89 |
+
for msg in st.session_state.messages:
|
| 90 |
+
if msg["role"] == "user":
|
| 91 |
+
st.markdown(f'<div class="chat-bubble-user">{msg["content"]}</div>', unsafe_allow_html=True)
|
| 92 |
+
else:
|
| 93 |
+
st.markdown(f'<div class="chat-bubble-bot">{msg["content"]}</div>', unsafe_allow_html=True)
|