| | import gradio as gr |
| | import openai |
| | import requests |
| | import os |
| | import fileinput |
| | from dotenv import load_dotenv |
| |
|
| | title="AIお笑い芸人(β)" |
| | inputs_label="あなたが話したいことは何ですか?" |
| | outputs_label="AIが返信をして、あなたを笑わしにかかります。面白さは保証しません。" |
| | description=""" |
| | - AIお笑い芸人(β)に話しかけると、どんな話題でもお笑いネタで返信してくれます!(1分程度で返信します) |
| | - どうも気分が優れないときに話しかけると気分が紛れるかもしれない!? |
| | - ※入出力の文字数は最大1000文字程度までを目安に入力してください。 |
| | """ |
| |
|
| | article = """ |
| | <!-- |
| | <h4>エンジニア募集中!</h4> |
| | 弊社ではAIを利用したサービスを一緒に開発してくれる仲間を募集しております! |
| | <p> |
| | 当サービスに興味を持っていただいた方は、お問い合わせフォームよりご連絡ください! |
| | <p> |
| | <ul> |
| | <li"><a href="https://www.najimino.co.jp/contact">お問い合わせフォーム</a></li> |
| | --> |
| | <h5>注意事項</h5> |
| | <ul> |
| | <li style="font-size: small;">当サービスでは、2023/3/1にリリースされたOpenAI社のChatGPT APIのgpt-3.5-turboを使用しております。</li> |
| | <li style="font-size: small;">当サービスで生成されたコンテンツは、OpenAI が提供する人工知能によるものであり、当サービスやOpenAI がその正確性や信頼性を保証するものではありません。</li> |
| | <li style="font-size: small;"><a href="https://platform.openai.com/docs/usage-policies">OpenAI の利用規約</a>に従い、データ保持しない方針です(ただし諸般の事情によっては変更する可能性はございます)。 |
| | <li style="font-size: small;">当サービスで生成されたコンテンツは事実確認をした上で、コンテンツ生成者およびコンテンツ利用者の責任において利用してください。</li> |
| | <li style="font-size: small;">当サービスでの使用により発生したいかなる損害についても、当社は一切の責任を負いません。</li> |
| | <li style="font-size: small;">当サービスはβ版のため、予告なくサービスを終了する場合がございます。</li> |
| | </ul> |
| | """ |
| |
|
| | load_dotenv() |
| | openai.api_key = os.getenv('OPENAI_API_KEY') |
| | MODEL = "gpt-3.5-turbo" |
| |
|
| | def get_filetext(filename, cache={}): |
| | if filename in cache: |
| | |
| | return cache[filename] |
| | else: |
| | if not os.path.exists(filename): |
| | raise ValueError(f"ファイル '{filename}' が見つかりませんでした") |
| | with open(filename, "r") as f: |
| | text = f.read() |
| | |
| | cache[filename] = text |
| | return text |
| |
|
| | class OpenAI: |
| | |
| | @classmethod |
| | def chat_completion(cls, prompt, start_with=""): |
| | constraints = get_filetext(filename = "constraints.md") |
| | template = get_filetext(filename = "template.md") |
| | |
| | |
| | data = { |
| | "model": "gpt-3.5-turbo", |
| | "messages": [ |
| | {"role": "system", "content": constraints} |
| | ,{"role": "system", "content": template} |
| | ,{"role": "assistant", "content": "Sure!"} |
| | ,{"role": "user", "content": prompt} |
| | ,{"role": "assistant", "content": start_with} |
| | ], |
| | } |
| |
|
| | |
| | response = requests.post( |
| | "https://api.openai.com/v1/chat/completions", |
| | headers={ |
| | "Content-Type": "application/json", |
| | "Authorization": f"Bearer {openai.api_key}" |
| | }, |
| | json=data |
| | ) |
| |
|
| | |
| | result = response.json() |
| | print(result) |
| | content = result["choices"][0]["message"]["content"].strip() |
| | return content |
| |
|
| | class NajiminoAI: |
| | |
| | @classmethod |
| | def generate_emo_prompt(cls, user_message): |
| | template = get_filetext(filename="template.md") |
| | prompt = f""" |
| | {user_message} |
| | --- |
| | 上記を元に、下記テンプレートを埋めてください。 |
| | --- |
| | {template} |
| | """ |
| | return prompt |
| |
|
| | @classmethod |
| | def generate_emo(cls, user_message): |
| | prompt = NajiminoAI.generate_emo_prompt(user_message); |
| | start_with = "" |
| | result = OpenAI.chat_completion(prompt=prompt, start_with=start_with) |
| | return result |
| |
|
| | def main(): |
| | iface = gr.Interface(fn=NajiminoAI.generate_emo, |
| | inputs=gr.Textbox(label=inputs_label), |
| | outputs=gr.Textbox(label=outputs_label), |
| | title=title, |
| | description=description, |
| | article=article, |
| | allow_flagging='never' |
| | ) |
| |
|
| | iface.launch() |
| |
|
| | if __name__ == '__main__': |
| | main() |