Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,20 @@
|
|
1 |
-
import os
|
2 |
import streamlit as st
|
3 |
from groq import Groq
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
# Set page configuration
|
6 |
-
st.set_page_config(page_title="Groq Chatbot", page_icon="
|
7 |
|
8 |
# Title
|
9 |
-
st.title("
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
st.header("π API Key")
|
14 |
-
api_key = st.text_input("Enter your Groq API Key", type="password")
|
15 |
-
st.markdown(
|
16 |
-
"Don't have a key? [Get it here](https://console.groq.com/keys)",
|
17 |
-
unsafe_allow_html=True
|
18 |
-
)
|
19 |
-
|
20 |
-
# Input prompt
|
21 |
-
user_input = st.text_input("Ask me anything:")
|
22 |
|
23 |
# Initialize chat history
|
24 |
if "messages" not in st.session_state:
|
@@ -29,32 +25,25 @@ for msg in st.session_state.messages:
|
|
29 |
with st.chat_message(msg["role"]):
|
30 |
st.markdown(msg["content"])
|
31 |
|
32 |
-
#
|
33 |
-
if user_input
|
34 |
-
# Add user message to
|
35 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
36 |
with st.chat_message("user"):
|
37 |
st.markdown(user_input)
|
38 |
|
39 |
# Call Groq API
|
40 |
try:
|
41 |
-
client = Groq(api_key=api_key)
|
42 |
-
|
43 |
response = client.chat.completions.create(
|
44 |
messages=st.session_state.messages,
|
45 |
-
model="llama3-70b-8192", #
|
46 |
)
|
47 |
-
|
48 |
reply = response.choices[0].message.content
|
49 |
|
50 |
-
# Add assistant reply to
|
51 |
st.session_state.messages.append({"role": "assistant", "content": reply})
|
52 |
with st.chat_message("assistant"):
|
53 |
st.markdown(reply)
|
54 |
|
55 |
except Exception as e:
|
56 |
st.error(f"Error: {e}")
|
57 |
-
|
58 |
-
elif user_input and not api_key:
|
59 |
-
st.warning("Please enter your Groq API Key in the sidebar.")
|
60 |
-
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from groq import Groq
|
3 |
|
4 |
+
# β
Set your Groq API key here (replace with your actual key)
|
5 |
+
GROQ_API_KEY = "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" # π Replace this with your real key
|
6 |
+
|
7 |
+
# Initialize Groq client
|
8 |
+
client = Groq(api_key=GROQ_API_KEY)
|
9 |
+
|
10 |
# Set page configuration
|
11 |
+
st.set_page_config(page_title="Groq Chatbot", page_icon="π€")
|
12 |
|
13 |
# Title
|
14 |
+
st.title("π€ Groq Chatbot using LLaMA 3")
|
15 |
|
16 |
+
# Text input from user
|
17 |
+
user_input = st.text_input("You:", placeholder="Type your question here...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
# Initialize chat history
|
20 |
if "messages" not in st.session_state:
|
|
|
25 |
with st.chat_message(msg["role"]):
|
26 |
st.markdown(msg["content"])
|
27 |
|
28 |
+
# Handle user input
|
29 |
+
if user_input:
|
30 |
+
# Add user message to history
|
31 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
32 |
with st.chat_message("user"):
|
33 |
st.markdown(user_input)
|
34 |
|
35 |
# Call Groq API
|
36 |
try:
|
|
|
|
|
37 |
response = client.chat.completions.create(
|
38 |
messages=st.session_state.messages,
|
39 |
+
model="llama3-70b-8192", # model ID from Groq
|
40 |
)
|
|
|
41 |
reply = response.choices[0].message.content
|
42 |
|
43 |
+
# Add assistant reply to history
|
44 |
st.session_state.messages.append({"role": "assistant", "content": reply})
|
45 |
with st.chat_message("assistant"):
|
46 |
st.markdown(reply)
|
47 |
|
48 |
except Exception as e:
|
49 |
st.error(f"Error: {e}")
|
|
|
|
|
|
|
|