Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import json
|
| 5 |
+
from groq_chat import chat_with_groq
|
| 6 |
+
|
| 7 |
+
st.set_page_config(page_title="Groq Chatbot", page_icon="π€", layout="centered")
|
| 8 |
+
|
| 9 |
+
st.title("π€ Groq Chatbot (LLaMA3)")
|
| 10 |
+
st.markdown("Chat with a blazing-fast LLaMA model powered by Groq API.")
|
| 11 |
+
|
| 12 |
+
# Session state init
|
| 13 |
+
if "chat_history" not in st.session_state:
|
| 14 |
+
st.session_state.chat_history = []
|
| 15 |
+
if "system_prompt" not in st.session_state:
|
| 16 |
+
st.session_state.system_prompt = "You are a helpful AI assistant."
|
| 17 |
+
if "temperature" not in st.session_state:
|
| 18 |
+
st.session_state.temperature = 0.7
|
| 19 |
+
|
| 20 |
+
# Sidebar - Settings
|
| 21 |
+
with st.sidebar:
|
| 22 |
+
st.markdown("## π§ Settings")
|
| 23 |
+
st.session_state.temperature = st.slider("Temperature", 0.0, 1.0, st.session_state.temperature, 0.05)
|
| 24 |
+
st.session_state.system_prompt = st.text_area("System Prompt", value=st.session_state.system_prompt)
|
| 25 |
+
|
| 26 |
+
st.markdown("---")
|
| 27 |
+
|
| 28 |
+
# Download chat history
|
| 29 |
+
if st.session_state.chat_history:
|
| 30 |
+
chat_json = json.dumps(st.session_state.chat_history, indent=2)
|
| 31 |
+
st.download_button("πΎ Download Chat", chat_json, file_name="chat_history.json", mime="application/json")
|
| 32 |
+
|
| 33 |
+
# Upload previous chat
|
| 34 |
+
uploaded = st.file_uploader("π Load Previous Chat", type=["json"])
|
| 35 |
+
if uploaded:
|
| 36 |
+
st.session_state.chat_history = json.load(uploaded)
|
| 37 |
+
st.success("Chat loaded successfully.")
|
| 38 |
+
st.rerun()
|
| 39 |
+
|
| 40 |
+
# Switch theme (guide)
|
| 41 |
+
st.markdown("---")
|
| 42 |
+
st.info("π‘ Use βοΈ in top-right to change theme.")
|
| 43 |
+
|
| 44 |
+
# Clear button
|
| 45 |
+
if st.button("π§Ή Clear Chat"):
|
| 46 |
+
st.session_state.chat_history = []
|
| 47 |
+
st.rerun()
|
| 48 |
+
|
| 49 |
+
# Display chat
|
| 50 |
+
for msg in st.session_state.chat_history:
|
| 51 |
+
avatar = "π€" if msg["role"] == "user" else "π€"
|
| 52 |
+
with st.chat_message(msg["role"], avatar=avatar):
|
| 53 |
+
st.markdown(msg["content"])
|
| 54 |
+
|
| 55 |
+
# Chat input
|
| 56 |
+
prompt = st.chat_input("Type your message here...")
|
| 57 |
+
|
| 58 |
+
if prompt:
|
| 59 |
+
st.session_state.chat_history.append({"role": "user", "content": prompt})
|
| 60 |
+
with st.chat_message("user", avatar="π€"):
|
| 61 |
+
st.markdown(prompt)
|
| 62 |
+
|
| 63 |
+
with st.spinner("Groq is thinking..."):
|
| 64 |
+
try:
|
| 65 |
+
response = chat_with_groq(
|
| 66 |
+
prompt,
|
| 67 |
+
history=st.session_state.chat_history,
|
| 68 |
+
system_prompt=st.session_state.system_prompt,
|
| 69 |
+
temperature=st.session_state.temperature
|
| 70 |
+
)
|
| 71 |
+
reply = response["choices"][0]["message"]["content"]
|
| 72 |
+
st.session_state.chat_history.append({"role": "assistant", "content": reply})
|
| 73 |
+
|
| 74 |
+
with st.chat_message("assistant", avatar="π€"):
|
| 75 |
+
st.markdown(reply)
|
| 76 |
+
|
| 77 |
+
# Display token usage if available
|
| 78 |
+
if "usage" in response:
|
| 79 |
+
st.sidebar.markdown(f"π **Tokens Used:** {response['usage']['total_tokens']}")
|
| 80 |
+
|
| 81 |
+
# Raw response toggle
|
| 82 |
+
with st.expander("π Raw API Response"):
|
| 83 |
+
st.json(response)
|
| 84 |
+
|
| 85 |
+
except Exception as e:
|
| 86 |
+
st.error(f"π₯ API Error: {e}")
|