Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Alejandro Rodriguez Salamanca
commited on
Commit
·
59c19dd
1
Parent(s):
251d97b
fix issue with responses in JSON format
Browse files
app.py
CHANGED
|
@@ -16,6 +16,27 @@ if not api_key:
|
|
| 16 |
client = ClientV2(api_key=api_key, client_name="hf-tiny-aya-global")
|
| 17 |
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def generate(
|
| 20 |
message: str,
|
| 21 |
history: list[dict],
|
|
@@ -29,16 +50,12 @@ def generate(
|
|
| 29 |
if system_prompt:
|
| 30 |
messages.append({"role": "system", "content": system_prompt})
|
| 31 |
|
| 32 |
-
# Add conversation history
|
| 33 |
for item in history:
|
| 34 |
role = item.get("role")
|
| 35 |
-
content = item.get("content")
|
| 36 |
-
if not isinstance(content, str):
|
| 37 |
-
content = str(content)
|
| 38 |
if role in ("assistant", "user") and content:
|
| 39 |
messages.append({"role": role, "content": content})
|
| 40 |
|
| 41 |
-
# Add current user message
|
| 42 |
current_text = str(message or "").strip()
|
| 43 |
if not current_text:
|
| 44 |
yield ""
|
|
@@ -46,7 +63,6 @@ def generate(
|
|
| 46 |
messages.append({"role": "user", "content": current_text})
|
| 47 |
|
| 48 |
try:
|
| 49 |
-
# Call Cohere API using the correct event type and delta access
|
| 50 |
response = client.chat_stream(
|
| 51 |
model=model_id,
|
| 52 |
messages=messages,
|
|
@@ -56,11 +72,17 @@ def generate(
|
|
| 56 |
|
| 57 |
output = ""
|
| 58 |
for event in response:
|
| 59 |
-
if getattr(event, "type", None)
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
except Exception:
|
| 66 |
logger.exception("Cohere API error")
|
|
|
|
| 16 |
client = ClientV2(api_key=api_key, client_name="hf-tiny-aya-global")
|
| 17 |
|
| 18 |
|
| 19 |
+
def _extract_text(content: object) -> str:
|
| 20 |
+
"""Extract plain text from any Cohere content shape.
|
| 21 |
+
|
| 22 |
+
Handles plain strings, objects with a `.text` attribute,
|
| 23 |
+
and lists of content blocks (e.g. [{'text': '...', 'type': 'text'}]).
|
| 24 |
+
"""
|
| 25 |
+
if content is None:
|
| 26 |
+
return ""
|
| 27 |
+
if isinstance(content, str):
|
| 28 |
+
return content
|
| 29 |
+
if isinstance(content, list):
|
| 30 |
+
parts = [_extract_text(block) for block in content]
|
| 31 |
+
return "".join(parts)
|
| 32 |
+
text = getattr(content, "text", None)
|
| 33 |
+
if text is not None:
|
| 34 |
+
return str(text)
|
| 35 |
+
if isinstance(content, dict):
|
| 36 |
+
return str(content.get("text", ""))
|
| 37 |
+
return ""
|
| 38 |
+
|
| 39 |
+
|
| 40 |
def generate(
|
| 41 |
message: str,
|
| 42 |
history: list[dict],
|
|
|
|
| 50 |
if system_prompt:
|
| 51 |
messages.append({"role": "system", "content": system_prompt})
|
| 52 |
|
|
|
|
| 53 |
for item in history:
|
| 54 |
role = item.get("role")
|
| 55 |
+
content = _extract_text(item.get("content"))
|
|
|
|
|
|
|
| 56 |
if role in ("assistant", "user") and content:
|
| 57 |
messages.append({"role": role, "content": content})
|
| 58 |
|
|
|
|
| 59 |
current_text = str(message or "").strip()
|
| 60 |
if not current_text:
|
| 61 |
yield ""
|
|
|
|
| 63 |
messages.append({"role": "user", "content": current_text})
|
| 64 |
|
| 65 |
try:
|
|
|
|
| 66 |
response = client.chat_stream(
|
| 67 |
model=model_id,
|
| 68 |
messages=messages,
|
|
|
|
| 72 |
|
| 73 |
output = ""
|
| 74 |
for event in response:
|
| 75 |
+
if getattr(event, "type", None) in ("content-delta", "content-start"):
|
| 76 |
+
delta = getattr(event, "delta", None)
|
| 77 |
+
if delta is None:
|
| 78 |
+
continue
|
| 79 |
+
msg = getattr(delta, "message", None)
|
| 80 |
+
if msg is None:
|
| 81 |
+
continue
|
| 82 |
+
text = _extract_text(getattr(msg, "content", None))
|
| 83 |
+
if text:
|
| 84 |
+
output += text
|
| 85 |
+
yield output
|
| 86 |
|
| 87 |
except Exception:
|
| 88 |
logger.exception("Cohere API error")
|