Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from groq import Groq
|
| 6 |
+
|
| 7 |
+
# Set your GROQ API Key here directly (since in Colab it's easier)
|
| 8 |
+
GROQ_API_KEY = "your-groq-api-key-here" # <-- Replace with your key
|
| 9 |
+
|
| 10 |
+
# Create the GROQ client
|
| 11 |
+
client = Groq(
|
| 12 |
+
api_key=GROQ_API_KEY,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# Streamlit app
|
| 16 |
+
st.title("🤖 Chatbot using GROQ API")
|
| 17 |
+
|
| 18 |
+
# Store the conversation
|
| 19 |
+
if "messages" not in st.session_state:
|
| 20 |
+
st.session_state.messages = []
|
| 21 |
+
|
| 22 |
+
# Display old messages
|
| 23 |
+
for message in st.session_state.messages:
|
| 24 |
+
if message["role"] == "user":
|
| 25 |
+
with st.chat_message("user"):
|
| 26 |
+
st.write(message["content"])
|
| 27 |
+
else:
|
| 28 |
+
with st.chat_message("assistant"):
|
| 29 |
+
st.write(message["content"])
|
| 30 |
+
|
| 31 |
+
# Input from user
|
| 32 |
+
user_input = st.chat_input("Type your message...")
|
| 33 |
+
|
| 34 |
+
if user_input:
|
| 35 |
+
# Save user message
|
| 36 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 37 |
+
|
| 38 |
+
# Send all messages to model
|
| 39 |
+
chat_completion = client.chat.completions.create(
|
| 40 |
+
messages=st.session_state.messages,
|
| 41 |
+
model="llama-3-3-70b-versatile",
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# Get model's reply
|
| 45 |
+
reply = chat_completion.choices[0].message.content
|
| 46 |
+
|
| 47 |
+
# Save assistant reply
|
| 48 |
+
st.session_state.messages.append({"role": "assistant", "content": reply})
|
| 49 |
+
|
| 50 |
+
# Display assistant reply
|
| 51 |
+
with st.chat_message("assistant"):
|
| 52 |
+
st.write(reply)
|