Naz786 commited on
Commit
ac9e194
·
verified ·
1 Parent(s): 19db98c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ from groq import Groq
4
+
5
+ # Load API key from environment variable
6
+ api_key = os.getenv("GROQ_API_KEY")
7
+
8
+ # Initialize Groq client with the secret API key
9
+ client = Groq(api_key=api_key)
10
+
11
+ # Streamlit page config
12
+ st.set_page_config(page_title="Groq Chatbot", page_icon="🤖")
13
+ st.title("🤖 Groq Chatbot (LLaMA3-70B)")
14
+
15
+ # Initialize chat history in session state
16
+ if "messages" not in st.session_state:
17
+ st.session_state.messages = []
18
+
19
+ # Display previous chat messages
20
+ for msg in st.session_state.messages:
21
+ st.chat_message(msg["role"]).markdown(msg["content"])
22
+
23
+ # User input
24
+ prompt = st.chat_input("Type your message...")
25
+
26
+ if prompt:
27
+ # Append user message
28
+ st.session_state.messages.append({"role": "user", "content": prompt})
29
+ st.chat_message("user").markdown(prompt)
30
+
31
+ # Get response from Groq API
32
+ with st.spinner("Thinking..."):
33
+ try:
34
+ response = client.chat.completions.create(
35
+ model="llama3-70b-8192",
36
+ messages=[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages],
37
+ )
38
+ reply = response.choices[0].message.content
39
+ except Exception as e:
40
+ reply = f"⚠️ Error: {str(e)}"
41
+
42
+ # Append and display assistant reply
43
+ st.session_state.messages.append({"role": "assistant", "content": reply})
44
+ st.chat_message("assistant").markdown(reply)