deepak191z commited on
Commit
0bd715a
·
verified ·
1 Parent(s): e8dda97

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +53 -20
main.py CHANGED
@@ -1,8 +1,13 @@
1
  from fastapi import FastAPI
2
- from duckduckgo_search import DDGS
3
  from fastapi.responses import JSONResponse
4
  from fastapi.middleware.cors import CORSMiddleware
5
-
 
 
 
 
 
6
  app = FastAPI()
7
  origins = ["*"]
8
 
@@ -13,25 +18,53 @@ app.add_middleware(
13
  allow_methods=["*"],
14
  allow_headers=["*"],
15
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- def chat_with_model(query: str, model: str) -> JSONResponse:
18
- results = None
19
- try:
20
- results = DDGS().chat(query, model=model)
21
- except Exception as e:
22
- return JSONResponse(content={"error": str(e)}, status_code=500)
23
- return JSONResponse(content={"results": results})
24
-
25
- @app.get("/chat/")
26
- async def chat(query: str) -> JSONResponse:
27
- results = None
28
- try:
29
- return DDGS().chat(query, model='gpt-4o-mini')
30
- except Exception as e:
31
- try:
32
- return DDGS().chat(query, model='claude-3-haiku')
33
- except Exception as e:
34
- return JSONResponse(content={"error": str(e)})
35
 
36
  if __name__ == "__main__":
37
  import uvicorn
 
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
+ from fastapi import FastAPI, File, UploadFile
10
+ from uuid import uuid4
11
  app = FastAPI()
12
  origins = ["*"]
13
 
 
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
+ file_path = os.path.join(UPLOAD_DIR, f"{uuid4()}.pdf")
35
+ with open(file_path, "wb") as buffer:
36
+ shutil.copyfileobj(file.file, buffer)
37
+
38
+ image_links = convert_pdf_to_images(file_path)
39
+ return {"images": image_links}
40
+
41
+ def convert_pdf_to_images(pdf_path):
42
+ doc = fitz.open(pdf_path)
43
+ image_links = []
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
+ os.remove(image_path) # Cleanup after upload
55
+
56
+ os.remove(pdf_path) # Cleanup uploaded PDF
57
+ return image_links
58
+
59
+ def upload_to_telegraph(image_path):
60
+ with open(image_path, "rb") as img_file:
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