ShubhamMhaske commited on
Commit
1bbb715
·
verified ·
1 Parent(s): 7d352d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -33
app.py CHANGED
@@ -1,44 +1,45 @@
1
  import streamlit as st
2
- import os
3
- from dotenv import load_dotenv
4
  from groq import Groq
5
 
6
- # Load environment variables from .env file
7
- load_dotenv()
8
-
9
- # Get the GROQ API key from the .env file
10
- API_KEY = os.getenv("GROQ_API_KEY")
11
- if not API_KEY:
12
- st.error("GROQ_API_KEY is not set. Please check your .env file.")
13
-
14
- # Initialize the Groq client
15
- client = Groq(api_key=API_KEY)
16
-
17
  # Streamlit App
18
  st.title("Q&A Application with Groq API")
19
  st.write("Ask a question and get a response using Groq's AI model!")
20
 
21
- # Input prompt from the user
22
- user_input = st.text_input("Enter your question:", "")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- # Button to trigger the API call
25
- if st.button("Get Answer"):
26
- if user_input.strip():
27
- try:
28
- # API call to Groq
29
- chat_completion = client.chat.completions.create(
30
- messages=[{"role": "user", "content": user_input}],
31
- model="llama3-8b-8192"
32
- )
33
- # Extracting the response
34
- response = chat_completion.choices[0].message.content
35
- # Display the response
36
- st.subheader("Response:")
37
- st.write(response)
38
- except Exception as e:
39
- st.error(f"Error: {str(e)}")
40
- else:
41
- st.warning("Please enter a valid question.")
42
 
43
  # Footer
44
  st.markdown("---")
 
1
  import streamlit as st
 
 
2
  from groq import Groq
3
 
 
 
 
 
 
 
 
 
 
 
 
4
  # Streamlit App
5
  st.title("Q&A Application with Groq API")
6
  st.write("Ask a question and get a response using Groq's AI model!")
7
 
8
+ # Input for API key
9
+ api_key = st.text_input("Enter your GROQ API Key:", type="password")
10
+
11
+ # Check if API key is provided
12
+ if not api_key:
13
+ st.warning("Please enter your GROQ API Key to proceed.")
14
+ else:
15
+ # Initialize the Groq client
16
+ try:
17
+ client = Groq(api_key=api_key)
18
+ st.success("GROQ API Key successfully set!")
19
+ except Exception as e:
20
+ st.error(f"Error initializing Groq client: {str(e)}")
21
+
22
+ # Input prompt from the user
23
+ user_input = st.text_input("Enter your question:", "")
24
 
25
+ # Button to trigger the API call
26
+ if st.button("Get Answer"):
27
+ if user_input.strip():
28
+ try:
29
+ # API call to Groq
30
+ chat_completion = client.chat.completions.create(
31
+ messages=[{"role": "user", "content": user_input}],
32
+ model="llama3-8b-8192"
33
+ )
34
+ # Extracting the response
35
+ response = chat_completion.choices[0].message.content
36
+ # Display the response
37
+ st.subheader("Response:")
38
+ st.write(response)
39
+ except Exception as e:
40
+ st.error(f"Error: {str(e)}")
41
+ else:
42
+ st.warning("Please enter a valid question.")
43
 
44
  # Footer
45
  st.markdown("---")