Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,172 +1,15 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
Search models, datasets, users...
|
4 |
-
Models
|
5 |
-
Datasets
|
6 |
-
Spaces
|
7 |
-
Posts
|
8 |
-
Docs
|
9 |
-
Enterprise
|
10 |
-
Pricing
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
Spaces:
|
15 |
-
|
16 |
-
dromerosm
|
17 |
-
/
|
18 |
-
groq-llama3
|
19 |
-
|
20 |
-
|
21 |
-
like
|
22 |
-
29
|
23 |
-
App
|
24 |
-
Files
|
25 |
-
Community
|
26 |
-
groq-llama3
|
27 |
-
/
|
28 |
-
app.py
|
29 |
-
|
30 |
-
dromerosm's picture
|
31 |
-
dromerosm
|
32 |
-
Update app.py
|
33 |
-
81a6356
|
34 |
-
verified
|
35 |
-
4 months ago
|
36 |
-
raw
|
37 |
-
|
38 |
-
Copy download link
|
39 |
-
history
|
40 |
-
blame
|
41 |
-
contribute
|
42 |
-
delete
|
43 |
-
|
44 |
-
4.5 kB
|
45 |
import os
|
46 |
-
from dotenv import find_dotenv, load_dotenv
|
47 |
-
import streamlit as st
|
48 |
-
from typing import Generator
|
49 |
-
from groq import Groq
|
50 |
-
|
51 |
-
_ = load_dotenv(find_dotenv())
|
52 |
-
st.set_page_config(page_icon="📃", layout="wide", page_title="Groq & LLaMA3.1 Chat Bot...")
|
53 |
-
|
54 |
-
|
55 |
-
def icon(emoji: str):
|
56 |
-
"""Shows an emoji as a Notion-style page icon."""
|
57 |
-
st.write(
|
58 |
-
f'<span style="font-size: 78px; line-height: 1">{emoji}</span>',
|
59 |
-
unsafe_allow_html=True,
|
60 |
-
)
|
61 |
-
|
62 |
-
|
63 |
-
# icon("⚡️")
|
64 |
-
|
65 |
-
st.subheader("Groq Chat with LLaMA3.1 App", divider="rainbow", anchor=False)
|
66 |
-
|
67 |
-
client = Groq(
|
68 |
-
api_key=os.environ['GROQ_API_KEY'],
|
69 |
-
)
|
70 |
-
|
71 |
-
# Initialize chat history and selected model
|
72 |
-
if "messages" not in st.session_state:
|
73 |
-
st.session_state.messages = []
|
74 |
-
|
75 |
-
if "selected_model" not in st.session_state:
|
76 |
-
st.session_state.selected_model = None
|
77 |
-
|
78 |
-
# Define model details
|
79 |
-
models = {
|
80 |
-
"llama-3.1-70b-versatile": {"name": "LLaMA3.1-70b", "tokens": 4096, "developer": "Meta"},
|
81 |
-
"llama-3.1-8b-instant": {"name": "LLaMA3.1-8b", "tokens": 4096, "developer": "Meta"},
|
82 |
-
"llama3-70b-8192": {"name": "Meta Llama 3 70B", "tokens": 4096, "developer": "Meta"},
|
83 |
-
"llama3-8b-8192": {"name": "Meta Llama 3 8B", "tokens": 4096, "developer": "Meta"},
|
84 |
-
"llama3-groq-70b-8192-tool-use-preview": {"name": "Llama 3 Groq 70B Tool Use (Preview)", "tokens": 4096, "developer": "Groq"},
|
85 |
-
"gemma-7b-it": {"name": "Gemma-7b-it", "tokens": 4096, "developer": "Google"},
|
86 |
-
"mixtral-8x7b-32768": {
|
87 |
-
"name": "Mixtral-8x7b-Instruct-v0.1",
|
88 |
-
"tokens": 32768,
|
89 |
-
"developer": "Mistral",
|
90 |
-
},
|
91 |
-
}
|
92 |
-
|
93 |
-
# Layout for model selection and max_tokens slider
|
94 |
-
col1, col2 = st.columns([1, 3]) # Adjust the ratio to make the first column smaller
|
95 |
-
|
96 |
-
|
97 |
-
with col1:
|
98 |
-
model_option = st.selectbox(
|
99 |
-
"Choose a model:",
|
100 |
-
options=list(models.keys()),
|
101 |
-
format_func=lambda x: models[x]["name"],
|
102 |
-
index=0, # Default to the first model in the list
|
103 |
-
)
|
104 |
-
max_tokens_range = models[model_option]["tokens"]
|
105 |
-
max_tokens = st.slider(
|
106 |
-
"Max Tokens:",
|
107 |
-
min_value=512,
|
108 |
-
max_value=max_tokens_range,
|
109 |
-
value=min(32768, max_tokens_range),
|
110 |
-
step=512,
|
111 |
-
help=f"Adjust the maximum number of tokens (words) for the model's response. Max for selected model: {max_tokens_range}",
|
112 |
-
)
|
113 |
-
|
114 |
-
# Detect model change and clear chat history if model has changed
|
115 |
-
if st.session_state.selected_model != model_option:
|
116 |
-
st.session_state.messages = []
|
117 |
-
st.session_state.selected_model = model_option
|
118 |
-
|
119 |
-
# Add a "Clear Chat" button
|
120 |
-
if st.button("Clear Chat"):
|
121 |
-
st.session_state.messages = []
|
122 |
-
|
123 |
-
# Display chat messages from history on app rerun
|
124 |
-
for message in st.session_state.messages:
|
125 |
-
avatar = "🔋" if message["role"] == "assistant" else "🧑💻"
|
126 |
-
with st.chat_message(message["role"], avatar=avatar):
|
127 |
-
st.markdown(message["content"])
|
128 |
-
|
129 |
-
|
130 |
-
def generate_chat_responses(chat_completion) -> Generator[str, None, None]:
|
131 |
-
"""Yield chat response content from the Groq API response."""
|
132 |
-
for chunk in chat_completion:
|
133 |
-
if chunk.choices[0].delta.content:
|
134 |
-
yield chunk.choices[0].delta.content
|
135 |
-
|
136 |
-
|
137 |
-
if prompt := st.chat_input("Enter your prompt here..."):
|
138 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
139 |
-
|
140 |
-
with st.chat_message("user", avatar="🧑💻"):
|
141 |
-
st.markdown(prompt)
|
142 |
-
|
143 |
-
# Fetch response from Groq API
|
144 |
-
try:
|
145 |
-
chat_completion = client.chat.completions.create(
|
146 |
-
model=model_option,
|
147 |
-
messages=[
|
148 |
-
{"role": m["role"], "content": m["content"]}
|
149 |
-
for m in st.session_state.messages
|
150 |
-
],
|
151 |
-
max_tokens=max_tokens,
|
152 |
-
stream=True,
|
153 |
-
)
|
154 |
-
|
155 |
-
# Use the generator function with st.write_stream
|
156 |
-
with st.chat_message("assistant", avatar="🔋"):
|
157 |
-
chat_responses_generator = generate_chat_responses(chat_completion)
|
158 |
-
full_response = st.write_stream(chat_responses_generator)
|
159 |
-
except Exception as e:
|
160 |
-
st.error(e, icon="❌")
|
161 |
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import groq_gradio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
os.environ["GROQ_API_KEY"] = "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"
|
6 |
+
gr.load(
|
7 |
+
name = "llama-3.2-3b-preview",
|
8 |
+
src = groq_gradio.registry,
|
9 |
+
title = "Groq-Gradio Chat",
|
10 |
+
theme = "upsatwal/mlsc_tiet",
|
11 |
+
examples = ["Tell me a short story about a puppy",
|
12 |
+
"Write a 14 line poem about travelling in Shakespeare style",
|
13 |
+
"What are the wonders of the world?",
|
14 |
+
"List the countries in Africa and their capitals"]
|
15 |
+
).launch()
|