Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
|
|
|
|
| 5 |
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
| 6 |
|
|
|
|
|
|
|
| 7 |
client = OpenAI(
|
| 8 |
base_url="https://api-inference.huggingface.co/v1/",
|
| 9 |
api_key=ACCESS_TOKEN,
|
| 10 |
-
# proxies={} #
|
| 11 |
)
|
| 12 |
|
| 13 |
def respond(
|
|
@@ -18,18 +24,22 @@ def respond(
|
|
| 18 |
temperature,
|
| 19 |
top_p,
|
| 20 |
):
|
|
|
|
| 21 |
messages = [{"role": "system", "content": system_message}]
|
| 22 |
|
|
|
|
| 23 |
for val in history:
|
| 24 |
if val[0]:
|
| 25 |
messages.append({"role": "user", "content": val[0]})
|
| 26 |
if val[1]:
|
| 27 |
messages.append({"role": "assistant", "content": val[1]})
|
| 28 |
|
|
|
|
| 29 |
messages.append({"role": "user", "content": message})
|
| 30 |
|
| 31 |
response = ""
|
| 32 |
|
|
|
|
| 33 |
for message in client.chat.completions.create(
|
| 34 |
model="Sakalti/model-3",
|
| 35 |
max_tokens=max_tokens,
|
|
@@ -42,9 +52,11 @@ def respond(
|
|
| 42 |
|
| 43 |
response += token
|
| 44 |
yield response
|
| 45 |
-
|
|
|
|
| 46 |
chatbot = gr.Chatbot(height=600)
|
| 47 |
|
|
|
|
| 48 |
demo = gr.ChatInterface(
|
| 49 |
respond,
|
| 50 |
additional_inputs=[
|
|
@@ -64,5 +76,6 @@ demo = gr.ChatInterface(
|
|
| 64 |
theme="Nymbo/Nymbo_Theme",
|
| 65 |
)
|
| 66 |
|
|
|
|
| 67 |
if __name__ == "__main__":
|
| 68 |
demo.launch()
|
|
|
|
| 1 |
+
# Hugging Face LLaMA Recipes 参照: https://github.com/huggingface/huggingface-llama-recipes/blob/main/inference-api.ipynb
|
| 2 |
+
# huggingface-llama-recipes: https://github.com/huggingface/huggingface-llama-recipes/tree/main
|
| 3 |
+
|
| 4 |
import gradio as gr
|
| 5 |
from openai import OpenAI
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
# 環境変数からHugging Faceのアクセストークンを取得
|
| 9 |
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
| 10 |
|
| 11 |
+
# OpenAIクライアントを初期化
|
| 12 |
+
# proxies引数は使用されないため削除
|
| 13 |
client = OpenAI(
|
| 14 |
base_url="https://api-inference.huggingface.co/v1/",
|
| 15 |
api_key=ACCESS_TOKEN,
|
| 16 |
+
# proxies={} # この行は削除
|
| 17 |
)
|
| 18 |
|
| 19 |
def respond(
|
|
|
|
| 24 |
temperature,
|
| 25 |
top_p,
|
| 26 |
):
|
| 27 |
+
# システムメッセージを含むメッセージリストを作成
|
| 28 |
messages = [{"role": "system", "content": system_message}]
|
| 29 |
|
| 30 |
+
# 既存の履歴をメッセージリストに追加
|
| 31 |
for val in history:
|
| 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 |
+
# 新しいユーザーのメッセージをメッセージリストに追加
|
| 38 |
messages.append({"role": "user", "content": message})
|
| 39 |
|
| 40 |
response = ""
|
| 41 |
|
| 42 |
+
# チャット補完を非同期にリクエスト
|
| 43 |
for message in client.chat.completions.create(
|
| 44 |
model="Sakalti/model-3",
|
| 45 |
max_tokens=max_tokens,
|
|
|
|
| 52 |
|
| 53 |
response += token
|
| 54 |
yield response
|
| 55 |
+
|
| 56 |
+
# Gradioチャットボットを初期化
|
| 57 |
chatbot = gr.Chatbot(height=600)
|
| 58 |
|
| 59 |
+
# Gradioチャットインターフェースを作成
|
| 60 |
demo = gr.ChatInterface(
|
| 61 |
respond,
|
| 62 |
additional_inputs=[
|
|
|
|
| 76 |
theme="Nymbo/Nymbo_Theme",
|
| 77 |
)
|
| 78 |
|
| 79 |
+
# インターフェースを起動
|
| 80 |
if __name__ == "__main__":
|
| 81 |
demo.launch()
|