Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,8 @@ import requests
|
|
| 3 |
import os
|
| 4 |
import json
|
| 5 |
from collections import deque
|
|
|
|
|
|
|
| 6 |
|
| 7 |
TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
| 8 |
|
|
@@ -11,7 +13,7 @@ if not TOKEN:
|
|
| 11 |
|
| 12 |
memory = deque(maxlen=10)
|
| 13 |
|
| 14 |
-
def respond(
|
| 15 |
message,
|
| 16 |
history: list[tuple[str, str]],
|
| 17 |
system_message="AI Assistant Role",
|
|
@@ -40,27 +42,27 @@ def respond(
|
|
| 40 |
"temperature": temperature,
|
| 41 |
"top_p": top_p,
|
| 42 |
"messages": messages,
|
| 43 |
-
"stream": True
|
| 44 |
}
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
theme = "Nymbo/Nymbo_Theme"
|
| 66 |
|
|
|
|
| 3 |
import os
|
| 4 |
import json
|
| 5 |
from collections import deque
|
| 6 |
+
import asyncio
|
| 7 |
+
import aiohttp
|
| 8 |
|
| 9 |
TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
| 10 |
|
|
|
|
| 13 |
|
| 14 |
memory = deque(maxlen=10)
|
| 15 |
|
| 16 |
+
async def respond(
|
| 17 |
message,
|
| 18 |
history: list[tuple[str, str]],
|
| 19 |
system_message="AI Assistant Role",
|
|
|
|
| 42 |
"temperature": temperature,
|
| 43 |
"top_p": top_p,
|
| 44 |
"messages": messages,
|
| 45 |
+
"stream": True
|
| 46 |
}
|
| 47 |
|
| 48 |
+
async with aiohttp.ClientSession() as session:
|
| 49 |
+
async with session.post("https://api-inference.huggingface.co/v1/chat/completions", headers=headers, json=payload) as response:
|
| 50 |
+
partial_words = ""
|
| 51 |
+
async for chunk in response.content:
|
| 52 |
+
if chunk:
|
| 53 |
+
chunk_data = chunk.decode('utf-8')
|
| 54 |
+
if chunk_data.startswith("data: "):
|
| 55 |
+
chunk_data = chunk_data[6:]
|
| 56 |
+
try:
|
| 57 |
+
response_json = json.loads(chunk_data)
|
| 58 |
+
if "choices" in response_json:
|
| 59 |
+
delta = response_json["choices"][0].get("delta", {})
|
| 60 |
+
if "content" in delta:
|
| 61 |
+
content = delta["content"]
|
| 62 |
+
partial_words += content
|
| 63 |
+
yield partial_words
|
| 64 |
+
except json.JSONDecodeError:
|
| 65 |
+
continue
|
| 66 |
|
| 67 |
theme = "Nymbo/Nymbo_Theme"
|
| 68 |
|