MalikShehram commited on
Commit
cf2c452
·
verified ·
1 Parent(s): b8f5847

Upload 3 files

Browse files
Files changed (3) hide show
  1. env.txt +1 -0
  2. main.py +58 -0
  3. requirements (4).txt +23 -0
env.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ NOVITA_API_KEY=sk_D6bKseqQOUZ9buvj64wuXqNivKiFe2_mRTVSeFSxulA
main.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile, Form, HTTPException
2
+ from fastapi.responses import PlainTextResponse, FileResponse, HTMLResponse
3
+ from fastapi.requests import Request
4
+ from fastapi.staticfiles import StaticFiles
5
+ from fastapi.middleware.cors import CORSMiddleware
6
+ from typing import Optional
7
+ from starlette.exceptions import HTTPException as StarletteHTTPException
8
+ from services.client import generate_code_comments_and_docs
9
+
10
+ # ✅ Initialize FastAPI app
11
+ app = FastAPI()
12
+ app.add_middleware(
13
+ CORSMiddleware,
14
+ allow_origins=["*"],
15
+ allow_credentials=True,
16
+ allow_methods=["*"],
17
+ allow_headers=["*"],
18
+ )
19
+
20
+ # Load static data
21
+ app.mount("/static", StaticFiles(directory="static"), name="static")
22
+
23
+
24
+ @app.get("/", response_class=HTMLResponse)
25
+ async def serve_index():
26
+ return FileResponse("static/index.html")
27
+
28
+ # ✅ API route
29
+ @app.post("/generate-docs")
30
+ async def generate_docs(
31
+ file: Optional[UploadFile] = File(None),
32
+ code: Optional[str] = Form(None)
33
+ ):
34
+ try:
35
+ if not file and not code:
36
+ raise HTTPException(status_code=400, detail="You must provide either a file or raw code.")
37
+
38
+ elif code:
39
+ response = generate_code_comments_and_docs(code)
40
+ return response
41
+
42
+ elif file:
43
+ content = await file.read()
44
+ code = content.decode("utf-8")
45
+ response = generate_code_comments_and_docs(code)
46
+ return response
47
+
48
+ else:
49
+ raise Exception("Invalid data")
50
+
51
+ except Exception as ex:
52
+ return PlainTextResponse(f"❌ Server error: {str(ex)}", status_code=500)
53
+
54
+ @app.exception_handler(StarletteHTTPException)
55
+ async def custom_404_handler(request: Request, exc: StarletteHTTPException):
56
+ if exc.status_code == 404:
57
+ return FileResponse("static/not-found.html", media_type="text/html")
58
+ return HTMLResponse(content=str(exc.detail), status_code=exc.status_code)
requirements (4).txt ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ annotated-types==0.7.0
2
+ anyio==4.9.0
3
+ certifi==2025.4.26
4
+ click==8.2.1
5
+ distro==1.9.0
6
+ dotenv==0.9.9
7
+ fastapi==0.115.12
8
+ h11==0.16.0
9
+ httpcore==1.0.9
10
+ httpx==0.28.1
11
+ idna==3.10
12
+ jiter==0.10.0
13
+ openai==1.86.0
14
+ pydantic==2.11.6
15
+ pydantic_core==2.33.2
16
+ python-dotenv==1.1.0
17
+ python-multipart==0.0.20
18
+ sniffio==1.3.1
19
+ starlette==0.46.2
20
+ tqdm==4.67.1
21
+ typing-inspection==0.4.1
22
+ typing_extensions==4.14.0
23
+ uvicorn==0.34.3