spav commited on
Commit
7d70ab6
·
verified ·
1 Parent(s): 19eab11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -24
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キーが設定されていません。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
- output_text = response.choices[0].message.content.strip()
19
- if not output_text:
20
- return "エラー: モデルからの応答がありません。"
21
- return output_text
22
  except Exception as e:
23
- return f"エラー: {e}"
24
 
25
- # 事前入力するテキスト
26
  default_text = "以下の英語を日本語に翻訳してください"
27
 
28
- with gr.Blocks() as interface:
29
- gr.Markdown("# ファインチューニングモデルチャット")
30
-
31
- # 入力エリア
32
- gr.Markdown("**入力:**") # 普通のテキスト
33
- user_input = gr.Textbox(lines=1, value=default_text, interactive=True) # ✅ 1行固定の入力欄(高さ自動調整なし)
34
-
35
- # ✅ 送信ボタン(入力と出力の間に配置)
36
- btn = gr.Button("送信")
37
-
38
- # ✅ 出力エリア
39
- gr.Markdown("**出力:**") # 普通のテキスト
40
- output = gr.Textbox(lines=10, interactive=True) # 出力欄
41
-
42
- btn.click(chat_with_model, inputs=user_input, outputs=output)
 
 
 
 
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()