Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# 환경 변수에서 API 토큰 가져오기
|
| 6 |
TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
|
@@ -9,6 +11,9 @@ TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
|
| 9 |
if not TOKEN:
|
| 10 |
raise ValueError("API token is not set. Please set the HUGGINGFACE_API_TOKEN environment variable.")
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
def respond(
|
| 13 |
message,
|
| 14 |
history: list[tuple[str, str]],
|
|
@@ -17,16 +22,18 @@ def respond(
|
|
| 17 |
temperature=0.7,
|
| 18 |
top_p=0.95,
|
| 19 |
):
|
|
|
|
|
|
|
|
|
|
| 20 |
messages = [{"role": "system", "content": system_message}]
|
| 21 |
|
| 22 |
-
|
|
|
|
| 23 |
if val[0]:
|
| 24 |
messages.append({"role": "user", "content": val[0]})
|
| 25 |
if val[1]:
|
| 26 |
messages.append({"role": "assistant", "content": val[1]})
|
| 27 |
|
| 28 |
-
messages.append({"role": "user", "content": message})
|
| 29 |
-
|
| 30 |
headers = {
|
| 31 |
"Authorization": f"Bearer {TOKEN}",
|
| 32 |
"Content-Type": "application/json"
|
|
@@ -50,6 +57,9 @@ def respond(
|
|
| 50 |
# content 영역만 출력
|
| 51 |
if "choices" in response_json:
|
| 52 |
content = response_json["choices"][0]["message"]["content"]
|
|
|
|
|
|
|
|
|
|
| 53 |
yield content
|
| 54 |
|
| 55 |
theme="Nymbo/Nymbo_Theme"
|
|
@@ -72,4 +82,4 @@ demo = gr.ChatInterface(
|
|
| 72 |
)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
| 75 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
+
import json
|
| 5 |
+
from collections import deque
|
| 6 |
|
| 7 |
# 환경 변수에서 API 토큰 가져오기
|
| 8 |
TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
|
|
|
| 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(
|
| 18 |
message,
|
| 19 |
history: list[tuple[str, str]],
|
|
|
|
| 22 |
temperature=0.7,
|
| 23 |
top_p=0.95,
|
| 24 |
):
|
| 25 |
+
# 현재 대화 내용을 메모리에 추가
|
| 26 |
+
memory.append((message, None))
|
| 27 |
+
|
| 28 |
messages = [{"role": "system", "content": system_message}]
|
| 29 |
|
| 30 |
+
# 메모리에서 대화 기록을 가져와 메시지 목록에 추가
|
| 31 |
+
for val in memory:
|
| 32 |
if val[0]:
|
| 33 |
messages.append({"role": "user", "content": val[0]})
|
| 34 |
if val[1]:
|
| 35 |
messages.append({"role": "assistant", "content": val[1]})
|
| 36 |
|
|
|
|
|
|
|
| 37 |
headers = {
|
| 38 |
"Authorization": f"Bearer {TOKEN}",
|
| 39 |
"Content-Type": "application/json"
|
|
|
|
| 57 |
# content 영역만 출력
|
| 58 |
if "choices" in response_json:
|
| 59 |
content = response_json["choices"][0]["message"]["content"]
|
| 60 |
+
response_text = content
|
| 61 |
+
# 마지막 대화에 모델의 응답을 추가하여 메모리에 저장
|
| 62 |
+
memory[-1] = (message, response_text)
|
| 63 |
yield content
|
| 64 |
|
| 65 |
theme="Nymbo/Nymbo_Theme"
|
|
|
|
| 82 |
)
|
| 83 |
|
| 84 |
if __name__ == "__main__":
|
| 85 |
+
demo.queue(concurrency_count=20).launch() # 큐를 10으로 설정하여 동시 처리 가능
|