Update app.py
Browse files
app.py
CHANGED
|
@@ -2,30 +2,42 @@ import openai
|
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
api_key = os.getenv("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")
|
|
|
|
|
|
|
| 7 |
|
| 8 |
client = openai.OpenAI(api_key=api_key)
|
| 9 |
|
| 10 |
def chat_with_model(user_input):
|
| 11 |
try:
|
| 12 |
response = client.chat.completions.create(
|
| 13 |
-
model="ft:gpt-4o-mini-2024-07-18:personal:enjphsr:AukEoAFZ",
|
| 14 |
messages=[{"role": "user", "content": user_input}]
|
| 15 |
)
|
| 16 |
return response.choices[0].message.content
|
| 17 |
except Exception as e:
|
| 18 |
return f"エラーが発生しました: {e}"
|
| 19 |
|
| 20 |
-
# 事前入力するテキスト
|
| 21 |
-
default_text = "
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
)
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# OpenAI APIキーを取得
|
| 6 |
+
api_key = os.getenv("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")
|
| 7 |
+
if api_key is None:
|
| 8 |
+
raise ValueError("ERROR: OpenAI APIキーが設定されていません。Hugging FaceのSettings > SecretsでAPIキーを追加してください。")
|
| 9 |
|
| 10 |
client = openai.OpenAI(api_key=api_key)
|
| 11 |
|
| 12 |
def chat_with_model(user_input):
|
| 13 |
try:
|
| 14 |
response = client.chat.completions.create(
|
| 15 |
+
model="ft:gpt-4o-mini-2024-07-18:personal:enjphsr:AukEoAFZ",
|
| 16 |
messages=[{"role": "user", "content": user_input}]
|
| 17 |
)
|
| 18 |
return response.choices[0].message.content
|
| 19 |
except Exception as e:
|
| 20 |
return f"エラーが発生しました: {e}"
|
| 21 |
|
| 22 |
+
# 事前入力するテキスト
|
| 23 |
+
default_text = "こんにちは!今日はどんなことを話しましょうか?"
|
| 24 |
|
| 25 |
+
with gr.Blocks() as interface:
|
| 26 |
+
gr.Markdown("# ファインチューニングモデルチャット")
|
| 27 |
+
user_input = gr.Textbox(lines=5, value=default_text)
|
| 28 |
+
output = gr.Textbox(lines=10, elem_id="output_box") # ✅ `elem_id` を指定
|
| 29 |
+
|
| 30 |
+
btn = gr.Button("送信")
|
| 31 |
+
btn.click(chat_with_model, inputs=user_input, outputs=output)
|
| 32 |
|
| 33 |
+
# ✅ CSS を適用
|
| 34 |
+
interface.load(_js="""
|
| 35 |
+
function autoResize() {
|
| 36 |
+
let box = document.getElementById("output_box");
|
| 37 |
+
box.style.height = "auto";
|
| 38 |
+
box.style.height = box.scrollHeight + "px";
|
| 39 |
+
}
|
| 40 |
+
document.getElementById("output_box").addEventListener("input", autoResize);
|
| 41 |
+
""")
|
| 42 |
+
|
| 43 |
+
interface.launch()
|