alanchen1115 commited on
Commit
a5ad84f
·
verified ·
1 Parent(s): e75a35a

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +21 -18
main.py CHANGED
@@ -1,30 +1,31 @@
 
1
  from fastapi.middleware.cors import CORSMiddleware
2
- from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status, staticfiles
3
- from gradio_client import Client
4
- import json
5
- import os
 
6
 
7
- from linebot import (
8
- LineBotApi, WebhookHandler
9
- )
10
- from linebot.exceptions import (
11
- InvalidSignatureError
12
- )
13
- from linebot.models import (
14
- MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage
15
- )
 
16
 
17
- # 設定 GeminiRAG 的HF網址
18
- client = Client(os.environ["GeminiRAGapi"], hf_token=os.environ['HF_TOKEN'])
19
  # 設定 Line Bot 的 API 金鑰和秘密金鑰
20
  line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
21
  line_handler = WebhookHandler(os.environ["CHANNEL_SECRET"])
 
22
  # 設定是否正在與使用者交談
23
  working_status = os.getenv("DEFALUT_TALKING", default = "true").lower() == "true"
24
 
25
  # 建立 FastAPI 應用程式
26
  app = FastAPI()
27
- # app.mount("/static", staticfiles.StaticFiles(directory="static"), name="static")
28
 
29
  # 設定 CORS,允許跨域請求
30
  app.add_middleware(
@@ -86,8 +87,10 @@ def handle_message(event):
86
  try:
87
  # 取得使用者輸入的文字
88
  prompt = event.message.text
89
- # 使用 GeminiRAGapi
90
- completion = client.predict(question=prompt, api_name="/predict")
 
 
91
  # 檢查生成結果是否為空
92
  if (completion != None):
93
  # 取得生成結果
 
1
+ import json, os, pathlib, glob
2
  from fastapi.middleware.cors import CORSMiddleware
3
+ from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status
4
+ from google import genai
5
+ from linebot import LineBotApi, WebhookHandler
6
+ from linebot.exceptions import InvalidSignatureError
7
+ from linebot.models import MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage
8
 
9
+ # Retrieve the PDF
10
+ files = glob.glob('docs\*.pdf')
11
+ files_content = [client.files.upload(file=pathlib.Path(file_path)) for file_path in files]
12
+
13
+ # 設定 Google AI API 金鑰
14
+ client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
15
+
16
+ # 設定生成文字的參數 + 角色扮演
17
+ generation_config = genai.types.GenerateContentConfig(max_output_tokens=3000, temperature=0.1, top_p=0.2,
18
+ system_instruction="您是一位問答助手。請僅限使用以上提供的內容來回答問題。如果您不知道答案,請說您不知道,不要捏造答案。")
19
 
 
 
20
  # 設定 Line Bot 的 API 金鑰和秘密金鑰
21
  line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
22
  line_handler = WebhookHandler(os.environ["CHANNEL_SECRET"])
23
+
24
  # 設定是否正在與使用者交談
25
  working_status = os.getenv("DEFALUT_TALKING", default = "true").lower() == "true"
26
 
27
  # 建立 FastAPI 應用程式
28
  app = FastAPI()
 
29
 
30
  # 設定 CORS,允許跨域請求
31
  app.add_middleware(
 
87
  try:
88
  # 取得使用者輸入的文字
89
  prompt = event.message.text
90
+ # 使用 client.models.generate_content
91
+ completion = client.models.generate_content(
92
+ model="gemini-2.5-flash",
93
+ contents=files_content.append(prompt))
94
  # 檢查生成結果是否為空
95
  if (completion != None):
96
  # 取得生成結果