|
|
import openai |
|
|
import gradio as gr |
|
|
import os |
|
|
|
|
|
|
|
|
api_key = os.getenv("OPENAI_API_KEY") |
|
|
if api_key is None: |
|
|
raise ValueError("ERROR: OpenAI APIキーが設定されていません。") |
|
|
|
|
|
client = openai.OpenAI(api_key=api_key) |
|
|
|
|
|
def chat_with_model(user_input): |
|
|
try: |
|
|
response = client.chat.completions.create( |
|
|
model="ft:gpt-4o-mini-2024-07-18:personal:enjphsr:AukEoAFZ", |
|
|
messages=[{"role": "user", "content": user_input}] |
|
|
) |
|
|
return response.choices[0].message.content |
|
|
except Exception as e: |
|
|
return f"エラーが発生しました: {e}" |
|
|
|
|
|
|
|
|
default_text = "以下の英語を日本語に翻訳してください" |
|
|
|
|
|
|
|
|
custom_css = """ |
|
|
body { |
|
|
overflow: auto !important; |
|
|
} |
|
|
.gradio-container { |
|
|
max-height: 100vh; |
|
|
overflow-y: auto; |
|
|
} |
|
|
""" |
|
|
|
|
|
interface = gr.Interface( |
|
|
fn=chat_with_model, |
|
|
inputs=gr.Textbox(lines=5, value=default_text), |
|
|
outputs=gr.Textbox(lines=10, interactive=True), |
|
|
title="英→日 スタレ翻訳bot", |
|
|
description="ver3.0までのテキストを学習済み", |
|
|
css=custom_css |
|
|
) |
|
|
|
|
|
interface.launch() |
|
|
|