Spaces:
Runtime error
Runtime error
Update app.py
#28
by
ouhenio
- opened
app.py
CHANGED
|
@@ -1,121 +1,90 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
-
import json
|
| 5 |
-
import requests
|
| 6 |
import random
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
print (API_URL)
|
| 14 |
-
print (OPENAI_API_KEYS)
|
| 15 |
-
NUM_THREADS = int(os.getenv("NUM_THREADS"))
|
| 16 |
-
|
| 17 |
-
print (NUM_THREADS)
|
| 18 |
|
| 19 |
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,
|
| 27 |
-
"messages": [{"role": "user", "content": f"{inputs}"}],
|
| 28 |
-
"temperature": temperature,
|
| 29 |
-
"top_p": top_p,
|
| 30 |
-
"n": 1,
|
| 31 |
-
"stream": True,
|
| 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 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
"Headers": f"{headers_dict}"
|
| 42 |
-
}
|
| 43 |
-
|
| 44 |
-
# print(f"chat_counter - {chat_counter}")
|
| 45 |
-
if chat_counter != 0 :
|
| 46 |
-
messages = []
|
| 47 |
for i, data in enumerate(history):
|
| 48 |
-
if i % 2 == 0
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 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 = {
|
| 62 |
-
"model": MODEL,
|
| 63 |
-
"messages": messages,
|
| 64 |
-
"temperature": temperature,
|
| 65 |
-
"top_p": top_p,
|
| 66 |
-
"n": 1,
|
| 67 |
-
"stream": True,
|
| 68 |
-
"presence_penalty":0,
|
| 69 |
-
"frequency_penalty":0,
|
| 70 |
-
}
|
| 71 |
-
|
| 72 |
chat_counter += 1
|
| 73 |
-
|
| 74 |
history.append(inputs)
|
| 75 |
-
token_counter = 0
|
| 76 |
-
partial_words = ""
|
| 77 |
-
|
| 78 |
-
|
| 79 |
try:
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
for chunk in
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
chunk = chunk.decode()
|
| 98 |
-
# decode each line as response data is in bytes
|
| 99 |
-
if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']:
|
| 100 |
-
partial_words = partial_words + json.loads(chunk[6:])['choices'][0]["delta"]["content"]
|
| 101 |
-
if token_counter == 0:
|
| 102 |
-
history.append(" " + partial_words)
|
| 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
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
def reset_textbox():
|
| 114 |
return gr.update(value='', interactive=False), gr.update(interactive=False)
|
| 115 |
|
| 116 |
-
title = """<h1 align="center">
|
| 117 |
if DISABLED:
|
| 118 |
-
title = """<h1 align="center" style="color:red">This app has reached
|
|
|
|
| 119 |
description = """Language models can be conditioned to act like dialogue agents through a conversational prompt that typically takes the form:
|
| 120 |
```
|
| 121 |
User: <utterance>
|
|
@@ -124,56 +93,51 @@ User: <utterance>
|
|
| 124 |
Assistant: <utterance>
|
| 125 |
...
|
| 126 |
```
|
| 127 |
-
In this app, you can explore the outputs of
|
| 128 |
"""
|
| 129 |
|
| 130 |
-
theme = gr.themes.Default(primary_hue="
|
| 131 |
|
| 132 |
-
with gr.Blocks(css
|
| 133 |
#chatbot {height: 520px; overflow: auto;}""",
|
| 134 |
theme=theme) as demo:
|
| 135 |
gr.HTML(title)
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
chatbot = gr.Chatbot(elem_id='chatbot') #c
|
| 143 |
-
inputs = gr.Textbox(placeholder= "Hi there!", label= "Type an input and press Enter") #t
|
| 144 |
-
state = gr.State([]) #s
|
| 145 |
with gr.Row():
|
| 146 |
with gr.Column(scale=7):
|
| 147 |
-
b1 = gr.Button(visible=not DISABLED)
|
| 148 |
with gr.Column(scale=3):
|
| 149 |
-
server_status_code = gr.Textbox(label="Status code from
|
| 150 |
-
|
| 151 |
-
#inputs, top_p, temperature, top_k, repetition_penalty
|
| 152 |
with gr.Accordion("Parameters", open=False):
|
| 153 |
-
top_p = gr.Slider(
|
| 154 |
-
temperature = gr.Slider(
|
| 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
|
| 160 |
-
# Get user consent
|
| 161 |
accept_checkbox = gr.Checkbox(visible=False)
|
| 162 |
-
js = "(x) => confirm('
|
| 163 |
-
|
|
|
|
| 164 |
gr.HTML("""
|
| 165 |
<div>
|
| 166 |
-
<p>
|
| 167 |
<ol>
|
| 168 |
-
<li><strong>
|
| 169 |
-
<li><strong>
|
| 170 |
-
<li><strong>
|
| 171 |
-
<li><strong>
|
| 172 |
</ol>
|
| 173 |
-
<p>
|
|
|
|
| 174 |
</div>
|
| 175 |
""")
|
| 176 |
-
accept_button = gr.Button("I Agree")
|
| 177 |
|
| 178 |
def enable_inputs():
|
| 179 |
return gr.update(visible=False), gr.update(visible=True)
|
|
@@ -182,8 +146,8 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
|
|
| 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)
|
| 185 |
-
inputs.submit(predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code, inputs, b1]
|
| 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]
|
| 188 |
-
|
| 189 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
+
import json
|
|
|
|
| 5 |
import random
|
| 6 |
+
from openai import OpenAI
|
| 7 |
|
| 8 |
+
MODEL = "gpt-4.1-mini"
|
| 9 |
+
DISABLED = os.getenv("DISABLED", "False").lower() == 'true'
|
| 10 |
+
OPENAI_API_KEYS_RAW = os.getenv("OPENAI_API_KEYS", "")
|
| 11 |
+
OPENAI_API_KEYS = [key.strip() for key in OPENAI_API_KEYS_RAW.split(',') if key.strip()] if OPENAI_API_KEYS_RAW else []
|
| 12 |
+
NUM_THREADS = int(os.getenv("NUM_THREADS", "4"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def exception_handler(exception_type, exception, traceback):
|
| 15 |
print("%s: %s" % (exception_type.__name__, exception))
|
| 16 |
+
|
| 17 |
sys.excepthook = exception_handler
|
| 18 |
sys.tracebacklimit = 0
|
| 19 |
+
|
| 20 |
+
def predict(inputs, top_p, temperature, chat_counter, chatbot, history, request: gr.Request):
|
| 21 |
+
if not OPENAI_API_KEYS or not OPENAI_API_KEYS[0]:
|
| 22 |
+
yield [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)], history, chat_counter, "No API keys configured", gr.update(interactive=True), gr.update(interactive=True)
|
| 23 |
+
return
|
| 24 |
+
|
| 25 |
+
api_key = random.choice(OPENAI_API_KEYS)
|
| 26 |
+
client = OpenAI(api_key=api_key)
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
headers_dict = {key.decode('utf-8'): value.decode('utf-8') for key, value in request.headers.raw}
|
| 29 |
+
|
| 30 |
+
messages = []
|
| 31 |
+
if chat_counter != 0:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
for i, data in enumerate(history):
|
| 33 |
+
role = 'user' if i % 2 == 0 else 'assistant'
|
| 34 |
+
messages.append({"role": role, "content": data})
|
| 35 |
+
|
| 36 |
+
messages.append({"role": "user", "content": inputs})
|
| 37 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
chat_counter += 1
|
|
|
|
| 39 |
history.append(inputs)
|
| 40 |
+
token_counter = 0
|
| 41 |
+
partial_words = ""
|
| 42 |
+
|
|
|
|
| 43 |
try:
|
| 44 |
+
stream = client.chat.completions.create(
|
| 45 |
+
model=MODEL,
|
| 46 |
+
messages=messages,
|
| 47 |
+
temperature=temperature,
|
| 48 |
+
top_p=top_p,
|
| 49 |
+
stream=True,
|
| 50 |
+
presence_penalty=0,
|
| 51 |
+
frequency_penalty=0,
|
| 52 |
+
max_tokens=2048
|
| 53 |
+
)
|
| 54 |
|
| 55 |
+
for chunk in stream:
|
| 56 |
+
if chunk.choices[0].delta.content is not None:
|
| 57 |
+
partial_words += chunk.choices[0].delta.content
|
| 58 |
+
if token_counter == 0:
|
| 59 |
+
history.append(" " + partial_words)
|
| 60 |
+
else:
|
| 61 |
+
history[-1] = partial_words
|
| 62 |
+
token_counter += 1
|
| 63 |
+
yield [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)], history, chat_counter, "200", gr.update(interactive=False), gr.update(interactive=False)
|
| 64 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
except Exception as e:
|
| 66 |
+
print(f'error found: {e}')
|
| 67 |
+
yield [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)], history, chat_counter, str(e), gr.update(interactive=True), gr.update(interactive=True)
|
| 68 |
+
|
| 69 |
+
log_data = {
|
| 70 |
+
"chat_counter": chat_counter,
|
| 71 |
+
"model": MODEL,
|
| 72 |
+
"messages": messages,
|
| 73 |
+
"response": partial_words,
|
| 74 |
+
"headers": headers_dict,
|
| 75 |
+
"temperature": temperature,
|
| 76 |
+
"top_p": top_p,
|
| 77 |
+
"token_counter": token_counter
|
| 78 |
+
}
|
| 79 |
+
print(json.dumps(log_data))
|
| 80 |
|
| 81 |
def reset_textbox():
|
| 82 |
return gr.update(value='', interactive=False), gr.update(interactive=False)
|
| 83 |
|
| 84 |
+
title = """<h1 align="center">LatamGPT Data Collection: Research Preview</h1>"""
|
| 85 |
if DISABLED:
|
| 86 |
+
title = """<h1 align="center" style="color:red">This app has reached usage limit. Please check back tomorrow.</h1>"""
|
| 87 |
+
|
| 88 |
description = """Language models can be conditioned to act like dialogue agents through a conversational prompt that typically takes the form:
|
| 89 |
```
|
| 90 |
User: <utterance>
|
|
|
|
| 93 |
Assistant: <utterance>
|
| 94 |
...
|
| 95 |
```
|
| 96 |
+
In this app, you can explore the outputs of GPT-4.1 mini while contributing to LatamGPT research.
|
| 97 |
"""
|
| 98 |
|
| 99 |
+
theme = gr.themes.Default(primary_hue="blue")
|
| 100 |
|
| 101 |
+
with gr.Blocks(css="""#col_container { margin-left: auto; margin-right: auto;}
|
| 102 |
#chatbot {height: 520px; overflow: auto;}""",
|
| 103 |
theme=theme) as demo:
|
| 104 |
gr.HTML(title)
|
| 105 |
+
|
| 106 |
+
with gr.Column(elem_id="col_container", visible=False) as main_block:
|
| 107 |
+
chatbot = gr.Chatbot(elem_id='chatbot')
|
| 108 |
+
inputs = gr.Textbox(placeholder="¡Hola! ¿En qué puedo ayudarte?", label="Escribe tu mensaje y presiona Enter")
|
| 109 |
+
state = gr.State([])
|
| 110 |
+
|
|
|
|
|
|
|
|
|
|
| 111 |
with gr.Row():
|
| 112 |
with gr.Column(scale=7):
|
| 113 |
+
b1 = gr.Button(visible=not DISABLED)
|
| 114 |
with gr.Column(scale=3):
|
| 115 |
+
server_status_code = gr.Textbox(label="Status code from server")
|
| 116 |
+
|
|
|
|
| 117 |
with gr.Accordion("Parameters", open=False):
|
| 118 |
+
top_p = gr.Slider(minimum=0, maximum=1.0, value=1.0, step=0.05, interactive=True, label="Top-p (nucleus sampling)")
|
| 119 |
+
temperature = gr.Slider(minimum=0, maximum=2.0, value=0.7, step=0.1, interactive=True, label="Temperature")
|
|
|
|
|
|
|
| 120 |
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
| 121 |
|
| 122 |
+
with gr.Column(elem_id="user_consent_container") as user_consent_block:
|
|
|
|
| 123 |
accept_checkbox = gr.Checkbox(visible=False)
|
| 124 |
+
js = "(x) => confirm('Al hacer clic en \"Acepto\", acepto que mis datos pueden ser publicados o compartidos para investigación.')"
|
| 125 |
+
|
| 126 |
+
with gr.Accordion("Consentimiento de Usuario para Recolección, Uso y Compartición de Datos", open=True):
|
| 127 |
gr.HTML("""
|
| 128 |
<div>
|
| 129 |
+
<p>Al usar nuestra aplicación, que funciona con la API de OpenAI, reconoces y aceptas los siguientes términos sobre los datos que proporcionas:</p>
|
| 130 |
<ol>
|
| 131 |
+
<li><strong>Recolección:</strong> Podemos recopilar información, incluyendo las entradas que escribes en nuestra aplicación, las salidas generadas por la API de OpenAI, y ciertos detalles técnicos sobre tu dispositivo y conexión (como tipo de navegador, sistema operativo e dirección IP) proporcionados por los headers de solicitud de tu dispositivo.</li>
|
| 132 |
+
<li><strong>Uso:</strong> Podemos usar los datos recopilados para propósitos de investigación, para mejorar nuestros servicios, y para desarrollar nuevos productos o servicios, incluyendo aplicaciones comerciales, y para propósitos de seguridad, como proteger contra acceso no autorizado y ataques.</li>
|
| 133 |
+
<li><strong>Compartición y Publicación:</strong> Tus datos, incluyendo los detalles técnicos recopilados de los headers de solicitud de tu dispositivo, pueden ser publicados, compartidos con terceros, o usados para análisis y propósitos de reportes.</li>
|
| 134 |
+
<li><strong>Retención de Datos:</strong> Podemos retener tus datos, incluyendo los detalles técnicos recopilados de los headers de solicitud de tu dispositivo, por el tiempo que sea necesario.</li>
|
| 135 |
</ol>
|
| 136 |
+
<p>Al continuar usando nuestra aplicación, proporcionas tu consentimiento explícito para la recolección, uso y potencial compartición de tus datos como se describe arriba. Si no estás de acuerdo con nuestras prácticas de recolección, uso y compartición de datos, por favor no uses nuestra aplicación.</p>
|
| 137 |
+
<p><strong>Este proyecto contribuye al desarrollo de LatamGPT, un modelo de lenguaje para América Latina.</strong></p>
|
| 138 |
</div>
|
| 139 |
""")
|
| 140 |
+
accept_button = gr.Button("Acepto / I Agree")
|
| 141 |
|
| 142 |
def enable_inputs():
|
| 143 |
return gr.update(visible=False), gr.update(visible=True)
|
|
|
|
| 146 |
accept_checkbox.change(fn=enable_inputs, inputs=[], outputs=[user_consent_block, main_block], queue=False)
|
| 147 |
|
| 148 |
inputs.submit(reset_textbox, [], [inputs, b1], queue=False)
|
| 149 |
+
inputs.submit(predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code, inputs, b1])
|
| 150 |
b1.click(reset_textbox, [], [inputs, b1], queue=False)
|
| 151 |
+
b1.click(predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code, inputs, b1])
|
| 152 |
+
|
| 153 |
+
demo.queue(max_size=20, default_concurrency_limit=NUM_THREADS, api_open=False).launch()
|