Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -88,8 +88,34 @@ def _check_and_count_usage(ip: str) -> tuple[bool, int]:
|
|
| 88 |
return True, current_count + 1
|
| 89 |
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
def _format_prompt(message: str, history) -> str:
|
| 92 |
-
clean_message =
|
| 93 |
recent_history = history[-MAX_HISTORY_MESSAGES:] if history else []
|
| 94 |
|
| 95 |
prompt_parts = [f"System: {SYSTEM_PROMPT}\n"]
|
|
@@ -97,7 +123,7 @@ def _format_prompt(message: str, history) -> str:
|
|
| 97 |
for item in recent_history:
|
| 98 |
if isinstance(item, dict):
|
| 99 |
role = item.get("role", "")
|
| 100 |
-
content = (item.get("content"
|
| 101 |
|
| 102 |
if role == "user" and content:
|
| 103 |
prompt_parts.append(f"User: {content}")
|
|
@@ -105,8 +131,8 @@ def _format_prompt(message: str, history) -> str:
|
|
| 105 |
prompt_parts.append(f"Assistant: {content}")
|
| 106 |
|
| 107 |
elif isinstance(item, (list, tuple)) and len(item) >= 2:
|
| 108 |
-
user_msg =
|
| 109 |
-
bot_msg =
|
| 110 |
|
| 111 |
if user_msg:
|
| 112 |
prompt_parts.append(f"User: {user_msg}")
|
|
|
|
| 88 |
return True, current_count + 1
|
| 89 |
|
| 90 |
|
| 91 |
+
def _safe_text(value) -> str:
|
| 92 |
+
"""Convert Gradio message content into safe plain text."""
|
| 93 |
+
if value is None:
|
| 94 |
+
return ""
|
| 95 |
+
|
| 96 |
+
if isinstance(value, str):
|
| 97 |
+
return value.strip()
|
| 98 |
+
|
| 99 |
+
if isinstance(value, dict):
|
| 100 |
+
# Some Gradio versions store content inside nested dicts.
|
| 101 |
+
content = value.get("content", "")
|
| 102 |
+
if isinstance(content, str):
|
| 103 |
+
return content.strip()
|
| 104 |
+
return str(content).strip()
|
| 105 |
+
|
| 106 |
+
if isinstance(value, (list, tuple)):
|
| 107 |
+
parts = []
|
| 108 |
+
for item in value:
|
| 109 |
+
text = _safe_text(item)
|
| 110 |
+
if text:
|
| 111 |
+
parts.append(text)
|
| 112 |
+
return " ".join(parts).strip()
|
| 113 |
+
|
| 114 |
+
return str(value).strip()
|
| 115 |
+
|
| 116 |
+
|
| 117 |
def _format_prompt(message: str, history) -> str:
|
| 118 |
+
clean_message = _safe_text(message)[:MAX_INPUT_CHARS]
|
| 119 |
recent_history = history[-MAX_HISTORY_MESSAGES:] if history else []
|
| 120 |
|
| 121 |
prompt_parts = [f"System: {SYSTEM_PROMPT}\n"]
|
|
|
|
| 123 |
for item in recent_history:
|
| 124 |
if isinstance(item, dict):
|
| 125 |
role = item.get("role", "")
|
| 126 |
+
content = _safe_text(item.get("content", ""))
|
| 127 |
|
| 128 |
if role == "user" and content:
|
| 129 |
prompt_parts.append(f"User: {content}")
|
|
|
|
| 131 |
prompt_parts.append(f"Assistant: {content}")
|
| 132 |
|
| 133 |
elif isinstance(item, (list, tuple)) and len(item) >= 2:
|
| 134 |
+
user_msg = _safe_text(item[0])
|
| 135 |
+
bot_msg = _safe_text(item[1])
|
| 136 |
|
| 137 |
if user_msg:
|
| 138 |
prompt_parts.append(f"User: {user_msg}")
|