Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,71 +1,123 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
-
|
3 |
-
from fastapi.responses import JSONResponse
|
4 |
-
from fastapi.middleware.cors import CORSMiddleware
|
5 |
-
import fitz # PyMuPDF
|
6 |
-
import requests
|
7 |
-
import shutil
|
8 |
import os
|
9 |
-
|
|
|
|
|
|
|
10 |
from uuid import uuid4
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
app = FastAPI()
|
12 |
-
origins = ["*"]
|
13 |
|
14 |
app.add_middleware(
|
15 |
CORSMiddleware,
|
16 |
-
allow_origins=
|
17 |
allow_credentials=True,
|
18 |
-
allow_methods=["*"],
|
19 |
-
allow_headers=["*"],
|
20 |
)
|
21 |
-
UPLOAD_DIR = "uploads"
|
22 |
-
IMAGES_DIR = "images"
|
23 |
-
TELEGRAPH_UPLOAD_URL = "https://telegra.ph/upload"
|
24 |
-
|
25 |
-
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
26 |
-
os.makedirs(IMAGES_DIR, exist_ok=True)
|
27 |
|
28 |
@app.get("/")
|
29 |
def check_status():
|
30 |
return {"status": "API is working"}
|
31 |
|
32 |
@app.post("/upload")
|
33 |
-
def upload_pdf(file: UploadFile = File(...)):
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
for i, page in enumerate(doc):
|
46 |
-
image_path = os.path.join(IMAGES_DIR, f"{uuid4()}.png")
|
47 |
-
pix = page.get_pixmap()
|
48 |
-
pix.save(image_path)
|
49 |
-
|
50 |
-
telegraph_url = upload_to_telegraph(image_path)
|
51 |
-
if telegraph_url:
|
52 |
-
image_links.append(telegraph_url)
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
response = requests.post(TELEGRAPH_UPLOAD_URL, files={"file": img_file})
|
62 |
-
|
63 |
-
if response.status_code == 200 and isinstance(response.json(), list):
|
64 |
-
return "https://telegra.ph" + response.json()[0]["src"]
|
65 |
-
return None
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
if __name__ == "__main__":
|
70 |
import uvicorn
|
71 |
-
uvicorn.run(app, host="0.0.0.0", port=7860, log_level="info", reload=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import shutil
|
3 |
+
import fitz # PyMuPDF
|
4 |
+
import logging
|
5 |
+
import asyncio
|
6 |
from uuid import uuid4
|
7 |
+
from fastapi import FastAPI, File, UploadFile
|
8 |
+
from telegram import Bot
|
9 |
+
from telegram.constants import ParseMode
|
10 |
+
from starlette.middleware.cors import CORSMiddleware
|
11 |
+
|
12 |
+
# Logging Setup
|
13 |
+
logging.basicConfig(level=logging.INFO)
|
14 |
+
logger = logging.getLogger(__name__)
|
15 |
+
|
16 |
+
# Telegram Bot Setup
|
17 |
+
TELEGRAM_BOT_TOKEN = "8046554458:AAGxVuq_xrTXihLmxE_vcopsdg11zPvv1_I"
|
18 |
+
TELEGRAM_CHAT_ID = "5173085859"
|
19 |
+
bot = Bot(token=TELEGRAM_BOT_TOKEN)
|
20 |
+
|
21 |
+
# Directories Setup
|
22 |
+
UPLOAD_DIR = "uploads"
|
23 |
+
IMAGES_DIR = "images"
|
24 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
25 |
+
os.makedirs(IMAGES_DIR, exist_ok=True)
|
26 |
+
|
27 |
+
# FastAPI Setup
|
28 |
app = FastAPI()
|
|
|
29 |
|
30 |
app.add_middleware(
|
31 |
CORSMiddleware,
|
32 |
+
allow_origins=["*"],
|
33 |
allow_credentials=True,
|
34 |
+
allow_methods=["*"],
|
35 |
+
allow_headers=["*"],
|
36 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
@app.get("/")
|
39 |
def check_status():
|
40 |
return {"status": "API is working"}
|
41 |
|
42 |
@app.post("/upload")
|
43 |
+
async def upload_pdf(file: UploadFile = File(...)):
|
44 |
+
""" Upload PDF, convert to images, send to Telegram & cleanup """
|
45 |
+
file_id = str(uuid4())
|
46 |
+
file_path = os.path.join(UPLOAD_DIR, f"{file_id}.pdf")
|
47 |
+
|
48 |
+
try:
|
49 |
+
# Save uploaded PDF
|
50 |
+
with open(file_path, "wb") as buffer:
|
51 |
+
shutil.copyfileobj(file.file, buffer)
|
52 |
+
|
53 |
+
# Convert PDF to images & send to Telegram
|
54 |
+
image_links = await convert_pdf_to_images(file_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
+
return {"status": "Images sent to Telegram", "images": image_links}
|
57 |
+
|
58 |
+
except Exception as e:
|
59 |
+
logger.error(f"Error processing PDF: {e}")
|
60 |
+
# Cleanup uploaded file if exists
|
61 |
+
if os.path.exists(file_path):
|
62 |
+
os.remove(file_path)
|
63 |
+
return {"status": "error", "message": str(e)}
|
64 |
+
|
65 |
+
async def convert_pdf_to_images(pdf_path):
|
66 |
+
""" Converts PDF pages to images & sends them to Telegram """
|
67 |
+
try:
|
68 |
+
# Use a thread executor for synchronous PDF operations
|
69 |
+
loop = asyncio.get_running_loop()
|
70 |
+
doc = await loop.run_in_executor(None, fitz.open, pdf_path)
|
71 |
+
image_links = []
|
72 |
+
|
73 |
+
for i in range(len(doc)):
|
74 |
+
page = await loop.run_in_executor(None, doc.load_page, i)
|
75 |
+
image_path = os.path.join(IMAGES_DIR, f"{uuid4()}.png")
|
76 |
+
|
77 |
+
# Get pixmap and save image
|
78 |
+
pix = await loop.run_in_executor(None, page.get_pixmap)
|
79 |
+
await loop.run_in_executor(None, pix.save, image_path)
|
80 |
+
|
81 |
+
# Send image to Telegram with page number caption
|
82 |
+
caption = f"Page {i+1}"
|
83 |
+
telegram_url = await send_image_to_telegram(image_path, caption)
|
84 |
+
|
85 |
+
if telegram_url:
|
86 |
+
image_links.append(telegram_url)
|
87 |
+
os.remove(image_path) # Cleanup only if successful
|
88 |
+
else:
|
89 |
+
logger.error(f"Failed to send page {i+1}")
|
90 |
+
if os.path.exists(image_path):
|
91 |
+
os.remove(image_path) # Cleanup failed image
|
92 |
|
93 |
+
await loop.run_in_executor(None, doc.close)
|
94 |
+
return image_links
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
+
except Exception as e:
|
97 |
+
logger.error(f"Error converting PDF: {e}")
|
98 |
+
raise
|
99 |
+
finally:
|
100 |
+
# Cleanup PDF after processing
|
101 |
+
if os.path.exists(pdf_path):
|
102 |
+
os.remove(pdf_path)
|
103 |
|
104 |
+
async def send_image_to_telegram(image_path, caption):
|
105 |
+
""" Sends an image to Telegram Bot with specified caption """
|
106 |
+
try:
|
107 |
+
with open(image_path, "rb") as img_file:
|
108 |
+
message = await bot.send_photo(
|
109 |
+
chat_id=TELEGRAM_CHAT_ID,
|
110 |
+
photo=img_file,
|
111 |
+
caption=caption
|
112 |
+
)
|
113 |
+
# Return better URL format if possible (note: may not work for private chats)
|
114 |
+
if message.chat.username:
|
115 |
+
return f"https://t.me/{message.chat.username}/{message.message_id}"
|
116 |
+
return f"https://t.me/c/{str(message.chat.id).replace('-100', '')}/{message.message_id}"
|
117 |
+
except Exception as e:
|
118 |
+
logger.error(f"Error sending image: {e}")
|
119 |
+
return None
|
120 |
|
121 |
if __name__ == "__main__":
|
122 |
import uvicorn
|
123 |
+
uvicorn.run(app, host="0.0.0.0", port=7860, log_level="info", reload=True)
|