Sakalti commited on
Commit
83615d5
·
verified ·
1 Parent(s): 523e129

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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 1.5 Flashを使用)
9
+ model = genai.GenerativeModel(model='gemini-1.5-flash')
10
+
11
+ # チャット履歴の初期化
12
+ chat_history = []
13
+
14
+ # 応答生成関数
15
+ def generate_response(user_input):
16
+ global chat_history
17
+ # ユーザー入力を履歴に追加
18
+ chat_history.append({"role": "user", "parts": [user_input]})
19
+ # モデルからの応答を取得
20
+ response = model.generate_content(chat_history)
21
+ # 応答を履歴に追加
22
+ chat_history.append({"role": "model", "parts": [response.text]})
23
+ # 履歴を整形して表示
24
+ conversation = ""
25
+ for message in chat_history:
26
+ role = "ユーザー" if message["role"] == "user" else "Gemini"
27
+ conversation += f"{role}: {message['parts'][0]}\n"
28
+ return conversation
29
+
30
+ # Gradioインターフェースの設定
31
+ iface = gr.Interface(
32
+ fn=generate_response,
33
+ inputs="text",
34
+ outputs="text",
35
+ title="Gemini Chatbot",
36
+ description="Gemini 1.5 Flashモデルを使用したチャットボットです。会話の履歴が表示されます。",
37
+ )
38
+
39
+ # インターフェースの起動
40
+ iface.launch()