anzorq commited on
Commit
7c5907d
1 Parent(s): 2feb5a4
Files changed (1) hide show
  1. app.py +11 -3
app.py CHANGED
@@ -29,7 +29,7 @@ def download_prompt_templates():
29
  return gr.update(value=choices[0], choices=choices)
30
 
31
  def on_token_change(user_token):
32
- openai.api_key = user_token or os.environ.get("OPENAI_API_KEY")
33
 
34
  def on_prompt_template_change(prompt_template):
35
  if not isinstance(prompt_template, str): return
@@ -49,6 +49,14 @@ def submit_message(user_token, prompt, prompt_template, temperature, max_tokens,
49
  system_prompt = [{ "role": "system", "content": prompt_template }]
50
 
51
  prompt_msg = { "role": "user", "content": prompt }
 
 
 
 
 
 
 
 
52
 
53
  try:
54
  completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=system_prompt + history + [prompt_msg], temperature=temperature, max_tokens=max_tokens)
@@ -68,7 +76,7 @@ def submit_message(user_token, prompt, prompt_template, temperature, max_tokens,
68
  total_tokens_used_msg = f"Total tokens used: {state['total_tokens']}"
69
  chat_messages = [(history[i]['content'], history[i+1]['content']) for i in range(0, len(history)-1, 2)]
70
 
71
- return gr.update(value=''), chat_messages, total_tokens_used_msg, state
72
 
73
  def clear_conversation():
74
  return gr.update(value=None, visible=True), None, "", get_empty_state()
@@ -97,7 +105,7 @@ with gr.Blocks(css=css) as demo:
97
  with gr.Column():
98
  with gr.Row():
99
  gr.Markdown("Enter your own OpenAI API Key. You can get one [here](https://platform.openai.com/account/api-keys).", elem_id="label")
100
- user_token = gr.Textbox(value='stub', placeholder="OpenAI API Key", type="password", show_label=False)
101
  with gr.Row():
102
  with gr.Column():
103
  chatbot = gr.Chatbot(elem_id="chatbox")
 
29
  return gr.update(value=choices[0], choices=choices)
30
 
31
  def on_token_change(user_token):
32
+ openai.api_key = user_token
33
 
34
  def on_prompt_template_change(prompt_template):
35
  if not isinstance(prompt_template, str): return
 
49
  system_prompt = [{ "role": "system", "content": prompt_template }]
50
 
51
  prompt_msg = { "role": "user", "content": prompt }
52
+
53
+ if not user_token:
54
+ history.append(prompt_msg)
55
+ history.append({
56
+ "role": "system",
57
+ "content": "Error: OpenAI API Key is not set."
58
+ })
59
+ return '', [(history[i]['content'], history[i+1]['content']) for i in range(0, len(history)-1, 2)], f"Total tokens used: 0", state
60
 
61
  try:
62
  completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=system_prompt + history + [prompt_msg], temperature=temperature, max_tokens=max_tokens)
 
76
  total_tokens_used_msg = f"Total tokens used: {state['total_tokens']}"
77
  chat_messages = [(history[i]['content'], history[i+1]['content']) for i in range(0, len(history)-1, 2)]
78
 
79
+ return '', chat_messages, total_tokens_used_msg, state
80
 
81
  def clear_conversation():
82
  return gr.update(value=None, visible=True), None, "", get_empty_state()
 
105
  with gr.Column():
106
  with gr.Row():
107
  gr.Markdown("Enter your own OpenAI API Key. You can get one [here](https://platform.openai.com/account/api-keys).", elem_id="label")
108
+ user_token = gr.Textbox(value='', placeholder="OpenAI API Key", type="password", show_label=False)
109
  with gr.Row():
110
  with gr.Column():
111
  chatbot = gr.Chatbot(elem_id="chatbox")