seawolf2357 commited on
Commit
bcac619
1 Parent(s): 1f82685

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -20
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
- response = requests.post("https://api-inference.huggingface.co/v1/chat/completions", headers=headers, json=payload, stream=True)
47
-
48
- partial_words = ""
49
- for chunk in response.iter_lines():
50
- if chunk:
51
- chunk_data = chunk.decode('utf-8')
52
- if chunk_data.startswith("data: "):
53
- chunk_data = chunk_data[6:] # "data: " 제거
54
- try:
55
- response_json = json.loads(chunk_data)
56
- if "choices" in response_json:
57
- delta = response_json["choices"][0].get("delta", {})
58
- if "content" in delta:
59
- content = delta["content"]
60
- partial_words += content
61
- yield partial_words
62
- except json.JSONDecodeError:
63
- continue
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