Spaces:
Sleeping
Sleeping
Rajan Sharma
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -76,10 +76,22 @@ def is_identity_query(message, history):
|
|
| 76 |
return True
|
| 77 |
return False
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
def _history_to_prompt(message, history):
|
| 80 |
"""Build a simple text prompt for the stable cohere.chat API."""
|
| 81 |
parts = []
|
| 82 |
-
for u, a in (history
|
| 83 |
if u:
|
| 84 |
parts.append(f"User: {u}")
|
| 85 |
if a:
|
|
@@ -145,9 +157,11 @@ def load_local_model():
|
|
| 145 |
|
| 146 |
def build_inputs(tokenizer, message, history):
|
| 147 |
msgs = []
|
| 148 |
-
for u, a in (history
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
| 151 |
msgs.append({"role": "user", "content": message})
|
| 152 |
return tokenizer.apply_chat_template(
|
| 153 |
msgs, tokenize=True, add_generation_prompt=True, return_tensors="pt"
|
|
|
|
| 76 |
return True
|
| 77 |
return False
|
| 78 |
|
| 79 |
+
def _iter_user_assistant(history):
|
| 80 |
+
"""
|
| 81 |
+
Yield (user, assistant) pairs from a Gradio history list.
|
| 82 |
+
Safely handles items that are lists/tuples with >2 elements.
|
| 83 |
+
"""
|
| 84 |
+
for item in (history or []):
|
| 85 |
+
if isinstance(item, (list, tuple)):
|
| 86 |
+
u = item[0] if len(item) > 0 else ""
|
| 87 |
+
a = item[1] if len(item) > 1 else ""
|
| 88 |
+
yield u, a
|
| 89 |
+
# If dicts ever appear, extend handling here.
|
| 90 |
+
|
| 91 |
def _history_to_prompt(message, history):
|
| 92 |
"""Build a simple text prompt for the stable cohere.chat API."""
|
| 93 |
parts = []
|
| 94 |
+
for u, a in _iter_user_assistant(history):
|
| 95 |
if u:
|
| 96 |
parts.append(f"User: {u}")
|
| 97 |
if a:
|
|
|
|
| 157 |
|
| 158 |
def build_inputs(tokenizer, message, history):
|
| 159 |
msgs = []
|
| 160 |
+
for u, a in _iter_user_assistant(history):
|
| 161 |
+
if u:
|
| 162 |
+
msgs.append({"role": "user", "content": u})
|
| 163 |
+
if a:
|
| 164 |
+
msgs.append({"role": "assistant", "content": a})
|
| 165 |
msgs.append({"role": "user", "content": message})
|
| 166 |
return tokenizer.apply_chat_template(
|
| 167 |
msgs, tokenize=True, add_generation_prompt=True, return_tensors="pt"
|