Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import time
|
| 2 |
-
import
|
|
|
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from openai import OpenAI
|
| 5 |
|
|
@@ -11,10 +13,12 @@ def obtener_modelos():
|
|
| 11 |
respuesta = requests.get(MODELOS_DE_OPENROUTER)
|
| 12 |
if respuesta.status_code == REQUEST_SUCESSFULL:
|
| 13 |
return [modelo["id"] for modelo in respuesta.json().get("data", [])]
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def obtener_respuesta(modelo, api_key, prompt):
|
| 16 |
client = OpenAI(base_url=URL, api_key=api_key)
|
| 17 |
-
mensajes = [
|
| 18 |
respuesta = client.chat.completions.create(model=modelo, messages=mensajes)
|
| 19 |
return respuesta.choices[0].message.content
|
| 20 |
|
|
@@ -28,9 +32,9 @@ def add_message(history, message):
|
|
| 28 |
history.append({"role": "user", "content": message["text"]})
|
| 29 |
return history, gr.MultimodalTextbox(value=None, interactive=False)
|
| 30 |
|
| 31 |
-
def bot(history, modelo, api_key):
|
| 32 |
prompt = history[-1]["content"] if isinstance(history[-1]["content"], str) else "Archivo enviado."
|
| 33 |
-
response = obtener_respuesta(modelo, api_key, prompt)
|
| 34 |
history.append({"role": "assistant", "content": ""})
|
| 35 |
for character in response:
|
| 36 |
history[-1]["content"] += character
|
|
@@ -39,14 +43,15 @@ def bot(history, modelo, api_key):
|
|
| 39 |
|
| 40 |
def main():
|
| 41 |
with gr.Blocks() as demo:
|
| 42 |
-
with gr.Tab("
|
| 43 |
chatbot = gr.Chatbot(elem_id="chatbot", type="messages")
|
| 44 |
chat_input = gr.MultimodalTextbox(interactive=True, file_count="multiple", placeholder="Enter message or upload file...", show_label=False)
|
| 45 |
-
with gr.Tab("
|
| 46 |
modelo = gr.Dropdown(label="seleccionar modelo", choices=obtener_modelos())
|
| 47 |
api_key = gr.Textbox(label="API-KEY")
|
|
|
|
| 48 |
chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
|
| 49 |
-
bot_msg = chat_msg.then(bot, [chatbot, modelo, api_key], chatbot, api_name="bot_response")
|
| 50 |
bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
|
| 51 |
chatbot.like(print_like_dislike, None, None, like_user_message=True)
|
| 52 |
demo.launch(share=False, debug=True, show_error=True)
|
|
|
|
| 1 |
import time
|
| 2 |
+
from typing import Iterable
|
| 3 |
+
|
| 4 |
+
import requests
|
| 5 |
import gradio as gr
|
| 6 |
from openai import OpenAI
|
| 7 |
|
|
|
|
| 13 |
respuesta = requests.get(MODELOS_DE_OPENROUTER)
|
| 14 |
if respuesta.status_code == REQUEST_SUCESSFULL:
|
| 15 |
return [modelo["id"] for modelo in respuesta.json().get("data", [])]
|
| 16 |
+
else:
|
| 17 |
+
raise Exception(respuesta.status_code)
|
| 18 |
|
| 19 |
def obtener_respuesta(modelo, api_key, prompt):
|
| 20 |
client = OpenAI(base_url=URL, api_key=api_key)
|
| 21 |
+
mensajes = Iterable["role": "assistant", "content": prompt]
|
| 22 |
respuesta = client.chat.completions.create(model=modelo, messages=mensajes)
|
| 23 |
return respuesta.choices[0].message.content
|
| 24 |
|
|
|
|
| 32 |
history.append({"role": "user", "content": message["text"]})
|
| 33 |
return history, gr.MultimodalTextbox(value=None, interactive=False)
|
| 34 |
|
| 35 |
+
def bot(system_prompt, history, modelo, api_key):
|
| 36 |
prompt = history[-1]["content"] if isinstance(history[-1]["content"], str) else "Archivo enviado."
|
| 37 |
+
response = obtener_respuesta(modelo, api_key, (system_prompt+prompt))
|
| 38 |
history.append({"role": "assistant", "content": ""})
|
| 39 |
for character in response:
|
| 40 |
history[-1]["content"] += character
|
|
|
|
| 43 |
|
| 44 |
def main():
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
+
with gr.Tab("ejecución"):
|
| 47 |
chatbot = gr.Chatbot(elem_id="chatbot", type="messages")
|
| 48 |
chat_input = gr.MultimodalTextbox(interactive=True, file_count="multiple", placeholder="Enter message or upload file...", show_label=False)
|
| 49 |
+
with gr.Tab("configuración"):
|
| 50 |
modelo = gr.Dropdown(label="seleccionar modelo", choices=obtener_modelos())
|
| 51 |
api_key = gr.Textbox(label="API-KEY")
|
| 52 |
+
system_prompt = gr.TextArea(label="system-prompt")
|
| 53 |
chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
|
| 54 |
+
bot_msg = chat_msg.then(bot, [system_prompt, chatbot, modelo, api_key], chatbot, api_name="bot_response")
|
| 55 |
bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
|
| 56 |
chatbot.like(print_like_dislike, None, None, like_user_message=True)
|
| 57 |
demo.launch(share=False, debug=True, show_error=True)
|