Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +78 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from groq import Groq
|
3 |
+
import streamlit as st
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
|
6 |
+
load_dotenv()
|
7 |
+
|
8 |
+
client = Groq(
|
9 |
+
api_key=os.getenv("GROQ_API_KEY"),
|
10 |
+
)
|
11 |
+
|
12 |
+
models = client.models.list()
|
13 |
+
available_models = [x.to_dict()["id"] for x in models.data]
|
14 |
+
|
15 |
+
system_prompt = "You are a part of chatbot application, where multiple chatbots are available for user to select. You are a helpful assistant currently selected by user. Your major task is to help user with his queries"
|
16 |
+
|
17 |
+
if "models" not in st.session_state:
|
18 |
+
st.session_state.models = available_models
|
19 |
+
|
20 |
+
if "messages" not in st.session_state:
|
21 |
+
st.session_state.messages = [
|
22 |
+
{
|
23 |
+
"role": "system",
|
24 |
+
"content": system_prompt,
|
25 |
+
},
|
26 |
+
]
|
27 |
+
|
28 |
+
if "selected_model" not in st.session_state:
|
29 |
+
st.session_state.selected_model = ''
|
30 |
+
|
31 |
+
st.set_page_config(layout="wide")
|
32 |
+
|
33 |
+
selected_model = st.sidebar.selectbox(
|
34 |
+
label="Select Model",
|
35 |
+
options=st.session_state.models,
|
36 |
+
)
|
37 |
+
|
38 |
+
if selected_model != st.session_state.selected_model:
|
39 |
+
st.session_state.selected_model = selected_model
|
40 |
+
|
41 |
+
st.header("Hey! 👋 I am GROQ Bot")
|
42 |
+
|
43 |
+
if selected_model:
|
44 |
+
st.markdown(f"##### You are talking to **```{selected_model}```**")
|
45 |
+
|
46 |
+
if st.sidebar.button("Clear History", icon=':material/delete:', type="primary", help="Bot will forget what ever you talked" ):
|
47 |
+
st.session_state.messages = [
|
48 |
+
{
|
49 |
+
"role": "system",
|
50 |
+
"content": system_prompt,
|
51 |
+
},
|
52 |
+
]
|
53 |
+
|
54 |
+
for message in st.session_state.messages:
|
55 |
+
if message["role"] != "system":
|
56 |
+
with st.chat_message(message["role"]):
|
57 |
+
st.write(message["content"])
|
58 |
+
|
59 |
+
if prompt := st.chat_input("Ask anything to me"):
|
60 |
+
|
61 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
62 |
+
|
63 |
+
with st.chat_message("user"):
|
64 |
+
st.markdown(prompt)
|
65 |
+
|
66 |
+
try:
|
67 |
+
response = client.chat.completions.create(
|
68 |
+
messages=st.session_state.messages,
|
69 |
+
model=selected_model,
|
70 |
+
)
|
71 |
+
response = response.choices[0].message.content
|
72 |
+
except :
|
73 |
+
response = "Model is having some issue it cann't answer now select any other model"
|
74 |
+
|
75 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
76 |
+
|
77 |
+
with st.chat_message("assistant"):
|
78 |
+
st.markdown(response)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
groq
|
3 |
+
python-dotenv
|