alanchen1115 commited on
Commit
f91846a
1 Parent(s): 6662bad

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +17 -18
main.py CHANGED
@@ -1,20 +1,16 @@
 
1
  from fastapi.middleware.cors import CORSMiddleware
2
  from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status
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
- client = Client(os.environ["GEMINI_API"])
18
  line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
19
  line_handler = WebhookHandler(os.environ["CHANNEL_SECRET"])
20
  working_status = os.getenv("DEFALUT_TALKING", default = "true").lower() == "true"
@@ -63,11 +59,14 @@ def handle_message(event):
63
 
64
  elif working_status:
65
  try:
66
- message = event.message.text
67
- out = client.predict(message, api_name="/predict")
68
-
 
 
 
69
  except:
70
- out = "Gemini error!"
71
 
72
  line_bot_api.reply_message(
73
  event.reply_token,
 
1
+ import json, os
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status
4
+ import google.generativeai as genai
5
+
6
+ from linebot import LineBotApi, WebhookHandler
7
+ from linebot.exceptions import InvalidSignatureError
8
+ from linebot.models import MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage
9
+
10
+ genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
11
+ generation_config = genai.types.GenerationConfig(max_output_tokens=2048, temperature=0.2, top_p=0.5, top_k=16)
12
+ model = genai.GenerativeModel('gemini-1.5-flash')
13
 
 
 
 
 
 
 
 
 
 
 
 
14
  line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
15
  line_handler = WebhookHandler(os.environ["CHANNEL_SECRET"])
16
  working_status = os.getenv("DEFALUT_TALKING", default = "true").lower() == "true"
 
59
 
60
  elif working_status:
61
  try:
62
+ prompt = event.message.text
63
+ completion = model.generate_content(prompt, generation_config=generation_config)
64
+ if (completion.parts[0].text != None):
65
+ out = completion.parts[0].text
66
+ else:
67
+ out = "Gemini沒答案!請換個說法!"
68
  except:
69
+ out = "Gemini執行出錯!請換個說法!"
70
 
71
  line_bot_api.reply_message(
72
  event.reply_token,