Spaces:
Paused
Paused
Get it to load the API key from environment variables
Browse files- .gitignore +3 -1
- app.py +21 -9
.gitignore
CHANGED
@@ -1,2 +1,4 @@
|
|
1 |
/venv
|
2 |
-
/__pycache__
|
|
|
|
|
|
1 |
/venv
|
2 |
+
/__pycache__
|
3 |
+
|
4 |
+
start.sh
|
app.py
CHANGED
@@ -50,6 +50,9 @@ from langchain.llms.openai import OpenAI
|
|
50 |
import re
|
51 |
import gradio as gr
|
52 |
|
|
|
|
|
|
|
53 |
|
54 |
def cut_dialogue_history(history_memory, keep_last_n_words=400):
|
55 |
if history_memory is None or len(history_memory) == 0:
|
@@ -129,6 +132,8 @@ class ConversationBot:
|
|
129 |
return_intermediate_steps=True,
|
130 |
agent_kwargs={'prefix': VISUAL_CHATGPT_PREFIX, 'format_instructions': VISUAL_CHATGPT_FORMAT_INSTRUCTIONS, 'suffix': VISUAL_CHATGPT_SUFFIX}, )
|
131 |
|
|
|
|
|
132 |
return gr.update(visible = True)
|
133 |
|
134 |
bot = ConversationBot({'Text2Image': 'cuda:0',
|
@@ -142,6 +147,10 @@ bot = ConversationBot({'Text2Image': 'cuda:0',
|
|
142 |
# 'DepthText2Image': 'cuda:0',
|
143 |
})
|
144 |
|
|
|
|
|
|
|
|
|
145 |
with gr.Blocks(css="#chatbot {overflow:auto; height:500px;}") as demo:
|
146 |
gr.Markdown("<h3><center>Visual ChatGPT</center></h3>")
|
147 |
gr.Markdown(
|
@@ -151,18 +160,19 @@ with gr.Blocks(css="#chatbot {overflow:auto; height:500px;}") as demo:
|
|
151 |
"""
|
152 |
)
|
153 |
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
|
|
161 |
|
162 |
chatbot = gr.Chatbot(elem_id="chatbot", label="Visual ChatGPT")
|
163 |
state = gr.State([])
|
164 |
|
165 |
-
with gr.Row(visible=
|
166 |
with gr.Column(scale=0.7):
|
167 |
txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter, or upload an image").style(container=False)
|
168 |
with gr.Column(scale=0.10, min_width=0):
|
@@ -193,7 +203,9 @@ with gr.Blocks(css="#chatbot {overflow:auto; height:500px;}") as demo:
|
|
193 |
|
194 |
|
195 |
|
196 |
-
|
|
|
|
|
197 |
txt.submit(bot.run_text, [txt, state], [chatbot, state])
|
198 |
txt.submit(lambda: "", None, txt)
|
199 |
run.click(bot.run_text, [txt, state], [chatbot, state])
|
|
|
50 |
import re
|
51 |
import gradio as gr
|
52 |
|
53 |
+
import os
|
54 |
+
|
55 |
+
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
|
56 |
|
57 |
def cut_dialogue_history(history_memory, keep_last_n_words=400):
|
58 |
if history_memory is None or len(history_memory) == 0:
|
|
|
132 |
return_intermediate_steps=True,
|
133 |
agent_kwargs={'prefix': VISUAL_CHATGPT_PREFIX, 'format_instructions': VISUAL_CHATGPT_FORMAT_INSTRUCTIONS, 'suffix': VISUAL_CHATGPT_SUFFIX}, )
|
134 |
|
135 |
+
print("Agent initialized.")
|
136 |
+
|
137 |
return gr.update(visible = True)
|
138 |
|
139 |
bot = ConversationBot({'Text2Image': 'cuda:0',
|
|
|
147 |
# 'DepthText2Image': 'cuda:0',
|
148 |
})
|
149 |
|
150 |
+
if OPENAI_API_KEY:
|
151 |
+
print("OPENAI_API_KEY found in environment variables. Starting agent.")
|
152 |
+
bot.init_agent(OPENAI_API_KEY)
|
153 |
+
|
154 |
with gr.Blocks(css="#chatbot {overflow:auto; height:500px;}") as demo:
|
155 |
gr.Markdown("<h3><center>Visual ChatGPT</center></h3>")
|
156 |
gr.Markdown(
|
|
|
160 |
"""
|
161 |
)
|
162 |
|
163 |
+
if not OPENAI_API_KEY:
|
164 |
+
with gr.Row():
|
165 |
+
openai_api_key_textbox = gr.Textbox(
|
166 |
+
placeholder="Paste your OpenAI API key here to start Visual ChatGPT(sk-...) and press Enter ↵️",
|
167 |
+
show_label=False,
|
168 |
+
lines=1,
|
169 |
+
type="password",
|
170 |
+
)
|
171 |
|
172 |
chatbot = gr.Chatbot(elem_id="chatbot", label="Visual ChatGPT")
|
173 |
state = gr.State([])
|
174 |
|
175 |
+
with gr.Row(visible=bot.agent) as input_raws:
|
176 |
with gr.Column(scale=0.7):
|
177 |
txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter, or upload an image").style(container=False)
|
178 |
with gr.Column(scale=0.10, min_width=0):
|
|
|
203 |
|
204 |
|
205 |
|
206 |
+
if not OPENAI_API_KEY:
|
207 |
+
openai_api_key_textbox.submit(bot.init_agent, [openai_api_key_textbox], [input_raws])
|
208 |
+
|
209 |
txt.submit(bot.run_text, [txt, state], [chatbot, state])
|
210 |
txt.submit(lambda: "", None, txt)
|
211 |
run.click(bot.run_text, [txt, state], [chatbot, state])
|