DanishShahzad commited on
Commit
e358cf4
Β·
verified Β·
1 Parent(s): a476781

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
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}")