Tanseer45203 commited on
Commit
ccc2bde
·
verified ·
1 Parent(s): ae7d3f3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -28
app.py CHANGED
@@ -1,36 +1,55 @@
1
- import os
2
  import streamlit as st
 
3
  from groq import Groq
4
 
5
- # Set the Groq API key (you can also export it as an environment variable)
6
- groq_api_key = os.getenv('GROQ_API_KEY') # or replace with your actual API key if not using environment variables
7
-
8
- if not groq_api_key:
9
- st.error("GROQ_API_KEY is not set. Please set your API key.")
10
- st.stop()
11
-
12
- # Initialize Groq client
13
- client = Groq(api_key=groq_api_key)
14
-
15
- # Function to get response from Groq API
16
- def get_groq_response(user_input):
17
  try:
18
- chat_completion = client.chat.completions.create(
19
- messages=[{"role": "user", "content": user_input}],
20
- model="llama-3.3-70b-versatile", # Specify the model you want to use
21
- )
22
- return chat_completion.choices[0].message.content
 
 
 
 
 
 
23
  except Exception as e:
24
- return f"Error occurred: {str(e)}"
25
 
26
- # Streamlit app layout
27
- st.title("Chatbot with Groq API")
28
 
29
- # Create an input box for the user to type their message
30
- user_input = st.text_input("Ask me anything:", "")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- # If user input is not empty, get the response from Groq
33
- if user_input:
34
- response = get_groq_response(user_input)
35
- st.write("### Bot Response:")
36
- st.write(response)
 
 
1
  import streamlit as st
2
+ import os
3
  from groq import Groq
4
 
5
+ # Custom function to load variables from a .env file
6
+ def load_dotenv(filepath=".env"):
 
 
 
 
 
 
 
 
 
 
7
  try:
8
+ with open(filepath, "r") as f:
9
+ for line in f:
10
+ line = line.strip()
11
+ # Skip comments and empty lines
12
+ if not line or line.startswith("#"):
13
+ continue
14
+ if "=" in line:
15
+ key, value = line.split("=", 1)
16
+ os.environ.setdefault(key.strip(), value.strip())
17
+ except FileNotFoundError:
18
+ st.error("No .env file found. Please create one with your API key.")
19
  except Exception as e:
20
+ st.error(f"Error loading .env file: {e}")
21
 
22
+ # Load environment variables from the .env file
23
+ load_dotenv()
24
 
25
+ def main():
26
+ st.title("Groq LLM Chatbot")
27
+
28
+ # Fetch API key from environment variables
29
+ groq_api_key = os.environ.get("GROQ_API_KEY")
30
+
31
+ if groq_api_key:
32
+ client = Groq(api_key=groq_api_key)
33
+ st.write("### Chat with LLM")
34
+ user_input = st.text_area("Enter your message:")
35
+
36
+ if st.button("Generate Response"):
37
+ if user_input.strip():
38
+ try:
39
+ response = client.chat.completions.create(
40
+ model="llama-3.3-70b-versatile",
41
+ messages=[
42
+ {"role": "user", "content": user_input}
43
+ ]
44
+ )
45
+ st.write("### Response:")
46
+ st.write(response.choices[0].message.content)
47
+ except Exception as e:
48
+ st.error(f"Error: {e}")
49
+ else:
50
+ st.warning("Please enter a message to generate a response.")
51
+ else:
52
+ st.error("API key not found. Please ensure your .env file contains GROQ_API_KEY.")
53
 
54
+ if __name__ == "__main__":
55
+ main()