seawolf2357 commited on
Commit
7826a10
1 Parent(s): e8bcde6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -21
app.py CHANGED
@@ -3,7 +3,6 @@ import requests
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")
@@ -44,36 +43,27 @@ async def respond(
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
 
69
- css = """
70
- footer {
71
- visibility: hidden;
72
- }
73
- """
74
-
75
  demo = gr.ChatInterface(
76
- css=css,
77
  fn=respond,
78
  theme=theme,
79
  additional_inputs=[
@@ -85,4 +75,4 @@ demo = gr.ChatInterface(
85
  )
86
 
87
  if __name__ == "__main__":
88
- demo.queue().launch(max_threads=20)
 
3
  import os
4
  import json
5
  from collections import deque
 
6
  import aiohttp
7
 
8
  TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
 
43
  "messages": messages,
44
  "stream": True
45
  }
46
+
47
  async with aiohttp.ClientSession() as session:
48
  async with session.post("https://api-inference.huggingface.co/v1/chat/completions", headers=headers, json=payload) as response:
49
+ try:
50
+ async for chunk in response.content:
51
+ if chunk:
52
+ chunk_data = chunk.decode('utf-8')
 
 
 
53
  response_json = json.loads(chunk_data)
54
  if "choices" in response_json:
55
  delta = response_json["choices"][0].get("delta", {})
56
  if "content" in delta:
57
  content = delta["content"]
58
+ yield content
59
+ except json.JSONDecodeError:
60
+ continue
61
+ except StopAsyncIteration:
62
+ return # 종료 신호를 적절히 처리
63
 
64
  theme = "Nymbo/Nymbo_Theme"
65
 
 
 
 
 
 
 
66
  demo = gr.ChatInterface(
 
67
  fn=respond,
68
  theme=theme,
69
  additional_inputs=[
 
75
  )
76
 
77
  if __name__ == "__main__":
78
+ demo.launch()