saherPervaiz commited on
Commit
ca18edf
·
verified ·
1 Parent(s): 4874a99

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -67
app.py CHANGED
@@ -1,69 +1,47 @@
1
  import os
2
  import streamlit as st
3
- from groq import Groq
4
-
5
- # Initialize Groq client
6
- API_KEY = os.getenv("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") # Ensure GROQ_API_KEY is set in your environment variables
7
- if not API_KEY:
8
- st.error("API key is missing. Please set the GROQ_API_KEY environment variable.")
9
- st.stop()
10
-
11
- client = Groq(api_key=API_KEY)
12
-
13
- # Streamlit app configuration
14
- st.set_page_config(page_title="AI Chatbot", page_icon="🤖", layout="wide")
15
- st.title("AI-Powered Chatbot with Groq")
16
-
17
- # Sidebar for user input
18
- st.sidebar.header("Chatbot Settings")
19
- chat_model = st.sidebar.selectbox("Select AI Model", ["llama-3.3-70b-versatile", "llama-2.0-50b-creative"])
20
-
21
- # Chat interface
22
- st.write("## Chat with AI")
23
- if "messages" not in st.session_state:
24
- st.session_state.messages = []
25
-
26
- # Display conversation history
27
- for msg in st.session_state.messages:
28
- if msg["role"] == "user":
29
- st.write(f"**You:** {msg['content']}")
30
- else:
31
- st.write(f"**AI:** {msg['content']}")
32
-
33
- # User input for the chatbot
34
- user_input = st.text_input("Enter your message:", key="user_input")
35
-
36
- # On submit, process the input
37
- if st.button("Send"):
38
- if user_input:
39
- # Add user input to the chat history
40
- st.session_state.messages.append({"role": "user", "content": user_input})
41
-
42
- # Call Groq API
43
- with st.spinner("Thinking..."):
44
- try:
45
- chat_completion = client.chat.completions.create(
46
- messages=st.session_state.messages,
47
- model=chat_model,
48
- )
49
- ai_response = chat_completion.choices[0].message.content
50
-
51
- # Add AI response to the chat history
52
- st.session_state.messages.append({"role": "assistant", "content": ai_response})
53
-
54
- # Display the AI's response
55
- st.write(f"**AI:** {ai_response}")
56
- except Exception as e:
57
- st.error(f"Error: {e}")
58
- else:
59
- st.warning("Please enter a message before sending.")
60
-
61
- # Clear conversation button
62
- if st.button("Clear Conversation"):
63
- st.session_state.messages = []
64
-
65
- # Footer
66
- st.markdown("""
67
- ---
68
- Powered by [Groq](https://groq.com) | Deployed on [Hugging Face Spaces](https://huggingface.co/spaces)
69
- """)
 
1
  import os
2
  import streamlit as st
3
+ from groq import Client
4
+
5
+ # Function to initialize Groq client
6
+ def get_groq_client():
7
+ api_key = os.getenv("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")
8
+ if not api_key:
9
+ st.error("API key is missing. Please set the GROQ_API_KEY environment variable.")
10
+ return None
11
+ return Client(api_key=api_key)
12
+
13
+ # Function to get chatbot response
14
+ def get_response(client, user_input):
15
+ try:
16
+ response = client.chat.completions.create(
17
+ messages=[{"role": "user", "content": user_input}],
18
+ model="llama-3.3-70b-versatile"
19
+ )
20
+ return response['choices'][0]['message']['content']
21
+ except Exception as e:
22
+ st.error(f"Error: {e}")
23
+ return None
24
+
25
+ # Streamlit UI
26
+ def main():
27
+ st.title("AI Chatbot using Groq")
28
+
29
+ # Get the Groq client
30
+ client = get_groq_client()
31
+ if not client:
32
+ return
33
+
34
+ # User input
35
+ user_input = st.text_input("Ask a question:")
36
+ if st.button("Get Answer"):
37
+ if user_input:
38
+ with st.spinner("Thinking..."):
39
+ answer = get_response(client, user_input)
40
+ if answer:
41
+ st.success("Response:")
42
+ st.write(answer)
43
+ else:
44
+ st.warning("Please enter a message.")
45
+
46
+ if __name__ == "__main__":
47
+ main()