Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import plotly.express as px
|
| 3 |
from openai import OpenAI
|
| 4 |
import time
|
| 5 |
import os
|
| 6 |
from dotenv import load_dotenv
|
|
|
|
| 7 |
|
| 8 |
load_dotenv()
|
| 9 |
api_key = os.getenv("OPENAI_API_KEY")
|
|
@@ -15,15 +15,6 @@ print("Cliente OpenAI inicializado")
|
|
| 15 |
|
| 16 |
assistant_id = "asst_0hq3iRy6LX0YLZP0QVzg17fT"
|
| 17 |
|
| 18 |
-
def random_plot():
|
| 19 |
-
df = px.data.iris()
|
| 20 |
-
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species",
|
| 21 |
-
size='petal_length', hover_data=['petal_width'])
|
| 22 |
-
return fig
|
| 23 |
-
|
| 24 |
-
def print_like_dislike(x: gr.LikeData):
|
| 25 |
-
print(x.index, x.value, x.liked)
|
| 26 |
-
|
| 27 |
def add_message(history, message):
|
| 28 |
if message.strip() != "":
|
| 29 |
history.append((message, None))
|
|
@@ -65,8 +56,30 @@ def bot(history):
|
|
| 65 |
|
| 66 |
if run.status == "requires_action":
|
| 67 |
print("La ejecuci贸n requiere una acci贸n")
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
if run.status != "completed":
|
| 72 |
print(f"La ejecuci贸n termin贸 con estado: {run.status}")
|
|
@@ -93,8 +106,6 @@ def bot(history):
|
|
| 93 |
print(f"Error en la funci贸n bot: {e}")
|
| 94 |
return history + [("Lo siento, ocurri贸 un error inesperado. Por favor, intenta de nuevo.", None)]
|
| 95 |
|
| 96 |
-
fig = random_plot()
|
| 97 |
-
|
| 98 |
with gr.Blocks(fill_height=True) as demo:
|
| 99 |
chatbot = gr.Chatbot(
|
| 100 |
elem_id="chatbot",
|
|
@@ -109,7 +120,6 @@ with gr.Blocks(fill_height=True) as demo:
|
|
| 109 |
chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
|
| 110 |
bot_msg = chat_msg.then(bot, chatbot, chatbot, api_name="bot_response")
|
| 111 |
bot_msg.then(lambda: gr.Textbox(interactive=True), None, [chat_input])
|
| 112 |
-
chatbot.like(print_like_dislike, None, None)
|
| 113 |
|
| 114 |
print("Iniciando la aplicaci贸n Gradio")
|
| 115 |
demo.queue()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from openai import OpenAI
|
| 3 |
import time
|
| 4 |
import os
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
+
import json
|
| 7 |
|
| 8 |
load_dotenv()
|
| 9 |
api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 15 |
|
| 16 |
assistant_id = "asst_0hq3iRy6LX0YLZP0QVzg17fT"
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def add_message(history, message):
|
| 19 |
if message.strip() != "":
|
| 20 |
history.append((message, None))
|
|
|
|
| 56 |
|
| 57 |
if run.status == "requires_action":
|
| 58 |
print("La ejecuci贸n requiere una acci贸n")
|
| 59 |
+
required_actions = run.required_action.submit_tool_outputs.tool_calls
|
| 60 |
+
tool_outputs = []
|
| 61 |
+
|
| 62 |
+
for action in required_actions:
|
| 63 |
+
function_name = action.function.name
|
| 64 |
+
function_args = json.loads(action.function.arguments)
|
| 65 |
+
|
| 66 |
+
# Aqu铆 simplemente pasamos la llamada a la funci贸n al asistente
|
| 67 |
+
result = json.dumps({
|
| 68 |
+
"function_called": function_name,
|
| 69 |
+
"arguments": function_args
|
| 70 |
+
})
|
| 71 |
+
|
| 72 |
+
tool_outputs.append({
|
| 73 |
+
"tool_call_id": action.id,
|
| 74 |
+
"output": result
|
| 75 |
+
})
|
| 76 |
+
|
| 77 |
+
client.beta.threads.runs.submit_tool_outputs(
|
| 78 |
+
thread_id=thread.id,
|
| 79 |
+
run_id=run.id,
|
| 80 |
+
tool_outputs=tool_outputs
|
| 81 |
+
)
|
| 82 |
+
continue
|
| 83 |
|
| 84 |
if run.status != "completed":
|
| 85 |
print(f"La ejecuci贸n termin贸 con estado: {run.status}")
|
|
|
|
| 106 |
print(f"Error en la funci贸n bot: {e}")
|
| 107 |
return history + [("Lo siento, ocurri贸 un error inesperado. Por favor, intenta de nuevo.", None)]
|
| 108 |
|
|
|
|
|
|
|
| 109 |
with gr.Blocks(fill_height=True) as demo:
|
| 110 |
chatbot = gr.Chatbot(
|
| 111 |
elem_id="chatbot",
|
|
|
|
| 120 |
chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
|
| 121 |
bot_msg = chat_msg.then(bot, chatbot, chatbot, api_name="bot_response")
|
| 122 |
bot_msg.then(lambda: gr.Textbox(interactive=True), None, [chat_input])
|
|
|
|
| 123 |
|
| 124 |
print("Iniciando la aplicaci贸n Gradio")
|
| 125 |
demo.queue()
|