Spaces:
Sleeping
Sleeping
Commit
Β·
d81040e
1
Parent(s):
f8b946e
updatation
Browse files
app.py
CHANGED
|
@@ -103,43 +103,6 @@ def export_chat_files(history: List[Dict[str, Any]]) -> Dict[str, Optional[str]]
|
|
| 103 |
# Core chat function
|
| 104 |
# ----------------------
|
| 105 |
|
| 106 |
-
def generate_reply(user_msg: str, history: Optional[List[Dict[str, Any]]]):
|
| 107 |
-
if history is None:
|
| 108 |
-
history = []
|
| 109 |
-
|
| 110 |
-
if not user_msg.strip():
|
| 111 |
-
return history
|
| 112 |
-
|
| 113 |
-
# Detect intent for system prompt if needed
|
| 114 |
-
intent = None
|
| 115 |
-
low = user_msg.lower()
|
| 116 |
-
for key in INTENT_TEMPLATES:
|
| 117 |
-
if low.startswith(key):
|
| 118 |
-
intent = key
|
| 119 |
-
user_msg = user_msg[len(key):].strip()
|
| 120 |
-
break
|
| 121 |
-
|
| 122 |
-
# Use intent in bot.ask (optional, can pass intent)
|
| 123 |
-
system_prefix = INTENT_TEMPLATES.get(intent, "Answer briefly in 2β3 sentences.")
|
| 124 |
-
prompt = f"{system_prefix}\nUser: {user_msg}"
|
| 125 |
-
bot_reply = bot.ask(user_msg, intent=intent)
|
| 126 |
-
|
| 127 |
-
# Timestamp only for UI display
|
| 128 |
-
ts = now_ts()
|
| 129 |
-
bot_reply_ui = f"{bot_reply}\n\nπ {ts}"
|
| 130 |
-
|
| 131 |
-
# Append **clean text** to history
|
| 132 |
-
history.append({"role": "user", "content": str(user_msg)})
|
| 133 |
-
history.append({"role": "assistant", "content": str(bot_reply)})
|
| 134 |
-
|
| 135 |
-
try:
|
| 136 |
-
memory.add(user_msg, bot_reply)
|
| 137 |
-
except:
|
| 138 |
-
pass
|
| 139 |
-
|
| 140 |
-
return history
|
| 141 |
-
|
| 142 |
-
|
| 143 |
# def generate_reply(user_msg: str, history: Optional[List[Dict[str, Any]]]):
|
| 144 |
# if history is None:
|
| 145 |
# history = []
|
|
@@ -147,7 +110,7 @@ def generate_reply(user_msg: str, history: Optional[List[Dict[str, Any]]]):
|
|
| 147 |
# if not user_msg.strip():
|
| 148 |
# return history
|
| 149 |
|
| 150 |
-
# # Detect intent
|
| 151 |
# intent = None
|
| 152 |
# low = user_msg.lower()
|
| 153 |
# for key in INTENT_TEMPLATES:
|
|
@@ -156,13 +119,18 @@ def generate_reply(user_msg: str, history: Optional[List[Dict[str, Any]]]):
|
|
| 156 |
# user_msg = user_msg[len(key):].strip()
|
| 157 |
# break
|
| 158 |
|
| 159 |
-
# #
|
|
|
|
|
|
|
| 160 |
# bot_reply = bot.ask(user_msg, intent=intent)
|
|
|
|
|
|
|
| 161 |
# ts = now_ts()
|
| 162 |
-
#
|
| 163 |
|
|
|
|
| 164 |
# history.append({"role": "user", "content": str(user_msg)})
|
| 165 |
-
# history.append({"role": "assistant", "content": str(
|
| 166 |
|
| 167 |
# try:
|
| 168 |
# memory.add(user_msg, bot_reply)
|
|
@@ -172,6 +140,38 @@ def generate_reply(user_msg: str, history: Optional[List[Dict[str, Any]]]):
|
|
| 172 |
# return history
|
| 173 |
|
| 174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
# ----------------------
|
| 176 |
# UI / Gradio
|
| 177 |
# ----------------------
|
|
|
|
| 103 |
# Core chat function
|
| 104 |
# ----------------------
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
# def generate_reply(user_msg: str, history: Optional[List[Dict[str, Any]]]):
|
| 107 |
# if history is None:
|
| 108 |
# history = []
|
|
|
|
| 110 |
# if not user_msg.strip():
|
| 111 |
# return history
|
| 112 |
|
| 113 |
+
# # Detect intent for system prompt if needed
|
| 114 |
# intent = None
|
| 115 |
# low = user_msg.lower()
|
| 116 |
# for key in INTENT_TEMPLATES:
|
|
|
|
| 119 |
# user_msg = user_msg[len(key):].strip()
|
| 120 |
# break
|
| 121 |
|
| 122 |
+
# # Use intent in bot.ask (optional, can pass intent)
|
| 123 |
+
# system_prefix = INTENT_TEMPLATES.get(intent, "Answer briefly in 2β3 sentences.")
|
| 124 |
+
# prompt = f"{system_prefix}\nUser: {user_msg}"
|
| 125 |
# bot_reply = bot.ask(user_msg, intent=intent)
|
| 126 |
+
|
| 127 |
+
# # Timestamp only for UI display
|
| 128 |
# ts = now_ts()
|
| 129 |
+
# bot_reply_ui = f"{bot_reply}\n\nπ {ts}"
|
| 130 |
|
| 131 |
+
# # Append **clean text** to history
|
| 132 |
# history.append({"role": "user", "content": str(user_msg)})
|
| 133 |
+
# history.append({"role": "assistant", "content": str(bot_reply)})
|
| 134 |
|
| 135 |
# try:
|
| 136 |
# memory.add(user_msg, bot_reply)
|
|
|
|
| 140 |
# return history
|
| 141 |
|
| 142 |
|
| 143 |
+
def generate_reply(user_msg: str, history: Optional[List[Dict[str, Any]]]):
|
| 144 |
+
if history is None:
|
| 145 |
+
history = []
|
| 146 |
+
|
| 147 |
+
if not user_msg.strip():
|
| 148 |
+
return history
|
| 149 |
+
|
| 150 |
+
# Detect intent prefix from templates
|
| 151 |
+
intent = None
|
| 152 |
+
low = user_msg.lower()
|
| 153 |
+
for key in INTENT_TEMPLATES:
|
| 154 |
+
if low.startswith(key):
|
| 155 |
+
intent = key
|
| 156 |
+
user_msg = user_msg[len(key):].strip()
|
| 157 |
+
break
|
| 158 |
+
|
| 159 |
+
# Ask chatbot (pass intent)
|
| 160 |
+
bot_reply = bot.ask(user_msg, intent=intent)
|
| 161 |
+
ts = now_ts()
|
| 162 |
+
bot_reply_ts = f"{bot_reply}\n\nπ {ts}"
|
| 163 |
+
|
| 164 |
+
history.append({"role": "user", "content": str(user_msg)})
|
| 165 |
+
history.append({"role": "assistant", "content": str(bot_reply_ts)})
|
| 166 |
+
|
| 167 |
+
try:
|
| 168 |
+
memory.add(user_msg, bot_reply)
|
| 169 |
+
except:
|
| 170 |
+
pass
|
| 171 |
+
|
| 172 |
+
return history
|
| 173 |
+
|
| 174 |
+
|
| 175 |
# ----------------------
|
| 176 |
# UI / Gradio
|
| 177 |
# ----------------------
|