Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,9 @@ import os
|
|
| 7 |
from huggingface_hub import InferenceClient
|
| 8 |
import json
|
| 9 |
from langchain_community.agent_toolkits import GmailToolkit
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
# Initialize FastAPI app
|
|
@@ -18,7 +21,10 @@ def read_root():
|
|
| 18 |
return {"message": "Connection"}
|
| 19 |
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
| 22 |
UPLOAD_FOLDER = "uploaded_pdfs"
|
| 23 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 24 |
|
|
@@ -37,6 +43,29 @@ async def upload_pdf(file: UploadFile = File(...)):
|
|
| 37 |
def list_uploaded_files():
|
| 38 |
files = os.listdir(UPLOAD_FOLDER)
|
| 39 |
return {"files": files}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# # API endpoint for prediction
|
| 41 |
# @app.post("/predict")
|
| 42 |
# async def predict_image(file: UploadFile = File(...)):
|
|
|
|
| 7 |
from huggingface_hub import InferenceClient
|
| 8 |
import json
|
| 9 |
from langchain_community.agent_toolkits import GmailToolkit
|
| 10 |
+
import time
|
| 11 |
+
import requests
|
| 12 |
+
from datetime import datetime
|
| 13 |
|
| 14 |
|
| 15 |
# Initialize FastAPI app
|
|
|
|
| 21 |
return {"message": "Connection"}
|
| 22 |
|
| 23 |
|
| 24 |
+
@app.get("/ping")
|
| 25 |
+
def ping():
|
| 26 |
+
return {"status": "alive"}
|
| 27 |
+
|
| 28 |
UPLOAD_FOLDER = "uploaded_pdfs"
|
| 29 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 30 |
|
|
|
|
| 43 |
def list_uploaded_files():
|
| 44 |
files = os.listdir(UPLOAD_FOLDER)
|
| 45 |
return {"files": files}
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def keep_alive(space_url="https://1mr-apigmail.hf.space/ping", interval_hours=5):
|
| 50 |
+
while True:
|
| 51 |
+
try:
|
| 52 |
+
print(f"🔄 Pinging {space_url} at {datetime.now()}")
|
| 53 |
+
response = requests.get(space_url)
|
| 54 |
+
|
| 55 |
+
if response.status_code == 200:
|
| 56 |
+
print("")
|
| 57 |
+
else:
|
| 58 |
+
print("")
|
| 59 |
+
except Exception as e:
|
| 60 |
+
print("")
|
| 61 |
+
|
| 62 |
+
time.sleep(interval_hours * 3600)
|
| 63 |
+
|
| 64 |
+
# Call the function
|
| 65 |
+
keep_alive()
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
# # API endpoint for prediction
|
| 70 |
# @app.post("/predict")
|
| 71 |
# async def predict_image(file: UploadFile = File(...)):
|