ysharma HF staff commited on
Commit
d7a12c5
1 Parent(s): c0531f2

added key textbox and feature

Browse files
Files changed (1) hide show
  1. app.py +16 -8
app.py CHANGED
@@ -10,11 +10,11 @@ API_URL = "https://api.openai.com/v1/chat/completions" #os.getenv("API_URL") + "
10
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
11
 
12
  #Inferenec function
13
- def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
14
 
15
  headers = {
16
  "Content-Type": "application/json",
17
- "Authorization": f"Bearer {OPENAI_API_KEY}"
18
  }
19
  print(f"system message is ^^ {system_msg}")
20
  if system_msg.strip() == '':
@@ -128,10 +128,18 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
128
  gr.HTML('''<center><a href="https://huggingface.co/spaces/ysharma/ChatGPT4?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>Duplicate the Space and run securely with your OpenAI API Key</center>''')
129
 
130
  with gr.Column(elem_id = "col_container"):
131
- #GPT4 API Key is provided by Huggingface
132
- with gr.Accordion(label="System message:", open=False):
133
- system_msg = gr.Textbox(label="Instruct the AI Assistant to set its beaviour", info = system_msg_info, value="")
134
- accordion_msg = gr.HTML(value="🚧 To set System message you will have to refresh the app", visible=False)
 
 
 
 
 
 
 
 
135
  chatbot = gr.Chatbot(label='GPT4', elem_id="chatbot")
136
  inputs = gr.Textbox(placeholder= "Hi there!", label= "Type an input and press Enter")
137
  state = gr.State([])
@@ -148,8 +156,8 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
148
  chat_counter = gr.Number(value=0, visible=False, precision=0)
149
 
150
  #Event handling
151
- inputs.submit( predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code],) #openai_api_key
152
- b1.click( predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code],) #openai_api_key
153
 
154
  inputs.submit(set_visible_false, [], [system_msg])
155
  b1.click(set_visible_false, [], [system_msg])
 
10
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
11
 
12
  #Inferenec function
13
+ def predict(openai_gpt4_key, system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
14
 
15
  headers = {
16
  "Content-Type": "application/json",
17
+ "Authorization": f"Bearer {openai_gpt4_key}" #Users will provide their own OPENAI_API_KEY
18
  }
19
  print(f"system message is ^^ {system_msg}")
20
  if system_msg.strip() == '':
 
128
  gr.HTML('''<center><a href="https://huggingface.co/spaces/ysharma/ChatGPT4?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>Duplicate the Space and run securely with your OpenAI API Key</center>''')
129
 
130
  with gr.Column(elem_id = "col_container"):
131
+ #Users need to provide their own GPT4 API key, it is no longer provided by Huggingface
132
+ with gr.Row():
133
+ openai_gpt4_key = gr.Textbox(label="OpenAI GPT4 Key", value="", type="password", placeholder="sk..", info = "You have to provide your own GPT4 keys for this app to function properly",)
134
+ with gr.Accordion(label="System message:", open=False):
135
+ system_msg = gr.Textbox(label="Instruct the AI Assistant to set its beaviour", info = system_msg_info, value="",placeholder="Type here..")
136
+ accordion_msg = gr.HTML(value="🚧 To set System message you will have to refresh the app", visible=False)
137
+
138
+ #with gr.Column(elem_id = "col_container"):
139
+ # #GPT4 API Key is provided by Huggingface
140
+ # with gr.Accordion(label="System message:", open=False):
141
+ # system_msg = gr.Textbox(label="Instruct the AI Assistant to set its beaviour", info = system_msg_info, value="")
142
+ # accordion_msg = gr.HTML(value="🚧 To set System message you will have to refresh the app", visible=False)
143
  chatbot = gr.Chatbot(label='GPT4', elem_id="chatbot")
144
  inputs = gr.Textbox(placeholder= "Hi there!", label= "Type an input and press Enter")
145
  state = gr.State([])
 
156
  chat_counter = gr.Number(value=0, visible=False, precision=0)
157
 
158
  #Event handling
159
+ inputs.submit( predict, [openai_gpt4_key, system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code],) #openai_api_key
160
+ b1.click( predict, [openai_gpt4_key, system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code],) #openai_api_key
161
 
162
  inputs.submit(set_visible_false, [], [system_msg])
163
  b1.click(set_visible_false, [], [system_msg])