Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -2,22 +2,23 @@ import json, os
|
|
| 2 |
import gradio as gr
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status
|
| 5 |
-
import google.generativeai as genai
|
| 6 |
-
|
| 7 |
from linebot import LineBotApi, WebhookHandler
|
| 8 |
from linebot.exceptions import InvalidSignatureError
|
| 9 |
from linebot.models import MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage
|
| 10 |
|
| 11 |
# 設定 Google AI API 金鑰
|
| 12 |
-
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
|
|
|
| 13 |
|
| 14 |
# 設定生成文字的參數
|
| 15 |
generation_config = genai.types.GenerationConfig(max_output_tokens=2048, temperature=0.2, top_p=0.5, top_k=16)
|
| 16 |
|
| 17 |
# 使用 Gemini-2.0-flash 模型
|
| 18 |
-
model = genai.GenerativeModel('gemini-2.0-flash',
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
# 設定 Line Bot 的 API 金鑰和秘密金鑰
|
| 23 |
line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
|
|
@@ -90,7 +91,7 @@ def handle_message(event):
|
|
| 90 |
try:
|
| 91 |
# 使用 model.start_chat 來初始化 user_id 的對話
|
| 92 |
user_id = event.source.user_id
|
| 93 |
-
chat = chat_sessions.get(user_id) or model.start_chat(history=[])
|
| 94 |
chat_sessions[user_id] = chat
|
| 95 |
# 取得使用者輸入的文字
|
| 96 |
user_input = event.message.text
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status
|
| 5 |
+
# import google.generativeai as genai
|
| 6 |
+
from google import genai
|
| 7 |
from linebot import LineBotApi, WebhookHandler
|
| 8 |
from linebot.exceptions import InvalidSignatureError
|
| 9 |
from linebot.models import MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage
|
| 10 |
|
| 11 |
# 設定 Google AI API 金鑰
|
| 12 |
+
# genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
| 13 |
+
client = genai.Client(api_key=userdata.get("GOOGLE_API_KEY"))
|
| 14 |
|
| 15 |
# 設定生成文字的參數
|
| 16 |
generation_config = genai.types.GenerationConfig(max_output_tokens=2048, temperature=0.2, top_p=0.5, top_k=16)
|
| 17 |
|
| 18 |
# 使用 Gemini-2.0-flash 模型
|
| 19 |
+
# model = genai.GenerativeModel('gemini-2.0-flash',
|
| 20 |
+
# generation_config=generation_config,
|
| 21 |
+
# system_instruction="你是博通古今的萬應機器人!")
|
| 22 |
|
| 23 |
# 設定 Line Bot 的 API 金鑰和秘密金鑰
|
| 24 |
line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
|
|
|
|
| 91 |
try:
|
| 92 |
# 使用 model.start_chat 來初始化 user_id 的對話
|
| 93 |
user_id = event.source.user_id
|
| 94 |
+
chat = chat_sessions.get(user_id) or client.chats.create(model="gemini-2.0-flash", config=generation_config) #model.start_chat(history=[])
|
| 95 |
chat_sessions[user_id] = chat
|
| 96 |
# 取得使用者輸入的文字
|
| 97 |
user_input = event.message.text
|