Update app.py
Browse files
app.py
CHANGED
|
@@ -2,43 +2,44 @@ import openai
|
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# OpenAI API
|
| 6 |
api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
if api_key is None:
|
| 8 |
-
raise ValueError("ERROR: OpenAI 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 |
-
|
| 19 |
-
if not output_text:
|
| 20 |
-
return "エラー: モデルからの応答がありません。"
|
| 21 |
-
return output_text
|
| 22 |
except Exception as e:
|
| 23 |
-
return f"
|
| 24 |
|
| 25 |
-
#
|
| 26 |
default_text = "以下の英語を日本語に翻訳してください"
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
interface.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# OpenAI APIキーを取得
|
| 6 |
api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
if api_key is None:
|
| 8 |
+
raise ValueError("ERROR: OpenAI 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 |
+
# カスタムCSSを追加
|
| 26 |
+
custom_css = """
|
| 27 |
+
body {
|
| 28 |
+
overflow: auto !important;
|
| 29 |
+
}
|
| 30 |
+
.gradio-container {
|
| 31 |
+
max-height: 100vh;
|
| 32 |
+
overflow-y: auto;
|
| 33 |
+
}
|
| 34 |
+
"""
|
| 35 |
+
|
| 36 |
+
interface = gr.Interface(
|
| 37 |
+
fn=chat_with_model,
|
| 38 |
+
inputs=gr.Textbox(lines=5, value=default_text),
|
| 39 |
+
outputs=gr.Textbox(lines=10, interactive=True),
|
| 40 |
+
title="英→日 スタレ翻訳bot",
|
| 41 |
+
description="ver3.0までのテキストを学習済み",
|
| 42 |
+
css=custom_css # カスタムCSSを適用
|
| 43 |
+
)
|
| 44 |
|
| 45 |
interface.launch()
|