ricitos2001 commited on
Commit
4f18eea
·
verified ·
1 Parent(s): 8ab2408

Update app.py

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