engrharis commited on
Commit
1542d75
·
verified ·
1 Parent(s): 9bfae5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -10
app.py CHANGED
@@ -1,17 +1,41 @@
1
  import os
2
-
3
  from groq import Groq
4
 
 
 
 
5
  # Initialize Groq client
6
- client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
 
 
 
 
7
 
8
  def get_response(prompt):
9
- # Create chat completion request
10
- chat_completion = client.chat.completions.create(
11
- messages=[{"role": "user", "content": prompt}],
12
- model="your_desired_model" # Replace with your preferred model
13
- )
14
- # Return the first choice's message content
15
- return chat_completion.choices[0].message.content
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- # Rest of your Streamlit app code using get_response function
 
 
 
 
 
 
 
1
  import os
2
+ import streamlit as st
3
  from groq import Groq
4
 
5
+ # Set your Groq API key (preferably via environment variable for security)
6
+ GROQ_API_KEY = os.environ.get("GROQ_API_KEY") or "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" # Fallback only for testing
7
+
8
  # Initialize Groq client
9
+ try:
10
+ client = Groq(api_key=GROQ_API_KEY)
11
+ except Exception as e:
12
+ st.error(f"Error initializing Groq client: {e}")
13
+ st.stop() # Stop execution if client initialization fails
14
 
15
  def get_response(prompt):
16
+ try:
17
+ chat_completion = client.chat.completions.create(
18
+ messages=[{"role": "user", "content": prompt}],
19
+ model="llama-3.3-70b-versatile" # Or any other available model
20
+ )
21
+ return chat_completion.choices[0].message.content
22
+ except Exception as e:
23
+ st.error(f"Error generating response: {e}")
24
+ return "An error occurred while generating the response."
25
+
26
+ # Streamlit app layout
27
+ st.title("Interactive Chatbot")
28
+
29
+ user_input = st.text_input("You: ", key="user_input")
30
+
31
+ if user_input:
32
+ bot_response = get_response(user_input)
33
+ st.write("Bot: ", bot_response)
34
 
35
+ if __name__ == "__main__":
36
+ st.sidebar.title("Settings")
37
+ st.sidebar.write("API Key Source: " + ("Environment Variable" if os.environ.get("GROQ_API_KEY") else "Directly in Code (Not Recommended)"))
38
+ if os.environ.get("GROQ_API_KEY"):
39
+ st.sidebar.success("Groq Client Initialized Successfully!")
40
+ else:
41
+ st.sidebar.warning("Using API Key Directly in Code (Not Recommended). Set GROQ_API_KEY Environment Variable for Security.")