Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import sys
|
4 |
-
import json
|
5 |
import requests
|
6 |
import random
|
7 |
|
@@ -20,7 +20,7 @@ def exception_handler(exception_type, exception, traceback):
|
|
20 |
print("%s: %s" % (exception_type.__name__, exception))
|
21 |
sys.excepthook = exception_handler
|
22 |
sys.tracebacklimit = 0
|
23 |
-
|
24 |
def predict(inputs, top_p, temperature, chat_counter, chatbot, history, request:gr.Request):
|
25 |
payload = {
|
26 |
"model": MODEL,
|
@@ -53,9 +53,9 @@ def predict(inputs, top_p, temperature, chat_counter, chatbot, history, request:
|
|
53 |
message["role"] = role
|
54 |
message["content"] = data
|
55 |
messages.append(message)
|
56 |
-
|
57 |
message = {}
|
58 |
-
message["role"] = "user"
|
59 |
message["content"] = inputs
|
60 |
messages.append(message)
|
61 |
payload = {
|
@@ -72,8 +72,8 @@ def predict(inputs, top_p, temperature, chat_counter, chatbot, history, request:
|
|
72 |
chat_counter += 1
|
73 |
|
74 |
history.append(inputs)
|
75 |
-
token_counter = 0
|
76 |
-
partial_words = ""
|
77 |
counter = 0
|
78 |
|
79 |
try:
|
@@ -83,7 +83,7 @@ def predict(inputs, top_p, temperature, chat_counter, chatbot, history, request:
|
|
83 |
#if response_code.strip() != "<Response [200]>":
|
84 |
# #print(f"response code - {response}")
|
85 |
# raise Exception(f"Sorry, hitting rate limit. Please try again later. {response}")
|
86 |
-
|
87 |
for chunk in response.iter_lines():
|
88 |
#print (chunk)
|
89 |
#sys.stdout.flush()
|
@@ -103,12 +103,12 @@ def predict(inputs, top_p, temperature, chat_counter, chatbot, history, request:
|
|
103 |
else:
|
104 |
history[-1] = partial_words
|
105 |
token_counter += 1
|
106 |
-
yield [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ], history, chat_counter, response, gr.update(interactive=False), gr.update(interactive=False) # resembles {chatbot: chat, state: history}
|
107 |
except Exception as e:
|
108 |
print (f'error found: {e}')
|
109 |
yield [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ], history, chat_counter, response, gr.update(interactive=True), gr.update(interactive=True)
|
110 |
print(json.dumps({"chat_counter": chat_counter, "payload": payload, "partial_words": partial_words, "token_counter": token_counter, "counter": counter}))
|
111 |
-
|
112 |
|
113 |
def reset_textbox():
|
114 |
return gr.update(value='', interactive=False), gr.update(interactive=False)
|
@@ -127,7 +127,7 @@ Assistant: <utterance>
|
|
127 |
In this app, you can explore the outputs of a gpt-4 turbo LLM.
|
128 |
"""
|
129 |
|
130 |
-
theme = gr.themes.Default(primary_hue="green")
|
131 |
|
132 |
with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
|
133 |
#chatbot {height: 520px; overflow: auto;}""",
|
@@ -137,7 +137,7 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
|
|
137 |
|
138 |
#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>''')
|
139 |
with gr.Column(elem_id = "col_container", visible=False) as main_block:
|
140 |
-
#GPT4 API Key is provided by Huggingface
|
141 |
#openai_api_key = gr.Textbox(type='password', label="Enter only your GPT4 OpenAI API key here")
|
142 |
chatbot = gr.Chatbot(elem_id='chatbot') #c
|
143 |
inputs = gr.Textbox(placeholder= "Hi there!", label= "Type an input and press Enter") #t
|
@@ -147,7 +147,7 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
|
|
147 |
b1 = gr.Button(visible=not DISABLED) #.style(full_width=True)
|
148 |
with gr.Column(scale=3):
|
149 |
server_status_code = gr.Textbox(label="Status code from OpenAI server", )
|
150 |
-
|
151 |
#inputs, top_p, temperature, top_k, repetition_penalty
|
152 |
with gr.Accordion("Parameters", open=False):
|
153 |
top_p = gr.Slider( minimum=-0, maximum=1.0, value=1.0, step=0.05, interactive=True, label="Top-p (nucleus sampling)",)
|
@@ -155,7 +155,7 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
|
|
155 |
#top_k = gr.Slider( minimum=1, maximum=50, value=4, step=1, interactive=True, label="Top-k",)
|
156 |
#repetition_penalty = gr.Slider( minimum=0.1, maximum=3.0, value=1.03, step=0.01, interactive=True, label="Repetition Penalty", )
|
157 |
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
158 |
-
|
159 |
with gr.Column(elem_id = "user_consent_container") as user_consent_block:
|
160 |
# Get user consent
|
161 |
accept_checkbox = gr.Checkbox(visible=False)
|
@@ -179,11 +179,11 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
|
|
179 |
return gr.update(visible=False), gr.update(visible=True)
|
180 |
|
181 |
accept_button.click(None, None, accept_checkbox, js=js, queue=False)
|
182 |
-
accept_checkbox.change(fn=enable_inputs, inputs=[], outputs=[
|
183 |
|
184 |
inputs.submit(reset_textbox, [], [inputs, b1], queue=False)
|
185 |
inputs.submit(predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code, inputs, b1],) #openai_api_key
|
186 |
b1.click(reset_textbox, [], [inputs, b1], queue=False)
|
187 |
b1.click(predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code, inputs, b1],) #openai_api_key
|
188 |
-
|
189 |
-
demo.queue(max_size=10, default_concurrency_limit=NUM_THREADS, api_open=False).launch(share=False)
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import sys
|
4 |
+
import json
|
5 |
import requests
|
6 |
import random
|
7 |
|
|
|
20 |
print("%s: %s" % (exception_type.__name__, exception))
|
21 |
sys.excepthook = exception_handler
|
22 |
sys.tracebacklimit = 0
|
23 |
+
|
24 |
def predict(inputs, top_p, temperature, chat_counter, chatbot, history, request:gr.Request):
|
25 |
payload = {
|
26 |
"model": MODEL,
|
|
|
53 |
message["role"] = role
|
54 |
message["content"] = data
|
55 |
messages.append(message)
|
56 |
+
|
57 |
message = {}
|
58 |
+
message["role"] = "user"
|
59 |
message["content"] = inputs
|
60 |
messages.append(message)
|
61 |
payload = {
|
|
|
72 |
chat_counter += 1
|
73 |
|
74 |
history.append(inputs)
|
75 |
+
token_counter = 0
|
76 |
+
partial_words = ""
|
77 |
counter = 0
|
78 |
|
79 |
try:
|
|
|
83 |
#if response_code.strip() != "<Response [200]>":
|
84 |
# #print(f"response code - {response}")
|
85 |
# raise Exception(f"Sorry, hitting rate limit. Please try again later. {response}")
|
86 |
+
|
87 |
for chunk in response.iter_lines():
|
88 |
#print (chunk)
|
89 |
#sys.stdout.flush()
|
|
|
103 |
else:
|
104 |
history[-1] = partial_words
|
105 |
token_counter += 1
|
106 |
+
yield [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ], history, chat_counter, response, gr.update(interactive=False), gr.update(interactive=False) # resembles {chatbot: chat, state: history}
|
107 |
except Exception as e:
|
108 |
print (f'error found: {e}')
|
109 |
yield [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ], history, chat_counter, response, gr.update(interactive=True), gr.update(interactive=True)
|
110 |
print(json.dumps({"chat_counter": chat_counter, "payload": payload, "partial_words": partial_words, "token_counter": token_counter, "counter": counter}))
|
111 |
+
|
112 |
|
113 |
def reset_textbox():
|
114 |
return gr.update(value='', interactive=False), gr.update(interactive=False)
|
|
|
127 |
In this app, you can explore the outputs of a gpt-4 turbo LLM.
|
128 |
"""
|
129 |
|
130 |
+
theme = gr.themes.Default(primary_hue="green")
|
131 |
|
132 |
with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
|
133 |
#chatbot {height: 520px; overflow: auto;}""",
|
|
|
137 |
|
138 |
#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>''')
|
139 |
with gr.Column(elem_id = "col_container", visible=False) as main_block:
|
140 |
+
#GPT4 API Key is provided by Huggingface
|
141 |
#openai_api_key = gr.Textbox(type='password', label="Enter only your GPT4 OpenAI API key here")
|
142 |
chatbot = gr.Chatbot(elem_id='chatbot') #c
|
143 |
inputs = gr.Textbox(placeholder= "Hi there!", label= "Type an input and press Enter") #t
|
|
|
147 |
b1 = gr.Button(visible=not DISABLED) #.style(full_width=True)
|
148 |
with gr.Column(scale=3):
|
149 |
server_status_code = gr.Textbox(label="Status code from OpenAI server", )
|
150 |
+
|
151 |
#inputs, top_p, temperature, top_k, repetition_penalty
|
152 |
with gr.Accordion("Parameters", open=False):
|
153 |
top_p = gr.Slider( minimum=-0, maximum=1.0, value=1.0, step=0.05, interactive=True, label="Top-p (nucleus sampling)",)
|
|
|
155 |
#top_k = gr.Slider( minimum=1, maximum=50, value=4, step=1, interactive=True, label="Top-k",)
|
156 |
#repetition_penalty = gr.Slider( minimum=0.1, maximum=3.0, value=1.03, step=0.01, interactive=True, label="Repetition Penalty", )
|
157 |
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
158 |
+
|
159 |
with gr.Column(elem_id = "user_consent_container") as user_consent_block:
|
160 |
# Get user consent
|
161 |
accept_checkbox = gr.Checkbox(visible=False)
|
|
|
179 |
return gr.update(visible=False), gr.update(visible=True)
|
180 |
|
181 |
accept_button.click(None, None, accept_checkbox, js=js, queue=False)
|
182 |
+
accept_checkbox.change(fn=enable_inputs, inputs=[], outputs=[ main_block], queue=False)
|
183 |
|
184 |
inputs.submit(reset_textbox, [], [inputs, b1], queue=False)
|
185 |
inputs.submit(predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code, inputs, b1],) #openai_api_key
|
186 |
b1.click(reset_textbox, [], [inputs, b1], queue=False)
|
187 |
b1.click(predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code, inputs, b1],) #openai_api_key
|
188 |
+
|
189 |
+
demo.queue(max_size=10, default_concurrency_limit=NUM_THREADS, api_open=False).launch(share=False)
|