sherrybabe1978 commited on
Commit
046de92
·
verified ·
1 Parent(s): 7a968c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -9
app.py CHANGED
@@ -4,13 +4,20 @@ import os
4
  import json
5
  import time
6
 
7
- # Initialize the Groq client with the API key from Hugging Face secrets
8
- api_key = os.environ.get("GROQ_API_KEY")
9
- if not api_key:
10
- st.error("No Groq API key found. Please add your GROQ_API_KEY to the Hugging Face Space secrets.")
11
- st.stop()
 
 
 
 
 
 
 
12
 
13
- client = groq.Groq(api_key=api_key)
14
 
15
  def make_api_call(messages, max_tokens, is_final_answer=False):
16
  for attempt in range(3):
@@ -44,10 +51,13 @@ def main():
44
  Open source [repository here](https://github.com/bklieger-groq)
45
  """)
46
 
47
- if not api_key:
48
- st.warning("Please add your GROQ_API_KEY to the Hugging Face Space secrets to use this application.")
 
 
 
49
  st.markdown("""
50
- To add your API key:
51
  1. Go to the Settings tab of this Space
52
  2. Scroll down to the "Repository secrets" section
53
  3. Click on "New secret"
 
4
  import json
5
  import time
6
 
7
+ def get_groq_client():
8
+ api_key = os.environ.get("GROQ_API_KEY")
9
+ if not api_key:
10
+ api_key = st.session_state.get('groq_api_key')
11
+
12
+ if api_key:
13
+ return groq.Groq(api_key=api_key)
14
+ return None
15
+
16
+ def set_api_key():
17
+ st.session_state['groq_api_key'] = st.session_state.api_key_input
18
+ st.experimental_rerun()
19
 
20
+ client = get_groq_client()
21
 
22
  def make_api_call(messages, max_tokens, is_final_answer=False):
23
  for attempt in range(3):
 
51
  Open source [repository here](https://github.com/bklieger-groq)
52
  """)
53
 
54
+ if not client:
55
+ st.warning("No Groq API key found. Please enter your API key below or add it to the Hugging Face Space secrets.")
56
+ api_key_input = st.text_input("Enter your Groq API key:", type="password", key="api_key_input")
57
+ st.button("Submit API Key", on_click=set_api_key)
58
+
59
  st.markdown("""
60
+ To add your API key to the Hugging Face Space secrets:
61
  1. Go to the Settings tab of this Space
62
  2. Scroll down to the "Repository secrets" section
63
  3. Click on "New secret"