NinaAchache commited on
Commit
2983354
1 Parent(s): c0d0fa1

Correct api key cache

Browse files
Files changed (1) hide show
  1. app.py +25 -11
app.py CHANGED
@@ -78,21 +78,32 @@ def gen_conv(query: str, history=[system_template], ipcc=True):
78
  return gradio_format, messages, sources
79
 
80
 
81
- # Gradio
82
- def connect(text=""):
83
- openai.api_key = text
 
 
 
84
  return f"You're all set: this is your api key: {openai.api_key}"
85
 
86
-
 
87
  with gr.Blocks(title="Eki IPCC Explorer") as demo:
 
 
 
 
 
 
88
  with gr.Row():
89
- with gr.Column():
90
- api_key = gr.Textbox(label="Open AI api key")
91
- connect_btn = gr.Button(value="Connect")
92
- with gr.Column():
93
- result = gr.Textbox(label="Connection")
94
-
95
- connect_btn.click(connect, inputs=api_key, outputs=result, api_name="Connection")
 
96
  gr.Markdown(
97
  """
98
  # Ask me anything, I'm a climate expert
@@ -119,6 +130,9 @@ with gr.Blocks(title="Eki IPCC Explorer") as demo:
119
  ask.submit(
120
  fn=gen_conv, inputs=[ask, state], outputs=[chatbot, state, sources_textbox]
121
  )
 
 
 
122
 
123
  demo.launch()
124
 
 
78
  return gradio_format, messages, sources
79
 
80
 
81
+ def set_openai_api_key(api_key):
82
+ """Set the api key and return chain.
83
+ If no api_key, then None is returned.
84
+ """
85
+ os.environ["OPENAI_API_KEY"] = api_key
86
+ openai.api_key = api_key
87
  return f"You're all set: this is your api key: {openai.api_key}"
88
 
89
+
90
+ # Gradio
91
  with gr.Blocks(title="Eki IPCC Explorer") as demo:
92
+ gr.Markdown(
93
+ """
94
+ # Add your OPENAI api key First
95
+ """
96
+ )
97
+
98
  with gr.Row():
99
+ openai_api_key_textbox = gr.Textbox(
100
+ placeholder="Paste your OpenAI API key (sk-...) and hit Enter",
101
+ show_label=False,
102
+ lines=1,
103
+ type="password",
104
+ )
105
+
106
+
107
  gr.Markdown(
108
  """
109
  # Ask me anything, I'm a climate expert
 
130
  ask.submit(
131
  fn=gen_conv, inputs=[ask, state], outputs=[chatbot, state, sources_textbox]
132
  )
133
+
134
+ openai_api_key_textbox.change(set_openai_api_key, inputs=[openai_api_key_textbox])
135
+ openai_api_key_textbox.submit(set_openai_api_key, inputs=[openai_api_key_textbox])
136
 
137
  demo.launch()
138