Spaces:
No application file
No application file
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from groq import Groq
|
| 4 |
+
|
| 5 |
+
# ----------------------------
|
| 6 |
+
# Groq API key
|
| 7 |
+
# ----------------------------
|
| 8 |
+
# On Hugging Face, you'll set this as a secret: GROQ_API_KEY
|
| 9 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "")
|
| 10 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 11 |
+
|
| 12 |
+
# ----------------------------
|
| 13 |
+
# Streamlit UI
|
| 14 |
+
# ----------------------------
|
| 15 |
+
st.set_page_config(page_title="Groq AI Chatbot", page_icon="🤖")
|
| 16 |
+
st.title("🤖 Groq AI Chatbot (Streamlit)")
|
| 17 |
+
|
| 18 |
+
if "messages" not in st.session_state:
|
| 19 |
+
st.session_state["messages"] = []
|
| 20 |
+
|
| 21 |
+
# Display chat history
|
| 22 |
+
for msg in st.session_state["messages"]:
|
| 23 |
+
if msg["role"] == "user":
|
| 24 |
+
st.markdown(f"**You:** {msg['content']}")
|
| 25 |
+
else:
|
| 26 |
+
st.markdown(f"**AI:** {msg['content']}")
|
| 27 |
+
|
| 28 |
+
# User input
|
| 29 |
+
prompt = st.text_input("Type your question here:")
|
| 30 |
+
|
| 31 |
+
if prompt:
|
| 32 |
+
st.session_state["messages"].append({"role": "user", "content": prompt})
|
| 33 |
+
try:
|
| 34 |
+
completion = client.chat.completions.create(
|
| 35 |
+
model="llama-3.1-8b-instant",
|
| 36 |
+
messages=st.session_state["messages"]
|
| 37 |
+
)
|
| 38 |
+
reply = completion.choices[0].message.content
|
| 39 |
+
except Exception as e:
|
| 40 |
+
reply = f"(Error: {e})"
|
| 41 |
+
|
| 42 |
+
st.session_state["messages"].append({"role": "assistant", "content": reply})
|
| 43 |
+
st.experimental_rerun()
|