Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -58,26 +58,38 @@ def retrieve_relevant_context(user_query):
|
|
| 58 |
|
| 59 |
# 4. Agentic RAG Controller Logic (Compatible with Gradio 5.x / 6.0+)
|
| 60 |
def chat_respond(user_message, chat_history):
|
| 61 |
-
# 1.
|
| 62 |
api_messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
|
|
|
| 63 |
for msg in chat_history:
|
| 64 |
-
if
|
| 65 |
-
|
| 66 |
-
else:
|
| 67 |
api_messages.append({"role": msg.role, "content": msg.content})
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
#
|
| 73 |
-
chat_history.append({"role": "user", "content":
|
| 74 |
chat_history.append({"role": "assistant", "content": ""})
|
| 75 |
|
| 76 |
-
# Clear the input box and
|
| 77 |
yield "", chat_history
|
| 78 |
|
| 79 |
try:
|
| 80 |
-
#
|
| 81 |
stream_response = client.chat_completion(
|
| 82 |
messages=api_messages,
|
| 83 |
max_tokens=1024,
|
|
@@ -86,20 +98,29 @@ def chat_respond(user_message, chat_history):
|
|
| 86 |
|
| 87 |
running_text = ""
|
| 88 |
for token_chunk in stream_response:
|
| 89 |
-
|
| 90 |
-
|
| 91 |
running_text += token
|
| 92 |
-
|
|
|
|
| 93 |
if isinstance(chat_history[-1], dict):
|
| 94 |
chat_history[-1]["content"] = running_text
|
| 95 |
else:
|
| 96 |
chat_history[-1].content = running_text
|
|
|
|
| 97 |
yield "", chat_history
|
| 98 |
|
| 99 |
except Exception as e:
|
| 100 |
-
print(f"Error during streaming: {e}")
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
# 5. UI Layout Styling Engine
|
| 104 |
custom_css = """
|
| 105 |
body, .gradio-container {
|
|
|
|
| 58 |
|
| 59 |
# 4. Agentic RAG Controller Logic (Compatible with Gradio 5.x / 6.0+)
|
| 60 |
def chat_respond(user_message, chat_history):
|
| 61 |
+
# 1. FORCE CONVERT the history to standard Python dictionaries for the Hugging Face API
|
| 62 |
api_messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 63 |
+
|
| 64 |
for msg in chat_history:
|
| 65 |
+
if hasattr(msg, "role") and hasattr(msg, "content"):
|
| 66 |
+
# If it's a Gradio 6 ChatMessage object, extract properties directly
|
|
|
|
| 67 |
api_messages.append({"role": msg.role, "content": msg.content})
|
| 68 |
+
elif isinstance(msg, dict):
|
| 69 |
+
# If it's already a dictionary
|
| 70 |
+
api_messages.append({"role": msg.get("role", "user"), "content": msg.get("content", "")})
|
| 71 |
+
elif isinstance(msg, (list, tuple)) and len(msg) >= 2:
|
| 72 |
+
# Fallback wrapper for old-style tuples
|
| 73 |
+
api_messages.append({"role": msg[0], "content": msg[1]})
|
| 74 |
+
|
| 75 |
+
# Handle case where user_message comes from a MultimodalTextbox component
|
| 76 |
+
if isinstance(user_message, dict) and "text" in user_message:
|
| 77 |
+
user_text = user_message["text"]
|
| 78 |
+
else:
|
| 79 |
+
user_text = str(user_message)
|
| 80 |
+
|
| 81 |
+
# Append current user message to the API payload
|
| 82 |
+
api_messages.append({"role": "user", "content": user_text})
|
| 83 |
|
| 84 |
+
# 2. Append to chat_history to display it instantly in the Gradio UI
|
| 85 |
+
chat_history.append({"role": "user", "content": user_text})
|
| 86 |
chat_history.append({"role": "assistant", "content": ""})
|
| 87 |
|
| 88 |
+
# Clear the input box and show the empty assistant block layout immediately
|
| 89 |
yield "", chat_history
|
| 90 |
|
| 91 |
try:
|
| 92 |
+
# 3. Request completion using the strictly cleaned api_messages list
|
| 93 |
stream_response = client.chat_completion(
|
| 94 |
messages=api_messages,
|
| 95 |
max_tokens=1024,
|
|
|
|
| 98 |
|
| 99 |
running_text = ""
|
| 100 |
for token_chunk in stream_response:
|
| 101 |
+
if token_chunk.choices and token_chunk.choices[0].delta.content:
|
| 102 |
+
token = token_chunk.choices[0].delta.content
|
| 103 |
running_text += token
|
| 104 |
+
|
| 105 |
+
# Safely update the final item in history depending on its type state
|
| 106 |
if isinstance(chat_history[-1], dict):
|
| 107 |
chat_history[-1]["content"] = running_text
|
| 108 |
else:
|
| 109 |
chat_history[-1].content = running_text
|
| 110 |
+
|
| 111 |
yield "", chat_history
|
| 112 |
|
| 113 |
except Exception as e:
|
| 114 |
+
print(f"Error encountered during streaming: {e}")
|
| 115 |
+
# Prevent UI from freezing by displaying the error message directly in the chat window
|
| 116 |
+
error_display = f"An error occurred: {str(e)}"
|
| 117 |
+
if isinstance(chat_history[-1], dict):
|
| 118 |
+
chat_history[-1]["content"] = error_display
|
| 119 |
+
else:
|
| 120 |
+
chat_history[-1].content = error_display
|
| 121 |
+
yield "", chat_history
|
| 122 |
+
|
| 123 |
+
|
| 124 |
# 5. UI Layout Styling Engine
|
| 125 |
custom_css = """
|
| 126 |
body, .gradio-container {
|