Not-Grim-Refer
commited on
Commit
β’
3615dcf
1
Parent(s):
cbc00d2
init
Browse files- README.md +13 -13
- app.py +128 -0
- requirements.txt +3 -0
README.md
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
-
---
|
2 |
-
title: Groq
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo: blue
|
6 |
-
sdk: streamlit
|
7 |
-
sdk_version: 1.33.0
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
license: mit
|
11 |
-
---
|
12 |
-
|
13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
+
---
|
2 |
+
title: Groq-LLaMA3.1
|
3 |
+
emoji: π
|
4 |
+
colorFrom: yellow
|
5 |
+
colorTo: blue
|
6 |
+
sdk: streamlit
|
7 |
+
sdk_version: 1.33.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: mit
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import find_dotenv, load_dotenv
|
3 |
+
import streamlit as st
|
4 |
+
from typing import Generator
|
5 |
+
from groq import Groq
|
6 |
+
|
7 |
+
_ = load_dotenv(find_dotenv())
|
8 |
+
st.set_page_config(page_icon="π", layout="wide", page_title="Groq & LLaMA3.1 Chat Bot...")
|
9 |
+
|
10 |
+
|
11 |
+
def icon(emoji: str):
|
12 |
+
"""Shows an emoji as a Notion-style page icon."""
|
13 |
+
st.write(
|
14 |
+
f'<span style="font-size: 78px; line-height: 1">{emoji}</span>',
|
15 |
+
unsafe_allow_html=True,
|
16 |
+
)
|
17 |
+
|
18 |
+
|
19 |
+
# icon("β‘οΈ")
|
20 |
+
|
21 |
+
st.subheader("Groq Chat with LLaMA3.1 App", divider="rainbow", anchor=False)
|
22 |
+
|
23 |
+
client = Groq(
|
24 |
+
api_key=os.environ['GROQ_API_KEY'],
|
25 |
+
)
|
26 |
+
|
27 |
+
# Initialize chat history and selected model
|
28 |
+
if "messages" not in st.session_state:
|
29 |
+
st.session_state.messages = []
|
30 |
+
|
31 |
+
if "selected_model" not in st.session_state:
|
32 |
+
st.session_state.selected_model = None
|
33 |
+
|
34 |
+
# Define model details
|
35 |
+
models = {
|
36 |
+
"llama-3.1-70b-versatile": {"name": "LLaMA3.1-70b", "tokens": 4096, "developer": "Meta"},
|
37 |
+
"llama-3.1-8b-instant": {"name": "LLaMA3.1-8b", "tokens": 4096, "developer": "Meta"},
|
38 |
+
"llama3-70b-8192": {"name": "Meta Llama 3 70B", "tokens": 4096, "developer": "Meta"},
|
39 |
+
"llama3-8b-8192": {"name": "Meta Llama 3 8B", "tokens": 4096, "developer": "Meta"},
|
40 |
+
"llama3-groq-70b-8192-tool-use-preview": {"name": "Llama 3 Groq 70B Tool Use (Preview)", "tokens": 4096, "developer": "Groq"},
|
41 |
+
"gemma-7b-it": {"name": "Gemma-7b-it", "tokens": 4096, "developer": "Google"},
|
42 |
+
"mixtral-8x7b-32768": {
|
43 |
+
"name": "Mixtral-8x7b-Instruct-v0.1",
|
44 |
+
"tokens": 32768,
|
45 |
+
"developer": "Mistral",
|
46 |
+
},
|
47 |
+
}
|
48 |
+
|
49 |
+
# Layout for model selection and max_tokens slider
|
50 |
+
col1, col2 = st.columns([1, 3]) # Adjust the ratio to make the first column smaller
|
51 |
+
|
52 |
+
|
53 |
+
with col1:
|
54 |
+
model_option = st.selectbox(
|
55 |
+
"Choose a model:",
|
56 |
+
options=list(models.keys()),
|
57 |
+
format_func=lambda x: models[x]["name"],
|
58 |
+
index=0, # Default to the first model in the list
|
59 |
+
)
|
60 |
+
max_tokens_range = models[model_option]["tokens"]
|
61 |
+
max_tokens = st.slider(
|
62 |
+
"Max Tokens:",
|
63 |
+
min_value=512,
|
64 |
+
max_value=max_tokens_range,
|
65 |
+
value=min(32768, max_tokens_range),
|
66 |
+
step=512,
|
67 |
+
help=f"Adjust the maximum number of tokens (words) for the model's response. Max for selected model: {max_tokens_range}",
|
68 |
+
)
|
69 |
+
|
70 |
+
# Detect model change and clear chat history if model has changed
|
71 |
+
if st.session_state.selected_model != model_option:
|
72 |
+
st.session_state.messages = []
|
73 |
+
st.session_state.selected_model = model_option
|
74 |
+
|
75 |
+
# Add a "Clear Chat" button
|
76 |
+
if st.button("Clear Chat"):
|
77 |
+
st.session_state.messages = []
|
78 |
+
|
79 |
+
# Display chat messages from history on app rerun
|
80 |
+
for message in st.session_state.messages:
|
81 |
+
avatar = "π" if message["role"] == "assistant" else "π§βπ»"
|
82 |
+
with st.chat_message(message["role"], avatar=avatar):
|
83 |
+
st.markdown(message["content"])
|
84 |
+
|
85 |
+
|
86 |
+
def generate_chat_responses(chat_completion) -> Generator[str, None, None]:
|
87 |
+
"""Yield chat response content from the Groq API response."""
|
88 |
+
for chunk in chat_completion:
|
89 |
+
if chunk.choices[0].delta.content:
|
90 |
+
yield chunk.choices[0].delta.content
|
91 |
+
|
92 |
+
|
93 |
+
if prompt := st.chat_input("Enter your prompt here..."):
|
94 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
95 |
+
|
96 |
+
with st.chat_message("user", avatar="π§βπ»"):
|
97 |
+
st.markdown(prompt)
|
98 |
+
|
99 |
+
# Fetch response from Groq API
|
100 |
+
try:
|
101 |
+
chat_completion = client.chat.completions.create(
|
102 |
+
model=model_option,
|
103 |
+
messages=[
|
104 |
+
{"role": m["role"], "content": m["content"]}
|
105 |
+
for m in st.session_state.messages
|
106 |
+
],
|
107 |
+
max_tokens=max_tokens,
|
108 |
+
stream=True,
|
109 |
+
)
|
110 |
+
|
111 |
+
# Use the generator function with st.write_stream
|
112 |
+
with st.chat_message("assistant", avatar="π"):
|
113 |
+
chat_responses_generator = generate_chat_responses(chat_completion)
|
114 |
+
full_response = st.write_stream(chat_responses_generator)
|
115 |
+
except Exception as e:
|
116 |
+
st.error(e, icon="β")
|
117 |
+
|
118 |
+
# Append the full response to session_state.messages
|
119 |
+
if isinstance(full_response, str):
|
120 |
+
st.session_state.messages.append(
|
121 |
+
{"role": "assistant", "content": full_response}
|
122 |
+
)
|
123 |
+
else:
|
124 |
+
# Handle the case where full_response is not a string
|
125 |
+
combined_response = "\n".join(str(item) for item in full_response)
|
126 |
+
st.session_state.messages.append(
|
127 |
+
{"role": "assistant", "content": combined_response}
|
128 |
+
)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
groq
|
3 |
+
python-dotenv
|