File size: 2,999 Bytes
e358cf4
 
 
 
 
 
 
 
07a941b
7e317c9
e358cf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# app.py

import streamlit as st
import json
from groq_chat import chat_with_groq

st.set_page_config(page_title="Groq Chatbot", page_icon="πŸ€–", layout="centered")

st.title("πŸ€– Groq Chatbot (Kimi)")
st.markdown("Chat with a blazing-fast Moonshot AI's Kimi model powered by Groq API.")

# Session state init
if "chat_history" not in st.session_state:
    st.session_state.chat_history = []
if "system_prompt" not in st.session_state:
    st.session_state.system_prompt = "You are a helpful AI assistant."
if "temperature" not in st.session_state:
    st.session_state.temperature = 0.7

# Sidebar - Settings
with st.sidebar:
    st.markdown("## πŸ”§ Settings")
    st.session_state.temperature = st.slider("Temperature", 0.0, 1.0, st.session_state.temperature, 0.05)
    st.session_state.system_prompt = st.text_area("System Prompt", value=st.session_state.system_prompt)

    st.markdown("---")

    # Download chat history
    if st.session_state.chat_history:
        chat_json = json.dumps(st.session_state.chat_history, indent=2)
        st.download_button("πŸ’Ύ Download Chat", chat_json, file_name="chat_history.json", mime="application/json")

    # Upload previous chat
    uploaded = st.file_uploader("πŸ“‚ Load Previous Chat", type=["json"])
    if uploaded:
        st.session_state.chat_history = json.load(uploaded)
        st.success("Chat loaded successfully.")
        st.rerun()

    # Switch theme (guide)
    st.markdown("---")
    st.info("πŸ’‘ Use βš™οΈ in top-right to change theme.")

# Clear button
if st.button("🧹 Clear Chat"):
    st.session_state.chat_history = []
    st.rerun()

# Display chat
for msg in st.session_state.chat_history:
    avatar = "πŸ‘€" if msg["role"] == "user" else "πŸ€–"
    with st.chat_message(msg["role"], avatar=avatar):
        st.markdown(msg["content"])

# Chat input
prompt = st.chat_input("Type your message here...")

if prompt:
    st.session_state.chat_history.append({"role": "user", "content": prompt})
    with st.chat_message("user", avatar="πŸ‘€"):
        st.markdown(prompt)

    with st.spinner("Groq is thinking..."):
        try:
            response = chat_with_groq(
                prompt,
                history=st.session_state.chat_history,
                system_prompt=st.session_state.system_prompt,
                temperature=st.session_state.temperature
            )
            reply = response["choices"][0]["message"]["content"]
            st.session_state.chat_history.append({"role": "assistant", "content": reply})

            with st.chat_message("assistant", avatar="πŸ€–"):
                st.markdown(reply)

            # Display token usage if available
            if "usage" in response:
                st.sidebar.markdown(f"πŸ“Š **Tokens Used:** {response['usage']['total_tokens']}")

            # Raw response toggle
            with st.expander("πŸ” Raw API Response"):
                st.json(response)

        except Exception as e:
            st.error(f"πŸ’₯ API Error: {e}")