Update app.py
Browse files
app.py
CHANGED
|
@@ -5,15 +5,15 @@ import os
|
|
| 5 |
import tempfile
|
| 6 |
from pathlib import Path
|
| 7 |
|
| 8 |
-
from gemini_client import
|
| 9 |
|
| 10 |
app = FastAPI(title="Gemini Chat API", description="API para interagir com Google Gemini")
|
| 11 |
|
| 12 |
# Inicializar chatbot globalmente
|
| 13 |
chatbot = None
|
| 14 |
|
| 15 |
-
def init_chatbot():
|
| 16 |
-
"""Inicializa o chatbot com os cookies"""
|
| 17 |
global chatbot
|
| 18 |
cookie_path = os.getenv("COOKIE_PATH", "cookies.json")
|
| 19 |
|
|
@@ -21,10 +21,16 @@ def init_chatbot():
|
|
| 21 |
raise FileNotFoundError(f"Arquivo de cookies não encontrado: {cookie_path}")
|
| 22 |
|
| 23 |
try:
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
print(f"Chatbot inicializado com sucesso usando {cookie_path}")
|
| 30 |
except Exception as e:
|
|
@@ -34,7 +40,7 @@ def init_chatbot():
|
|
| 34 |
# Inicializar na startup
|
| 35 |
@app.on_event("startup")
|
| 36 |
async def startup_event():
|
| 37 |
-
init_chatbot()
|
| 38 |
|
| 39 |
@app.get("/")
|
| 40 |
def root():
|
|
@@ -78,8 +84,8 @@ async def get_response(
|
|
| 78 |
}
|
| 79 |
)
|
| 80 |
|
| 81 |
-
# Enviar mensagem
|
| 82 |
-
response = chatbot.ask(message)
|
| 83 |
|
| 84 |
# Retornar resposta formatada
|
| 85 |
return {
|
|
@@ -163,7 +169,7 @@ async def post_response(
|
|
| 163 |
temp_file.close()
|
| 164 |
media_path = temp_file.name
|
| 165 |
|
| 166 |
-
response = chatbot.ask(message, image=media_path)
|
| 167 |
elif video:
|
| 168 |
# Salvar vídeo temporariamente
|
| 169 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=Path(video.filename).suffix if video.filename else ".mp4")
|
|
@@ -172,7 +178,7 @@ async def post_response(
|
|
| 172 |
temp_file.close()
|
| 173 |
media_path = temp_file.name
|
| 174 |
|
| 175 |
-
response = chatbot.ask(message, video=media_path)
|
| 176 |
elif audio:
|
| 177 |
# Salvar áudio temporariamente
|
| 178 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=Path(audio.filename).suffix if audio.filename else ".mp3")
|
|
@@ -181,10 +187,10 @@ async def post_response(
|
|
| 181 |
temp_file.close()
|
| 182 |
media_path = temp_file.name
|
| 183 |
|
| 184 |
-
response = chatbot.ask(message, audio=media_path)
|
| 185 |
else:
|
| 186 |
# Apenas texto
|
| 187 |
-
response = chatbot.ask(message)
|
| 188 |
|
| 189 |
# Retornar resposta formatada
|
| 190 |
return {
|
|
|
|
| 5 |
import tempfile
|
| 6 |
from pathlib import Path
|
| 7 |
|
| 8 |
+
from gemini_client import AsyncChatbot, Model, load_cookies
|
| 9 |
|
| 10 |
app = FastAPI(title="Gemini Chat API", description="API para interagir com Google Gemini")
|
| 11 |
|
| 12 |
# Inicializar chatbot globalmente
|
| 13 |
chatbot = None
|
| 14 |
|
| 15 |
+
async def init_chatbot():
|
| 16 |
+
"""Inicializa o chatbot com os cookies de forma assíncrona"""
|
| 17 |
global chatbot
|
| 18 |
cookie_path = os.getenv("COOKIE_PATH", "cookies.json")
|
| 19 |
|
|
|
|
| 21 |
raise FileNotFoundError(f"Arquivo de cookies não encontrado: {cookie_path}")
|
| 22 |
|
| 23 |
try:
|
| 24 |
+
# Carregar cookies
|
| 25 |
+
secure_1psid, secure_1psidts, additional_cookies = load_cookies(cookie_path)
|
| 26 |
+
|
| 27 |
+
# Criar AsyncChatbot diretamente
|
| 28 |
+
chatbot = await AsyncChatbot.create(
|
| 29 |
+
secure_1psid=secure_1psid,
|
| 30 |
+
secure_1psidts=secure_1psidts,
|
| 31 |
+
model=Model.G_2_5_PRO,
|
| 32 |
+
additional_cookies=additional_cookies,
|
| 33 |
+
cookie_path=cookie_path
|
| 34 |
)
|
| 35 |
print(f"Chatbot inicializado com sucesso usando {cookie_path}")
|
| 36 |
except Exception as e:
|
|
|
|
| 40 |
# Inicializar na startup
|
| 41 |
@app.on_event("startup")
|
| 42 |
async def startup_event():
|
| 43 |
+
await init_chatbot()
|
| 44 |
|
| 45 |
@app.get("/")
|
| 46 |
def root():
|
|
|
|
| 84 |
}
|
| 85 |
)
|
| 86 |
|
| 87 |
+
# Enviar mensagem (assíncrono)
|
| 88 |
+
response = await chatbot.ask(message)
|
| 89 |
|
| 90 |
# Retornar resposta formatada
|
| 91 |
return {
|
|
|
|
| 169 |
temp_file.close()
|
| 170 |
media_path = temp_file.name
|
| 171 |
|
| 172 |
+
response = await chatbot.ask(message, image=media_path)
|
| 173 |
elif video:
|
| 174 |
# Salvar vídeo temporariamente
|
| 175 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=Path(video.filename).suffix if video.filename else ".mp4")
|
|
|
|
| 178 |
temp_file.close()
|
| 179 |
media_path = temp_file.name
|
| 180 |
|
| 181 |
+
response = await chatbot.ask(message, video=media_path)
|
| 182 |
elif audio:
|
| 183 |
# Salvar áudio temporariamente
|
| 184 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=Path(audio.filename).suffix if audio.filename else ".mp3")
|
|
|
|
| 187 |
temp_file.close()
|
| 188 |
media_path = temp_file.name
|
| 189 |
|
| 190 |
+
response = await chatbot.ask(message, audio=media_path)
|
| 191 |
else:
|
| 192 |
# Apenas texto
|
| 193 |
+
response = await chatbot.ask(message)
|
| 194 |
|
| 195 |
# Retornar resposta formatada
|
| 196 |
return {
|