Spaces:
Sleeping
Sleeping
alanchen1115
commited on
Commit
β’
f23a640
1
Parent(s):
9acb27a
Update main.py
Browse files
main.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
from fastapi.middleware.cors import CORSMiddleware
|
2 |
from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status
|
3 |
-
|
4 |
|
5 |
import json
|
6 |
import os
|
@@ -15,7 +15,11 @@ from linebot.models import (
|
|
15 |
MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage
|
16 |
)
|
17 |
|
18 |
-
|
|
|
|
|
|
|
|
|
19 |
app = FastAPI()
|
20 |
app.add_middleware(
|
21 |
CORSMiddleware,
|
@@ -59,11 +63,21 @@ def handle_message(event):
|
|
59 |
event.reply_token,
|
60 |
TextSendMessage(text="Bye!"))
|
61 |
return
|
62 |
-
|
|
|
63 |
if working_status:
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
line_bot_api.reply_message(
|
68 |
event.reply_token,
|
69 |
TextSendMessage(text=out))
|
|
|
1 |
from fastapi.middleware.cors import CORSMiddleware
|
2 |
from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status
|
3 |
+
import google.generativeai as genai
|
4 |
|
5 |
import json
|
6 |
import os
|
|
|
15 |
MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage
|
16 |
)
|
17 |
|
18 |
+
GOOGLE_API_KEY = "os.environ["GOOGLE_API_KEY"]
|
19 |
+
genai.configure(api_key = GOOGLE_API_KEY)
|
20 |
+
# Initialize the Gemini model
|
21 |
+
model = genai.GenerativeModel('gemini-pro')
|
22 |
+
|
23 |
app = FastAPI()
|
24 |
app.add_middleware(
|
25 |
CORSMiddleware,
|
|
|
63 |
event.reply_token,
|
64 |
TextSendMessage(text="Bye!"))
|
65 |
return
|
66 |
+
|
67 |
+
messages = []
|
68 |
if working_status:
|
69 |
+
message = event.message.text
|
70 |
+
messages.append({
|
71 |
+
"role": "user",
|
72 |
+
"parts": [message],
|
73 |
+
})
|
74 |
+
|
75 |
+
response = model.generate_content(messages)
|
76 |
+
out = response.parts[0].text
|
77 |
+
# Check if response is valid
|
78 |
+
if not response.parts or not response.parts[0].text:
|
79 |
+
out = "Gemini: [No response or the response does not contain text]"
|
80 |
+
|
81 |
line_bot_api.reply_message(
|
82 |
event.reply_token,
|
83 |
TextSendMessage(text=out))
|