Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,10 @@ import gradio as gr
|
|
| 2 |
from openai import OpenAI
|
| 3 |
|
| 4 |
def respond(message, chat_history, api_key):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# Gatekeeper: Halt execution if no compute resource is provided
|
| 6 |
if not api_key:
|
| 7 |
chat_history.append({"role": "user", "content": message})
|
|
@@ -19,7 +23,7 @@ def respond(message, chat_history, api_key):
|
|
| 19 |
}
|
| 20 |
]
|
| 21 |
|
| 22 |
-
#
|
| 23 |
openai_messages.extend(chat_history)
|
| 24 |
openai_messages.append({"role": "user", "content": message})
|
| 25 |
|
|
@@ -32,7 +36,7 @@ def respond(message, chat_history, api_key):
|
|
| 32 |
|
| 33 |
bot_reply = response.choices[0].message.content
|
| 34 |
|
| 35 |
-
# Append the new interaction using the
|
| 36 |
chat_history.append({"role": "user", "content": message})
|
| 37 |
chat_history.append({"role": "assistant", "content": bot_reply})
|
| 38 |
|
|
@@ -71,8 +75,8 @@ with gr.Blocks() as demo:
|
|
| 71 |
info="Injected dynamically at runtime. Keys are not stored or logged."
|
| 72 |
)
|
| 73 |
|
| 74 |
-
# 2. Main Chat Display -
|
| 75 |
-
chatbot = gr.Chatbot(height=450, label="Intelligence Feed"
|
| 76 |
|
| 77 |
# 3. User Input Area
|
| 78 |
with gr.Row():
|
|
|
|
| 2 |
from openai import OpenAI
|
| 3 |
|
| 4 |
def respond(message, chat_history, api_key):
|
| 5 |
+
# Failsafe to ensure history initializes as an empty list
|
| 6 |
+
if chat_history is None:
|
| 7 |
+
chat_history = []
|
| 8 |
+
|
| 9 |
# Gatekeeper: Halt execution if no compute resource is provided
|
| 10 |
if not api_key:
|
| 11 |
chat_history.append({"role": "user", "content": message})
|
|
|
|
| 23 |
}
|
| 24 |
]
|
| 25 |
|
| 26 |
+
# Pass the direct Gradio history into the OpenAI pipeline
|
| 27 |
openai_messages.extend(chat_history)
|
| 28 |
openai_messages.append({"role": "user", "content": message})
|
| 29 |
|
|
|
|
| 36 |
|
| 37 |
bot_reply = response.choices[0].message.content
|
| 38 |
|
| 39 |
+
# Append the new interaction using the native dictionary format
|
| 40 |
chat_history.append({"role": "user", "content": message})
|
| 41 |
chat_history.append({"role": "assistant", "content": bot_reply})
|
| 42 |
|
|
|
|
| 75 |
info="Injected dynamically at runtime. Keys are not stored or logged."
|
| 76 |
)
|
| 77 |
|
| 78 |
+
# 2. Main Chat Display - Removed the 'type' parameter as it is now strictly implicit
|
| 79 |
+
chatbot = gr.Chatbot(height=450, label="Intelligence Feed")
|
| 80 |
|
| 81 |
# 3. User Input Area
|
| 82 |
with gr.Row():
|