asad9641 commited on
Commit
65c131d
·
verified ·
1 Parent(s): b39fa56

Create App.py

Browse files
Files changed (1) hide show
  1. App.py +37 -0
App.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from groq import Groq
4
+
5
+ # Get Groq API key from environment variable (set as HF secret)
6
+ GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "")
7
+ client = Groq(api_key=GROQ_API_KEY)
8
+
9
+ st.set_page_config(page_title="Groq AI Chatbot", page_icon="🤖")
10
+ st.title("🤖 Groq AI Chatbot (Streamlit)")
11
+
12
+ if "messages" not in st.session_state:
13
+ st.session_state["messages"] = []
14
+
15
+ # Display chat history
16
+ for msg in st.session_state["messages"]:
17
+ if msg["role"] == "user":
18
+ st.markdown(f"**You:** {msg['content']}")
19
+ else:
20
+ st.markdown(f"**AI:** {msg['content']}")
21
+
22
+ # User input
23
+ prompt = st.text_input("Type your question here:")
24
+
25
+ if prompt:
26
+ st.session_state["messages"].append({"role": "user", "content": prompt})
27
+ try:
28
+ completion = client.chat.completions.create(
29
+ model="llama-3.1-8b-instant",
30
+ messages=st.session_state["messages"]
31
+ )
32
+ reply = completion.choices[0].message.content
33
+ except Exception as e:
34
+ reply = f"(Error: {e})"
35
+
36
+ st.session_state["messages"].append({"role": "assistant", "content": reply})
37
+ st.experimental_rerun()