Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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("π¬ Groq Chatbot with LLaMA 3")
|
10 |
+
|
11 |
+
# Sidebar for API key input
|
12 |
+
with st.sidebar:
|
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:
|
25 |
+
st.session_state.messages = []
|
26 |
+
|
27 |
+
# Display chat history
|
28 |
+
for msg in st.session_state.messages:
|
29 |
+
with st.chat_message(msg["role"]):
|
30 |
+
st.markdown(msg["content"])
|
31 |
+
|
32 |
+
# When user submits input
|
33 |
+
if user_input and api_key:
|
34 |
+
# Add user message to session
|
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", # updated model ID from Groq
|
46 |
+
)
|
47 |
+
|
48 |
+
reply = response.choices[0].message.content
|
49 |
+
|
50 |
+
# Add assistant reply to session
|
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 |
+
|