Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
from groq import Groq
|
| 4 |
+
|
| 5 |
+
# You will set API key directly in Colab cell environment (no .env file needed)
|
| 6 |
+
api_key = "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"
|
| 7 |
+
|
| 8 |
+
if not api_key:
|
| 9 |
+
st.error("Please set the GROQ_API_KEY environment variable.")
|
| 10 |
+
st.stop()
|
| 11 |
+
|
| 12 |
+
# Initialize GROQ client
|
| 13 |
+
client = Groq(api_key=api_key)
|
| 14 |
+
|
| 15 |
+
# Streamlit app
|
| 16 |
+
st.set_page_config(page_title="GROQ Chatbot", page_icon="🤖")
|
| 17 |
+
st.title("🤖 GROQ Chatbot")
|
| 18 |
+
|
| 19 |
+
# Session state to store messages
|
| 20 |
+
if "messages" not in st.session_state:
|
| 21 |
+
st.session_state.messages = []
|
| 22 |
+
|
| 23 |
+
# Display previous messages
|
| 24 |
+
for msg in st.session_state.messages:
|
| 25 |
+
with st.chat_message(msg["role"]):
|
| 26 |
+
st.markdown(msg["content"])
|
| 27 |
+
|
| 28 |
+
# User input
|
| 29 |
+
user_prompt = st.chat_input("Type your message...")
|
| 30 |
+
|
| 31 |
+
if user_prompt:
|
| 32 |
+
# Add user message to session state
|
| 33 |
+
st.session_state.messages.append({"role": "user", "content": user_prompt})
|
| 34 |
+
|
| 35 |
+
with st.chat_message("user"):
|
| 36 |
+
st.markdown(user_prompt)
|
| 37 |
+
|
| 38 |
+
# Send the conversation to GROQ API
|
| 39 |
+
try:
|
| 40 |
+
chat_completion = client.chat.completions.create(
|
| 41 |
+
messages=[
|
| 42 |
+
{"role": "user", "content": user_prompt}
|
| 43 |
+
],
|
| 44 |
+
model="llama-3.3-70b-versatile",
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
bot_reply = chat_completion.choices[0].message.content
|
| 48 |
+
|
| 49 |
+
# Add assistant response to session state
|
| 50 |
+
st.session_state.messages.append({"role": "assistant", "content": bot_reply})
|
| 51 |
+
|
| 52 |
+
with st.chat_message("assistant"):
|
| 53 |
+
st.markdown(bot_reply)
|
| 54 |
+
|
| 55 |
+
except Exception as e:
|
| 56 |
+
st.error(f"Error: {e}")
|