Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
from groq import Groq
|
4 |
+
|
5 |
+
# Set up Streamlit page config
|
6 |
+
st.set_page_config(page_title="Groq Chatbot", page_icon="🤖")
|
7 |
+
|
8 |
+
# Title
|
9 |
+
st.title("🤖 Chat with Groq LLM")
|
10 |
+
|
11 |
+
# Input API Key (optional for local testing, hidden in HF Spaces)
|
12 |
+
if "GROQ_API_KEY" not in os.environ:
|
13 |
+
groq_api_key = st.text_input("Enter your GROQ API Key", type="password")
|
14 |
+
else:
|
15 |
+
groq_api_key = os.environ.get("GROQ_API_KEY")
|
16 |
+
|
17 |
+
# Initialize chat history
|
18 |
+
if "messages" not in st.session_state:
|
19 |
+
st.session_state.messages = []
|
20 |
+
|
21 |
+
# Show chat history
|
22 |
+
for msg in st.session_state.messages:
|
23 |
+
st.chat_message(msg["role"]).markdown(msg["content"])
|
24 |
+
|
25 |
+
# Input field
|
26 |
+
user_input = st.chat_input("Type your message here...")
|
27 |
+
|
28 |
+
if user_input and groq_api_key:
|
29 |
+
# Display user message
|
30 |
+
st.chat_message("user").markdown(user_input)
|
31 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
32 |
+
|
33 |
+
# Send request to Groq API
|
34 |
+
client = Groq(api_key=groq_api_key)
|
35 |
+
try:
|
36 |
+
response = client.chat.completions.create(
|
37 |
+
messages=st.session_state.messages,
|
38 |
+
model="llama3-70b-8192" # Or "llama-3.3-70b-versatile" if that's valid
|
39 |
+
)
|
40 |
+
reply = response.choices[0].message.content
|
41 |
+
st.chat_message("assistant").markdown(reply)
|
42 |
+
st.session_state.messages.append({"role": "assistant", "content": reply})
|
43 |
+
except Exception as e:
|
44 |
+
st.error(f"Error: {e}")
|