myn0908 commited on
Commit
eee1c6d
·
1 Parent(s): 690b0cf

update stop api button for save cost from openai api

Browse files
Files changed (2) hide show
  1. app.py +20 -12
  2. bot/utils/constanst.py +6 -1
app.py CHANGED
@@ -4,7 +4,7 @@ from queue import Empty, Queue
4
  from threading import Thread
5
  from bot.web_scrapping.crawler_and_indexer import content_crawler_and_index
6
  from bot.utils.callbacks import QueueCallback
7
- from bot.utils.constanst import set_api_key
8
  from bot.utils.show_log import logger
9
  from bot.web_scrapping.default import *
10
  from langchain.chat_models import ChatOpenAI
@@ -13,8 +13,8 @@ from langchain.schema import AIMessage, BaseMessage, HumanMessage, SystemMessage
13
 
14
 
15
  def apply_api_key(api_key):
16
- set_api_key(api_key=api_key)
17
- return 'Successfully openai api key'
18
 
19
 
20
  human_message_prompt_template = HumanMessagePromptTemplate.from_template("{text}")
@@ -166,15 +166,23 @@ def main():
166
  [system_prompt, model_name, temperature],
167
  [chat, message, chatbot, messages],
168
  )
169
- with gr.Column():
170
- status = gr.Textbox(label='API KEY STATUS')
171
- api_key_set = gr.Textbox(label='Set your OPENAI API KEY')
172
- api_key_set_button = gr.Button("Set API key")
173
- api_key_set_button.click(
174
- apply_api_key,
175
- [api_key_set],
176
- [status]
177
- )
 
 
 
 
 
 
 
 
178
  with gr.Column():
179
  system_prompt_area = gr.TextArea(
180
  default_system_prompt, lines=4, label="prompt", interactive=True
 
4
  from threading import Thread
5
  from bot.web_scrapping.crawler_and_indexer import content_crawler_and_index
6
  from bot.utils.callbacks import QueueCallback
7
+ from bot.utils.constanst import set_api_key, stop_api_key
8
  from bot.utils.show_log import logger
9
  from bot.web_scrapping.default import *
10
  from langchain.chat_models import ChatOpenAI
 
13
 
14
 
15
  def apply_api_key(api_key):
16
+ api_key = set_api_key(api_key=api_key)
17
+ return f'Successfully set {api_key}'
18
 
19
 
20
  human_message_prompt_template = HumanMessagePromptTemplate.from_template("{text}")
 
166
  [system_prompt, model_name, temperature],
167
  [chat, message, chatbot, messages],
168
  )
169
+ with gr.Row():
170
+ with gr.Column():
171
+ status = gr.Textbox(label='API KEY STATUS')
172
+ api_key_set = gr.Textbox(label='Set your OPENAI API KEY')
173
+ api_key_set_button = gr.Button("Set API key")
174
+ api_key_set_button.click(
175
+ apply_api_key,
176
+ [api_key_set],
177
+ [status]
178
+ )
179
+ with gr.Column():
180
+ status_2 = gr.Textbox(label='STOP API KEY STATUS')
181
+ stop_api_button = gr.Button('Stop API key')
182
+ stop_api_button.click(
183
+ stop_api_key,
184
+ [],
185
+ [status_2])
186
  with gr.Column():
187
  system_prompt_area = gr.TextArea(
188
  default_system_prompt, lines=4, label="prompt", interactive=True
bot/utils/constanst.py CHANGED
@@ -5,4 +5,9 @@ API_KEY = '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'
5
 
6
  def set_api_key(api_key=API_KEY):
7
  os.environ['OPENAI_API_KEY'] = api_key
8
- return True
 
 
 
 
 
 
5
 
6
  def set_api_key(api_key=API_KEY):
7
  os.environ['OPENAI_API_KEY'] = api_key
8
+ return api_key
9
+
10
+
11
+ def stop_api_key(value):
12
+ os.environ.pop('OPENAI_API_KEY', None)
13
+ return 'Stopped API key'