Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import time
|
| 4 |
-
import threading
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
client = InferenceClient("Qwen/Qwen2.5-3b-Instruct")
|
| 7 |
|
| 8 |
def respond(
|
|
@@ -11,11 +13,10 @@ def respond(
|
|
| 11 |
system_message,
|
| 12 |
max_tokens,
|
| 13 |
temperature,
|
| 14 |
-
top_p
|
| 15 |
):
|
| 16 |
messages = [{"role": "system", "content": system_message}]
|
| 17 |
-
|
| 18 |
-
# メッセージ履歴を追加
|
| 19 |
for val in history:
|
| 20 |
if val[0]:
|
| 21 |
messages.append({"role": "user", "content": val[0]})
|
|
@@ -24,40 +25,26 @@ def respond(
|
|
| 24 |
|
| 25 |
messages.append({"role": "user", "content": message})
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
return response_content, response_time
|
| 41 |
-
|
| 42 |
-
# 応答時間を表示するためのスレッド
|
| 43 |
-
response_content = "応答生成中です..."
|
| 44 |
-
thread = threading.Thread(target=ai_response)
|
| 45 |
-
thread.start()
|
| 46 |
-
|
| 47 |
-
# 応答を返すまでの間、経過時間を更新
|
| 48 |
-
elapsed_time_display = ""
|
| 49 |
-
elapsed_time = 0
|
| 50 |
-
while thread.is_alive():
|
| 51 |
-
elapsed_time += 1
|
| 52 |
-
elapsed_time_display = f"{elapsed_time}秒経過..."
|
| 53 |
-
time.sleep(1)
|
| 54 |
-
if not thread.is_alive():
|
| 55 |
-
break
|
| 56 |
-
|
| 57 |
-
thread.join() # スレッド終了を待機
|
| 58 |
|
| 59 |
-
|
|
|
|
| 60 |
|
|
|
|
|
|
|
|
|
|
| 61 |
demo = gr.ChatInterface(
|
| 62 |
respond,
|
| 63 |
additional_inputs=[
|
|
@@ -72,6 +59,7 @@ demo = gr.ChatInterface(
|
|
| 72 |
label="Top-p (核サンプリング)",
|
| 73 |
),
|
| 74 |
],
|
|
|
|
| 75 |
css="""
|
| 76 |
.gradio-container {
|
| 77 |
background-color: #212121;
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import time
|
|
|
|
| 4 |
|
| 5 |
+
"""
|
| 6 |
+
`huggingface_hub` の推論 API サポートについての詳細は、ドキュメントを確認してください: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 7 |
+
"""
|
| 8 |
client = InferenceClient("Qwen/Qwen2.5-3b-Instruct")
|
| 9 |
|
| 10 |
def respond(
|
|
|
|
| 13 |
system_message,
|
| 14 |
max_tokens,
|
| 15 |
temperature,
|
| 16 |
+
top_p,
|
| 17 |
):
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
+
|
|
|
|
| 20 |
for val in history:
|
| 21 |
if val[0]:
|
| 22 |
messages.append({"role": "user", "content": val[0]})
|
|
|
|
| 25 |
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
+
# AI応答時間計測開始
|
| 29 |
+
start_time = time.time()
|
| 30 |
+
|
| 31 |
+
# 応答生成
|
| 32 |
+
response = client.chat_completion(
|
| 33 |
+
messages,
|
| 34 |
+
max_tokens=max_tokens,
|
| 35 |
+
temperature=temperature,
|
| 36 |
+
top_p=top_p,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# 応答にかかった時間を計測
|
| 40 |
+
elapsed_time = time.time() - start_time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# 応答内容と経過時間を返す
|
| 43 |
+
return response.choices[0].message.content, f"応答にかかった時間: {elapsed_time:.2f}秒"
|
| 44 |
|
| 45 |
+
"""
|
| 46 |
+
ChatInterfaceのカスタマイズ方法については、gradioのドキュメントを確認してください: https://www.gradio.app/docs/chatinterface
|
| 47 |
+
"""
|
| 48 |
demo = gr.ChatInterface(
|
| 49 |
respond,
|
| 50 |
additional_inputs=[
|
|
|
|
| 59 |
label="Top-p (核サンプリング)",
|
| 60 |
),
|
| 61 |
],
|
| 62 |
+
# 背景色をCSSで設定
|
| 63 |
css="""
|
| 64 |
.gradio-container {
|
| 65 |
background-color: #212121;
|