ZeroTech commited on
Commit
87d2485
·
1 Parent(s): 46cc176

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -2
app.py CHANGED
@@ -8,7 +8,7 @@ import paddlehub as hub
8
  import random
9
  from encoder import get_encoder
10
 
11
- openai.api_key = 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
12
 
13
  from utils import get_tmt_client, getTextTrans_tmt
14
  tmt_client = get_tmt_client()
@@ -351,6 +351,10 @@ total_tokens = 4096
351
  max_output_tokens = 1024
352
  max_input_tokens = total_tokens - max_output_tokens
353
 
 
 
 
 
354
  def get_response_from_openai(input, chat_history, model_radio):
355
  error_1 = 'You exceeded your current quota, please check your plan and billing details.'
356
  def openai_create(input_list, model_radio):
@@ -418,6 +422,7 @@ def get_response_from_openai(input, chat_history, model_radio):
418
  return ret, response.usage
419
  except Exception as e:
420
  logger.info(f"openai_create_error__{e}")
 
421
  return ret, {"completion_tokens": -1, "prompt_tokens": -1, "total_tokens": -1}
422
 
423
  # logger.info(f'chat_history = {chat_history}')
@@ -475,6 +480,8 @@ with gr.Blocks(title='Talk to chatGPT') as demo:
475
  chat_radio = gr.Radio(["Talk to chatGPT", "Text to Image"], elem_id="chat_radio",value="Talk to chatGPT", show_label=False, visible=True)
476
  model_radio = gr.Radio(["GPT-3.0", "GPT-3.5"], elem_id="model_radio", value="GPT-3.5",
477
  label='GPT model: ', show_label=True,interactive=True, visible=True)
 
 
478
  with gr.Row(elem_id="btns_row"):
479
  with gr.Column(id="submit_col"):
480
  submit_btn = gr.Button(value = "submit",elem_id="submit-btn").style(
@@ -495,6 +502,15 @@ with gr.Blocks(title='Talk to chatGPT') as demo:
495
  with gr.Row(elem_id='tab_img', visible=False).style(height=5):
496
  tab_img = gr.TabbedInterface(tab_actions, tab_titles)
497
 
498
-
 
 
 
 
 
 
 
 
 
499
 
500
  demo.launch(debug = True)
 
8
  import random
9
  from encoder import get_encoder
10
 
11
+ openai.api_key = os.getenv("OPENAI_API_KEY")
12
 
13
  from utils import get_tmt_client, getTextTrans_tmt
14
  tmt_client = get_tmt_client()
 
351
  max_output_tokens = 1024
352
  max_input_tokens = total_tokens - max_output_tokens
353
 
354
+ def set_openai_api_key(api_key):
355
+ if api_key and api_key.startswith("sk-") and len(api_key) > 50:
356
+ openai.api_key = api_key
357
+
358
  def get_response_from_openai(input, chat_history, model_radio):
359
  error_1 = 'You exceeded your current quota, please check your plan and billing details.'
360
  def openai_create(input_list, model_radio):
 
422
  return ret, response.usage
423
  except Exception as e:
424
  logger.info(f"openai_create_error__{e}")
425
+ ret = f"Openai said: {e} Perhaps enter your OpenAI API key."
426
  return ret, {"completion_tokens": -1, "prompt_tokens": -1, "total_tokens": -1}
427
 
428
  # logger.info(f'chat_history = {chat_history}')
 
480
  chat_radio = gr.Radio(["Talk to chatGPT", "Text to Image"], elem_id="chat_radio",value="Talk to chatGPT", show_label=False, visible=True)
481
  model_radio = gr.Radio(["GPT-3.0", "GPT-3.5"], elem_id="model_radio", value="GPT-3.5",
482
  label='GPT model: ', show_label=True,interactive=True, visible=True)
483
+ openai_api_key_textbox = gr.Textbox(placeholder="Paste your OpenAI API key (sk-...) and hit Enter",
484
+ show_label=False, lines=1, type='password')
485
  with gr.Row(elem_id="btns_row"):
486
  with gr.Column(id="submit_col"):
487
  submit_btn = gr.Button(value = "submit",elem_id="submit-btn").style(
 
502
  with gr.Row(elem_id='tab_img', visible=False).style(height=5):
503
  tab_img = gr.TabbedInterface(tab_actions, tab_titles)
504
 
505
+ openai_api_key_textbox.change(set_openai_api_key,
506
+ inputs=[openai_api_key_textbox],
507
+ outputs=[])
508
+ openai_api_key_textbox.submit(set_openai_api_key,
509
+ inputs=[openai_api_key_textbox],
510
+ outputs=[])
511
+ chat_radio.change(fn=chat_radio_change,
512
+ inputs=[chat_radio],
513
+ outputs=[model_radio, openai_api_key_textbox],
514
+ )
515
 
516
  demo.launch(debug = True)