File size: 4,620 Bytes
4589ea9
14ffe85
4589ea9
 
 
 
 
 
 
 
 
 
 
 
e1d60d0
4589ea9
 
 
e1d60d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4589ea9
e1d60d0
 
 
14ffe85
e1d60d0
 
 
 
 
 
 
 
 
4589ea9
e1d60d0
4589ea9
e1d60d0
 
 
14ffe85
e1d60d0
 
 
4589ea9
e1d60d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4589ea9
e1d60d0
4589ea9
 
 
 
e1d60d0
4589ea9
 
e1d60d0
4589ea9
 
e1d60d0
4589ea9
14ffe85
 
 
 
 
 
e1d60d0
14ffe85
 
 
4589ea9
 
e1d60d0
4589ea9
14ffe85
4589ea9
 
e1d60d0
4589ea9
 
 
 
14ffe85
e1d60d0
14ffe85
 
 
e1d60d0
 
 
 
 
14ffe85
e1d60d0
 
 
 
 
14ffe85
4589ea9
e1d60d0
4589ea9
 
 
 
14ffe85
4589ea9
 
e1d60d0
 
 
 
 
 
14ffe85
e1d60d0
 
14ffe85
 
 
 
 
 
 
 
4589ea9
14ffe85
 
 
4589ea9
e1d60d0
 
 
 
 
 
 
 
 
 
 
 
 
 
14ffe85
 
 
 
 
 
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import streamlit as st
import os
from groq import Groq

# -----------------------
# PAGE CONFIG
# -----------------------
st.set_page_config(
    page_title="Krish AI Chatbot πŸ€–",
    page_icon="πŸ€–",
    layout="centered"
)

# -----------------------
# MOBILE RESPONSIVE UI πŸ”₯
# -----------------------
st.markdown("""
<style>

/* Main background */
.stApp {
    background-color: #0E1117;
}

/* Container padding (mobile fix) */
.block-container {
    padding-top: 1rem;
    padding-left: 1rem;
    padding-right: 1rem;
    max-width: 100%;
}

/* Title */
.title {
    text-align: center;
    font-size: 28px;
    font-weight: bold;
    color: white;
}

/* Subtitle */
.subtitle {
    text-align: center;
    color: #AAAAAA;
    font-size: 14px;
    margin-bottom: 15px;
}

/* Chat container */
.chat-container {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

/* User bubble */
.chat-row-user {
    display: flex;
    justify-content: flex-end;
}

.chat-bubble-user {
    background-color: #4CAF50;
    color: white;
    padding: 10px 14px;
    border-radius: 15px;
    font-size: 14px;
    max-width: 85%;
    word-wrap: break-word;
}

/* Bot bubble */
.chat-row-bot {
    display: flex;
    justify-content: flex-start;
}

.chat-bubble-bot {
    background-color: #2A2F3A;
    color: white;
    padding: 10px 14px;
    border-radius: 15px;
    font-size: 14px;
    max-width: 85%;
    word-wrap: break-word;
}

/* Input box fix */
textarea {
    color: white !important;
}

/* -----------------------
   MOBILE OPTIMIZATION πŸ“±
----------------------- */
@media (max-width: 600px) {

    .title {
        font-size: 22px;
    }

    .chat-bubble-user,
    .chat-bubble-bot {
        font-size: 13px;
        padding: 8px 12px;
        max-width: 90%;
    }

    .block-container {
        padding-left: 0.5rem;
        padding-right: 0.5rem;
    }
}

</style>
""", unsafe_allow_html=True)

# -----------------------
# HEADER
# -----------------------
st.markdown('<div class="title">πŸ’¬ Krish AI Chatbot</div>', unsafe_allow_html=True)
st.markdown('<div class="subtitle">Smart, Fast & Beautiful AI Chat πŸš€</div>', unsafe_allow_html=True)

# -----------------------
# API KEY (Secrets + fallback)
# -----------------------
api_key = os.getenv("GROQ_API_KEY")

if not api_key:
    api_key = st.sidebar.text_input("Enter Groq API Key πŸ”‘", type="password")

if not api_key:
    st.warning("Please enter your Groq API key")
    st.stop()

client = Groq(api_key=api_key)

# -----------------------
# MODEL
# -----------------------
MODEL = "llama-3.1-8b-instant"

# -----------------------
# SESSION STATE
# -----------------------
if "messages" not in st.session_state:
    st.session_state.messages = []

# -----------------------
# DISPLAY CHAT
# -----------------------
for msg in st.session_state.messages:
    if msg["role"] == "user":
        st.markdown(f"""
        <div class="chat-row-user">
            <div class="chat-bubble-user">{msg["content"]}</div>
        </div>
        """, unsafe_allow_html=True)
    else:
        st.markdown(f"""
        <div class="chat-row-bot">
            <div class="chat-bubble-bot">{msg["content"]}</div>
        </div>
        """, unsafe_allow_html=True)

# -----------------------
# INPUT
# -----------------------
user_input = st.chat_input("Type your message...")

if user_input:
    # Save user message
    st.session_state.messages.append({"role": "user", "content": user_input})

    # Show instantly
    st.markdown(f"""
    <div class="chat-row-user">
        <div class="chat-bubble-user">{user_input}</div>
    </div>
    """, unsafe_allow_html=True)

    # Placeholder for streaming
    placeholder = st.empty()
    full_response = ""

    try:
        stream = client.chat.completions.create(
            model=MODEL,
            messages=st.session_state.messages,
            stream=True
        )

        for chunk in stream:
            content = chunk.choices[0].delta.content or ""
            full_response += content

            placeholder.markdown(f"""
            <div class="chat-row-bot">
                <div class="chat-bubble-bot">{full_response}β–Œ</div>
            </div>
            """, unsafe_allow_html=True)

        # Final message
        placeholder.markdown(f"""
        <div class="chat-row-bot">
            <div class="chat-bubble-bot">{full_response}</div>
        </div>
        """, unsafe_allow_html=True)

        # Save
        st.session_state.messages.append(
            {"role": "assistant", "content": full_response}
        )

    except Exception as e:
        st.error(f"Error: {e}")