Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,13 +3,15 @@ import os
|
|
| 3 |
import sys
|
| 4 |
import json
|
| 5 |
import requests
|
|
|
|
|
|
|
| 6 |
|
| 7 |
MODEL = "gpt-4o-2024-08-06"
|
| 8 |
API_URL = os.getenv("API_URL")
|
| 9 |
DISABLED = os.getenv("DISABLED") == 'True'
|
| 10 |
-
|
| 11 |
print (API_URL)
|
| 12 |
-
print (
|
| 13 |
NUM_THREADS = int(os.getenv("NUM_THREADS"))
|
| 14 |
|
| 15 |
print (NUM_THREADS)
|
|
@@ -18,20 +20,6 @@ def exception_handler(exception_type, exception, traceback):
|
|
| 18 |
print("%s: %s" % (exception_type.__name__, exception))
|
| 19 |
sys.excepthook = exception_handler
|
| 20 |
sys.tracebacklimit = 0
|
| 21 |
-
|
| 22 |
-
#https://github.com/gradio-app/gradio/issues/3531#issuecomment-1484029099
|
| 23 |
-
def parse_codeblock(text):
|
| 24 |
-
lines = text.split("\n")
|
| 25 |
-
for i, line in enumerate(lines):
|
| 26 |
-
if "```" in line:
|
| 27 |
-
if line != "```":
|
| 28 |
-
lines[i] = f'<pre><code class="{lines[i][3:]}">'
|
| 29 |
-
else:
|
| 30 |
-
lines[i] = '</code></pre>'
|
| 31 |
-
else:
|
| 32 |
-
if i > 0:
|
| 33 |
-
lines[i] = "<br/>" + line.replace("<", "<").replace(">", ">")
|
| 34 |
-
return "".join(lines)
|
| 35 |
|
| 36 |
def predict(inputs, top_p, temperature, chat_counter, chatbot, history, request:gr.Request):
|
| 37 |
payload = {
|
|
@@ -44,11 +32,13 @@ def predict(inputs, top_p, temperature, chat_counter, chatbot, history, request:
|
|
| 44 |
"presence_penalty":0,
|
| 45 |
"frequency_penalty":0,
|
| 46 |
}
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
headers = {
|
| 49 |
"Content-Type": "application/json",
|
| 50 |
"Authorization": f"Bearer {OPENAI_API_KEY}",
|
| 51 |
-
"Headers": f"{
|
| 52 |
}
|
| 53 |
|
| 54 |
# print(f"chat_counter - {chat_counter}")
|
|
@@ -95,6 +85,8 @@ def predict(inputs, top_p, temperature, chat_counter, chatbot, history, request:
|
|
| 95 |
# raise Exception(f"Sorry, hitting rate limit. Please try again later. {response}")
|
| 96 |
|
| 97 |
for chunk in response.iter_lines():
|
|
|
|
|
|
|
| 98 |
#Skipping first chunk
|
| 99 |
if counter == 0:
|
| 100 |
counter += 1
|
|
@@ -111,17 +103,17 @@ def predict(inputs, top_p, temperature, chat_counter, chatbot, history, request:
|
|
| 111 |
else:
|
| 112 |
history[-1] = partial_words
|
| 113 |
token_counter += 1
|
| 114 |
-
yield [(
|
| 115 |
except Exception as e:
|
| 116 |
print (f'error found: {e}')
|
| 117 |
-
yield [(
|
| 118 |
print(json.dumps({"chat_counter": chat_counter, "payload": payload, "partial_words": partial_words, "token_counter": token_counter, "counter": counter}))
|
| 119 |
|
| 120 |
|
| 121 |
def reset_textbox():
|
| 122 |
return gr.update(value='', interactive=False), gr.update(interactive=False)
|
| 123 |
|
| 124 |
-
title = """<h1 align="center">GPT-
|
| 125 |
if DISABLED:
|
| 126 |
title = """<h1 align="center" style="color:red">This app has reached OpenAI's usage limit. Please check back tomorrow.</h1>"""
|
| 127 |
description = """Language models can be conditioned to act like dialogue agents through a conversational prompt that typically takes the form:
|
|
@@ -141,8 +133,6 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
|
|
| 141 |
#chatbot {height: 520px; overflow: auto;}""",
|
| 142 |
theme=theme) as demo:
|
| 143 |
gr.HTML(title)
|
| 144 |
-
#gr.HTML("""<h3 align="center">This app provides you full access to GPT-4O (128K token limit). You don't need any OPENAI API key.</h1>""")
|
| 145 |
-
#gr.HTML("""<h3 align="center" style="color: red;">If this app is too busy, consider trying our other GPT-4O app. Visit it below:<br/><a href="https://huggingface.co/spaces/yuntian-deng/ChatGPT4Turbo">https://huggingface.co/spaces/yuntian-deng/ChatGPT4Turbo</a></h3>""")
|
| 146 |
gr.HTML("""<h3 align="center" style="color: red;">If this app doesn't respond, consider trying our other GPT-4O app:<br/><a href="https://huggingface.co/spaces/yuntian-deng/ChatGPT4Turbo">https://huggingface.co/spaces/yuntian-deng/ChatGPT4Turbo</a></h3>""")
|
| 147 |
|
| 148 |
#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>''')
|
|
@@ -154,7 +144,7 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
|
|
| 154 |
state = gr.State([]) #s
|
| 155 |
with gr.Row():
|
| 156 |
with gr.Column(scale=7):
|
| 157 |
-
b1 = gr.Button(visible=not DISABLED)
|
| 158 |
with gr.Column(scale=3):
|
| 159 |
server_status_code = gr.Textbox(label="Status code from OpenAI server", )
|
| 160 |
|
|
@@ -186,9 +176,9 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
|
|
| 186 |
accept_button = gr.Button("I Agree")
|
| 187 |
|
| 188 |
def enable_inputs():
|
| 189 |
-
return
|
| 190 |
|
| 191 |
-
accept_button.click(None, None, accept_checkbox,
|
| 192 |
accept_checkbox.change(fn=enable_inputs, inputs=[], outputs=[user_consent_block, main_block], queue=False)
|
| 193 |
|
| 194 |
inputs.submit(reset_textbox, [], [inputs, b1], queue=False)
|
|
@@ -196,4 +186,4 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
|
|
| 196 |
b1.click(reset_textbox, [], [inputs, b1], queue=False)
|
| 197 |
b1.click(predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code, inputs, b1],) #openai_api_key
|
| 198 |
|
| 199 |
-
demo.queue(max_size=
|
|
|
|
| 3 |
import sys
|
| 4 |
import json
|
| 5 |
import requests
|
| 6 |
+
import random
|
| 7 |
+
|
| 8 |
|
| 9 |
MODEL = "gpt-4o-2024-08-06"
|
| 10 |
API_URL = os.getenv("API_URL")
|
| 11 |
DISABLED = os.getenv("DISABLED") == 'True'
|
| 12 |
+
OPENAI_API_KEYS = os.getenv("OPENAI_API_KEYS").split(',')
|
| 13 |
print (API_URL)
|
| 14 |
+
print (OPENAI_API_KEYS)
|
| 15 |
NUM_THREADS = int(os.getenv("NUM_THREADS"))
|
| 16 |
|
| 17 |
print (NUM_THREADS)
|
|
|
|
| 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 = {
|
|
|
|
| 32 |
"presence_penalty":0,
|
| 33 |
"frequency_penalty":0,
|
| 34 |
}
|
| 35 |
+
OPENAI_API_KEY = random.choice(OPENAI_API_KEYS)
|
| 36 |
+
print (OPENAI_API_KEY)
|
| 37 |
+
headers_dict = {key.decode('utf-8'): value.decode('utf-8') for key, value in request.headers.raw}
|
| 38 |
headers = {
|
| 39 |
"Content-Type": "application/json",
|
| 40 |
"Authorization": f"Bearer {OPENAI_API_KEY}",
|
| 41 |
+
"Headers": f"{headers_dict}"
|
| 42 |
}
|
| 43 |
|
| 44 |
# print(f"chat_counter - {chat_counter}")
|
|
|
|
| 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()
|
| 90 |
#Skipping first chunk
|
| 91 |
if counter == 0:
|
| 92 |
counter += 1
|
|
|
|
| 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)
|
| 115 |
|
| 116 |
+
title = """<h1 align="center">GPT-4o: Research Preview (Short-Term Availability)</h1>"""
|
| 117 |
if DISABLED:
|
| 118 |
title = """<h1 align="center" style="color:red">This app has reached OpenAI's usage limit. Please check back tomorrow.</h1>"""
|
| 119 |
description = """Language models can be conditioned to act like dialogue agents through a conversational prompt that typically takes the form:
|
|
|
|
| 133 |
#chatbot {height: 520px; overflow: auto;}""",
|
| 134 |
theme=theme) as demo:
|
| 135 |
gr.HTML(title)
|
|
|
|
|
|
|
| 136 |
gr.HTML("""<h3 align="center" style="color: red;">If this app doesn't respond, consider trying our other GPT-4O app:<br/><a href="https://huggingface.co/spaces/yuntian-deng/ChatGPT4Turbo">https://huggingface.co/spaces/yuntian-deng/ChatGPT4Turbo</a></h3>""")
|
| 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>''')
|
|
|
|
| 144 |
state = gr.State([]) #s
|
| 145 |
with gr.Row():
|
| 146 |
with gr.Column(scale=7):
|
| 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 |
|
|
|
|
| 176 |
accept_button = gr.Button("I Agree")
|
| 177 |
|
| 178 |
def enable_inputs():
|
| 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=[user_consent_block, main_block], queue=False)
|
| 183 |
|
| 184 |
inputs.submit(reset_textbox, [], [inputs, b1], queue=False)
|
|
|
|
| 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)
|