Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,58 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import time
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
from langfuse.
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
chatbot.like(print_like_dislike, None, None, like_user_message=True)
|
| 60 |
-
demo.launch(share=False, debug=True, show_error=True, pwa=True)
|
| 61 |
-
|
| 62 |
-
if __name__ == "__main__":
|
| 63 |
-
main()
|
| 64 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import requests
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from langfuse.openai import OpenAI
|
| 6 |
+
from langfuse.decorators import langfuse_context, observe
|
| 7 |
+
|
| 8 |
+
MODELOS_DE_OPENROUTER = "https://openrouter.ai/api/v1/models"
|
| 9 |
+
URL="https://openrouter.ai/api/v1"
|
| 10 |
+
REQUEST_SUCESSFULL = 200
|
| 11 |
+
|
| 12 |
+
def obtener_modelos():
|
| 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 |
+
|
| 17 |
+
def obtener_respuesta(modelo, api_key, prompt):
|
| 18 |
+
client = OpenAI(base_url=URL, api_key=api_key)
|
| 19 |
+
mensajes = [{"role": "assistant", "content": prompt}]
|
| 20 |
+
respuesta = client.chat.completions.create(model=modelo, messages=mensajes)
|
| 21 |
+
return respuesta.choices[0].message.content
|
| 22 |
+
|
| 23 |
+
def print_like_dislike(x: gr.LikeData):
|
| 24 |
+
print(x.index, x.value, x.liked)
|
| 25 |
+
|
| 26 |
+
def add_message(history, message):
|
| 27 |
+
for x in message["files"]:
|
| 28 |
+
history.append({"role": "user", "content": {"path": x}})
|
| 29 |
+
if message["text"] is not None:
|
| 30 |
+
history.append({"role": "user", "content": message["text"]})
|
| 31 |
+
return history, gr.MultimodalTextbox(value=None, interactive=False)
|
| 32 |
+
|
| 33 |
+
def bot(history, modelo, api_key):
|
| 34 |
+
prompt = history[-1]["content"] if isinstance(history[-1]["content"], str) else "Archivo enviado."
|
| 35 |
+
response = obtener_respuesta(modelo, api_key, prompt)
|
| 36 |
+
history.append({"role": "assistant", "content": ""})
|
| 37 |
+
for character in response:
|
| 38 |
+
history[-1]["content"] += character
|
| 39 |
+
time.sleep(0.05)
|
| 40 |
+
yield history
|
| 41 |
+
|
| 42 |
+
def main():
|
| 43 |
+
with gr.Blocks() as demo:
|
| 44 |
+
with gr.Tab("ejecucion"):
|
| 45 |
+
chatbot = gr.Chatbot(elem_id="chatbot", type="messages")
|
| 46 |
+
chat_input = gr.MultimodalTextbox(interactive=True, file_count="multiple", placeholder="Enter message or upload file...", show_label=False,sources=["microphone", "upload"])
|
| 47 |
+
with gr.Tab("configuracion"):
|
| 48 |
+
modelo = gr.Dropdown(label="seleccionar modelo", choices=obtener_modelos())
|
| 49 |
+
api_key = gr.Textbox(label="API-KEY")
|
| 50 |
+
chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
|
| 51 |
+
bot_msg = chat_msg.then(bot, [chatbot, modelo, api_key], chatbot, api_name="bot_response")
|
| 52 |
+
bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
|
| 53 |
+
chatbot.like(print_like_dislike, None, None, like_user_message=True)
|
| 54 |
+
demo.launch(share=False, debug=True, show_error=True, pwa=True)
|
| 55 |
+
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
main()
|
| 58 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|