Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import google.generativeai as genai
|
| 4 |
|
| 5 |
-
# API
|
| 6 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 7 |
|
| 8 |
# モデルの初期化(Gemini 2.0 Flash)
|
|
@@ -11,54 +11,47 @@ model = genai.GenerativeModel(model_name='gemini-2.0-flash')
|
|
| 11 |
# チャット履歴の初期化
|
| 12 |
chat_history = []
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
def generate_response(
|
| 16 |
global chat_history
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
# システムメッセージを履歴の最初に追加
|
| 23 |
-
if system_prompt:
|
| 24 |
-
chat_history.insert(0, {"role": "system", "parts": [system_prompt]})
|
| 25 |
-
|
| 26 |
-
# ユーザーのメッセージを履歴に追加
|
| 27 |
-
chat_history.append({"role": "user", "parts": [message]})
|
| 28 |
-
|
| 29 |
-
# モデルからの応答を取得
|
| 30 |
response = model.generate_content(
|
| 31 |
chat_history,
|
| 32 |
generation_config={
|
| 33 |
-
"temperature":
|
| 34 |
-
"top_p":
|
| 35 |
-
"top_k":
|
| 36 |
"max_output_tokens": int(max_output_tokens)
|
| 37 |
}
|
| 38 |
)
|
| 39 |
-
|
| 40 |
# 応答を履歴に追加
|
| 41 |
chat_history.append({"role": "model", "parts": [response.text]})
|
| 42 |
-
|
| 43 |
-
return response.text
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import google.generativeai as genai
|
| 4 |
|
| 5 |
+
# APIキーの設定
|
| 6 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 7 |
|
| 8 |
# モデルの初期化(Gemini 2.0 Flash)
|
|
|
|
| 11 |
# チャット履歴の初期化
|
| 12 |
chat_history = []
|
| 13 |
|
| 14 |
+
# 応答生成関数
|
| 15 |
+
def generate_response(user_input, temperature, top_p, top_k, max_output_tokens):
|
| 16 |
global chat_history
|
| 17 |
+
# ユーザー入力を履歴に追加
|
| 18 |
+
chat_history.append({"role": "user", "parts": [user_input]})
|
| 19 |
+
|
| 20 |
+
# 設定に基づいて応答生成
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
response = model.generate_content(
|
| 22 |
chat_history,
|
| 23 |
generation_config={
|
| 24 |
+
"temperature": temperature,
|
| 25 |
+
"top_p": top_p,
|
| 26 |
+
"top_k": top_k,
|
| 27 |
"max_output_tokens": int(max_output_tokens)
|
| 28 |
}
|
| 29 |
)
|
| 30 |
+
|
| 31 |
# 応答を履歴に追加
|
| 32 |
chat_history.append({"role": "model", "parts": [response.text]})
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# 履歴を整形して表示
|
| 35 |
+
conversation = ""
|
| 36 |
+
for message in chat_history:
|
| 37 |
+
role = "ユーザー" if message["role"] == "user" else "Gemini"
|
| 38 |
+
conversation += f"{role}: {message['parts'][0]}\n"
|
| 39 |
+
return conversation
|
| 40 |
|
| 41 |
+
# Gradioインターフェースの設定
|
| 42 |
+
iface = gr.Interface(
|
| 43 |
+
fn=generate_response,
|
| 44 |
+
inputs=[
|
| 45 |
+
gr.Textbox(label="ユーザー入力"),
|
| 46 |
+
gr.Slider(0.0, 1.0, value=0.7, step=0.05, label="Temperature"),
|
| 47 |
+
gr.Slider(0.0, 1.0, value=0.9, step=0.05, label="Top-p"),
|
| 48 |
+
gr.Slider(1, 100, value=40, step=1, label="Top-k"),
|
| 49 |
+
gr.Number(value=1024, label="Max Output Tokens"),
|
| 50 |
+
],
|
| 51 |
+
outputs="text",
|
| 52 |
+
title="Gemini Chatbot",
|
| 53 |
+
description="Gemini 2.0 Flashモデルを使用したチャットボットです。temperatureやtop-pなどを自由に調整できます。",
|
| 54 |
+
)
|
| 55 |
|
| 56 |
+
# インターフェースの起動
|
| 57 |
+
iface.launch()
|