Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,14 +4,11 @@ import os
|
|
| 4 |
import json
|
| 5 |
from collections import deque
|
| 6 |
|
| 7 |
-
# 환경 변수에서 API 토큰 가져오기
|
| 8 |
TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
| 9 |
|
| 10 |
-
# API 토큰이 설정되어 있는지 확인
|
| 11 |
if not TOKEN:
|
| 12 |
raise ValueError("API token is not set. Please set the HUGGINGFACE_API_TOKEN environment variable.")
|
| 13 |
|
| 14 |
-
# 대화 기록을 관리하는 큐 (최대 10개의 대화 기록을 유지)
|
| 15 |
memory = deque(maxlen=10)
|
| 16 |
|
| 17 |
def respond(
|
|
@@ -22,15 +19,11 @@ def respond(
|
|
| 22 |
temperature=0.7,
|
| 23 |
top_p=0.95,
|
| 24 |
):
|
| 25 |
-
# 시스템 메시지에 접두사 추가
|
| 26 |
system_prefix = "System: 입력어의 언어(영어, 한국어, 중국어, 일본어 등)에 따라 동일한 언어로 답변하라."
|
| 27 |
full_system_message = f"{system_prefix}{system_message}"
|
| 28 |
|
| 29 |
-
# 현재 대화 내용을 메모리에 추가
|
| 30 |
memory.append((message, None))
|
| 31 |
-
|
| 32 |
messages = [{"role": "system", "content": full_system_message}]
|
| 33 |
-
# 메모리에서 대화 기록을 가져와 메시지 목록에 추가
|
| 34 |
for val in memory:
|
| 35 |
if val[0]:
|
| 36 |
messages.append({"role": "user", "content": val[0]})
|
|
@@ -46,28 +39,31 @@ def respond(
|
|
| 46 |
"max_tokens": max_tokens,
|
| 47 |
"temperature": temperature,
|
| 48 |
"top_p": top_p,
|
| 49 |
-
"messages": messages
|
|
|
|
| 50 |
}
|
|
|
|
| 51 |
response = requests.post("https://api-inference.huggingface.co/v1/chat/completions", headers=headers, json=payload, stream=True)
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
for chunk in response.iter_content(chunk_size=None):
|
| 56 |
if chunk:
|
| 57 |
chunk_data = chunk.decode('utf-8')
|
|
|
|
|
|
|
| 58 |
try:
|
| 59 |
response_json = json.loads(chunk_data)
|
| 60 |
-
# content 영역만 출력
|
| 61 |
if "choices" in response_json:
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
except json.JSONDecodeError:
|
| 66 |
-
continue
|
| 67 |
|
| 68 |
theme = "Nymbo/Nymbo_Theme"
|
| 69 |
|
| 70 |
-
# Gradio ChatInterface 설정
|
| 71 |
demo = gr.ChatInterface(
|
| 72 |
fn=respond,
|
| 73 |
theme=theme,
|
|
|
|
| 4 |
import json
|
| 5 |
from collections import deque
|
| 6 |
|
|
|
|
| 7 |
TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
| 8 |
|
|
|
|
| 9 |
if not TOKEN:
|
| 10 |
raise ValueError("API token is not set. Please set the HUGGINGFACE_API_TOKEN environment variable.")
|
| 11 |
|
|
|
|
| 12 |
memory = deque(maxlen=10)
|
| 13 |
|
| 14 |
def respond(
|
|
|
|
| 19 |
temperature=0.7,
|
| 20 |
top_p=0.95,
|
| 21 |
):
|
|
|
|
| 22 |
system_prefix = "System: 입력어의 언어(영어, 한국어, 중국어, 일본어 등)에 따라 동일한 언어로 답변하라."
|
| 23 |
full_system_message = f"{system_prefix}{system_message}"
|
| 24 |
|
|
|
|
| 25 |
memory.append((message, None))
|
|
|
|
| 26 |
messages = [{"role": "system", "content": full_system_message}]
|
|
|
|
| 27 |
for val in memory:
|
| 28 |
if val[0]:
|
| 29 |
messages.append({"role": "user", "content": val[0]})
|
|
|
|
| 39 |
"max_tokens": max_tokens,
|
| 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 |
|
|
|
|
| 67 |
demo = gr.ChatInterface(
|
| 68 |
fn=respond,
|
| 69 |
theme=theme,
|