youngtsai commited on
Commit
422a4ed
·
1 Parent(s): 541bd4e

MAX_WITHOUT_KEY

Browse files
Files changed (1) hide show
  1. app.py +11 -5
app.py CHANGED
@@ -19,11 +19,17 @@ def transcribe(audio, chatbot_history, openai_key):
19
 
20
 
21
 
22
- def openai_stream(history, openai_key, chat_model):
 
 
23
 
24
  if not history or history[-1][1]:
25
  return history
26
 
 
 
 
 
27
  history[-1][1] = ""
28
  for chunk in openai.ChatCompletion.create(
29
  model=chat_model,
@@ -32,7 +38,7 @@ def openai_stream(history, openai_key, chat_model):
32
  "content": history[-1][0]
33
  }],
34
  stream=True,
35
- api_key=openai_key,
36
  ):
37
  content = chunk["choices"][0].get("delta", {}).get("content")
38
  if content:
@@ -68,6 +74,7 @@ with gr.Blocks(theme=theme) as demo:
68
 
69
  with gr.Column(scale=2):
70
  with gr.Row():
 
71
  chat_model = gr.Dropdown(choices=available_models, value="gpt-3.5-turbo", allow_custom_value=True)
72
  openai_key = gr.Textbox(label="Enter OPENAI API Key", placeholder="Example: sk-AJDKakdAJD...")
73
  chatbot = gr.Chatbot(label="ChatGPT Dialog")
@@ -75,16 +82,15 @@ with gr.Blocks(theme=theme) as demo:
75
 
76
  streaming_event_kwargs = dict(
77
  fn=openai_stream,
78
- inputs=[chatbot, openai_key, chat_model],
79
  outputs=chatbot,
80
  )
81
 
82
-
83
  msg.submit(show_message, [msg, chatbot], [msg, chatbot], queue=False).then(
84
  **streaming_event_kwargs
85
  )
86
  audio.stream(transcribe, inputs=[audio, chatbot, openai_key], outputs=[chatbot]).then(
87
- **streaming_event_kwargs
88
  )
89
 
90
  clear.click(lambda: None, None, chatbot, queue=False)
 
19
 
20
 
21
 
22
+ MAX_WITHOUT_KEY = 5
23
+
24
+ def openai_stream(history, openai_key, chat_model, use_key):
25
 
26
  if not history or history[-1][1]:
27
  return history
28
 
29
+ if not use_key and len(history) >= MAX_WITHOUT_KEY:
30
+ history[-1][1] = "Sorry, you've reached the maximum number of messages without an OpenAI key."
31
+ return history
32
+
33
  history[-1][1] = ""
34
  for chunk in openai.ChatCompletion.create(
35
  model=chat_model,
 
38
  "content": history[-1][0]
39
  }],
40
  stream=True,
41
+ api_key=openai_key if use_key else None,
42
  ):
43
  content = chunk["choices"][0].get("delta", {}).get("content")
44
  if content:
 
74
 
75
  with gr.Column(scale=2):
76
  with gr.Row():
77
+ use_key = gr.Switch(label="Use OpenAI Key")
78
  chat_model = gr.Dropdown(choices=available_models, value="gpt-3.5-turbo", allow_custom_value=True)
79
  openai_key = gr.Textbox(label="Enter OPENAI API Key", placeholder="Example: sk-AJDKakdAJD...")
80
  chatbot = gr.Chatbot(label="ChatGPT Dialog")
 
82
 
83
  streaming_event_kwargs = dict(
84
  fn=openai_stream,
85
+ inputs=[chatbot, openai_key, chat_model, use_key],
86
  outputs=chatbot,
87
  )
88
 
 
89
  msg.submit(show_message, [msg, chatbot], [msg, chatbot], queue=False).then(
90
  **streaming_event_kwargs
91
  )
92
  audio.stream(transcribe, inputs=[audio, chatbot, openai_key], outputs=[chatbot]).then(
93
+ **streaming_event_kwargs
94
  )
95
 
96
  clear.click(lambda: None, None, chatbot, queue=False)