Update app/fir_pdf_gen.py
Browse files- app/fir_pdf_gen.py +16 -44
app/fir_pdf_gen.py
CHANGED
|
@@ -5,16 +5,6 @@ from pydantic import BaseModel
|
|
| 5 |
import os
|
| 6 |
import uuid
|
| 7 |
import httpx
|
| 8 |
-
import cloudinary
|
| 9 |
-
import cloudinary.uploader
|
| 10 |
-
|
| 11 |
-
# Initialize Cloudinary
|
| 12 |
-
cloudinary.config(
|
| 13 |
-
cloud_name = "dgdxa7qqg", # Your Cloudinary cloud name
|
| 14 |
-
api_key = "376418913322648", # Your Cloudinary API key
|
| 15 |
-
api_secret = "ut-74eisi_NAFxfrEUDhER2szgM", # Your Cloudinary API secret
|
| 16 |
-
secure=True
|
| 17 |
-
)
|
| 18 |
|
| 19 |
router = APIRouter()
|
| 20 |
|
|
@@ -29,7 +19,7 @@ def generate_fir_pdf(data: dict) -> str:
|
|
| 29 |
pdf.add_page()
|
| 30 |
pdf.set_font("Arial", size=11)
|
| 31 |
|
| 32 |
-
# Add content
|
| 33 |
pdf.cell(0, 10, f"Book No.: {data['book_no']}", ln=True)
|
| 34 |
pdf.cell(0, 10, f"Form No.: {data['form_no']}", ln=True)
|
| 35 |
pdf.cell(0, 10, f"Police Station: {data['police_station']}", ln=True)
|
|
@@ -44,13 +34,12 @@ def generate_fir_pdf(data: dict) -> str:
|
|
| 44 |
pdf.cell(0, 10, f"Date and Time of Dispatch from Police Station: {data['dispatch_time']}", ln=True)
|
| 45 |
pdf.cell(0, 10, f"Signature of Writer: ..............................", ln=True)
|
| 46 |
|
| 47 |
-
# Save PDF
|
| 48 |
output_dir = "fir_reports"
|
| 49 |
os.makedirs(output_dir, exist_ok=True)
|
| 50 |
file_name = f"FIR_Report_{uuid.uuid4().hex}.pdf"
|
| 51 |
file_path = os.path.join(output_dir, file_name)
|
| 52 |
pdf.output(file_path)
|
| 53 |
-
|
| 54 |
return file_path
|
| 55 |
|
| 56 |
class FIRDetails(BaseModel):
|
|
@@ -69,7 +58,6 @@ class FIRDetails(BaseModel):
|
|
| 69 |
|
| 70 |
@router.get("/download/{file_name}")
|
| 71 |
async def download_file(file_name: str):
|
| 72 |
-
# Check if the file exists before returning
|
| 73 |
file_path = os.path.join("fir_reports", file_name)
|
| 74 |
if not os.path.exists(file_path):
|
| 75 |
raise HTTPException(status_code=404, detail="File not found")
|
|
@@ -79,49 +67,33 @@ async def get_lawgpt_response(description_offense: str) -> str:
|
|
| 79 |
"""
|
| 80 |
Sends the description_offense to an external service and retrieves the response.
|
| 81 |
"""
|
| 82 |
-
url = "http://0.0.0.0:7860/lawgpt/
|
| 83 |
try:
|
| 84 |
async with httpx.AsyncClient() as client:
|
| 85 |
-
response = await client.post(url, json={"
|
| 86 |
-
response.raise_for_status() # Raise error for
|
| 87 |
data = response.json()
|
| 88 |
-
return data.get("
|
| 89 |
except Exception as e:
|
| 90 |
raise HTTPException(status_code=500, detail=f"Failed to get response from LawGPT: {str(e)}")
|
| 91 |
|
| 92 |
@router.post("/")
|
| 93 |
async def generate_fir(details: FIRDetails):
|
| 94 |
try:
|
| 95 |
-
# Get
|
| 96 |
updated_description = await get_lawgpt_response(details.description_offense)
|
|
|
|
|
|
|
| 97 |
details.description_offense = updated_description
|
| 98 |
|
| 99 |
-
# Generate the
|
| 100 |
file_path = generate_fir_pdf(details.dict())
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
resource_type="raw", # PDFs are treated as raw files in Cloudinary
|
| 107 |
-
folder="fir_reports/"
|
| 108 |
-
)
|
| 109 |
-
os.remove(file_path) # Clean up the local file
|
| 110 |
-
|
| 111 |
-
# Get Cloudinary URLs
|
| 112 |
-
view_url = response['secure_url']
|
| 113 |
-
download_url = f"{view_url}?attachment=true"
|
| 114 |
-
|
| 115 |
-
return {
|
| 116 |
-
"message": "FIR PDF generated and uploaded successfully!",
|
| 117 |
-
"view_url": view_url, # Cloudinary view URL
|
| 118 |
-
"download_url": download_url # Cloudinary download URL
|
| 119 |
-
}
|
| 120 |
-
except Exception as e:
|
| 121 |
-
os.remove(file_path) # Clean up in case of failure
|
| 122 |
-
raise HTTPException(status_code=500, detail=f"Error uploading to Cloudinary: {str(e)}")
|
| 123 |
-
|
| 124 |
except HTTPException as http_exc:
|
| 125 |
raise http_exc
|
| 126 |
except Exception as e:
|
| 127 |
-
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 5 |
import os
|
| 6 |
import uuid
|
| 7 |
import httpx
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
router = APIRouter()
|
| 10 |
|
|
|
|
| 19 |
pdf.add_page()
|
| 20 |
pdf.set_font("Arial", size=11)
|
| 21 |
|
| 22 |
+
# Add content
|
| 23 |
pdf.cell(0, 10, f"Book No.: {data['book_no']}", ln=True)
|
| 24 |
pdf.cell(0, 10, f"Form No.: {data['form_no']}", ln=True)
|
| 25 |
pdf.cell(0, 10, f"Police Station: {data['police_station']}", ln=True)
|
|
|
|
| 34 |
pdf.cell(0, 10, f"Date and Time of Dispatch from Police Station: {data['dispatch_time']}", ln=True)
|
| 35 |
pdf.cell(0, 10, f"Signature of Writer: ..............................", ln=True)
|
| 36 |
|
| 37 |
+
# Save PDF
|
| 38 |
output_dir = "fir_reports"
|
| 39 |
os.makedirs(output_dir, exist_ok=True)
|
| 40 |
file_name = f"FIR_Report_{uuid.uuid4().hex}.pdf"
|
| 41 |
file_path = os.path.join(output_dir, file_name)
|
| 42 |
pdf.output(file_path)
|
|
|
|
| 43 |
return file_path
|
| 44 |
|
| 45 |
class FIRDetails(BaseModel):
|
|
|
|
| 58 |
|
| 59 |
@router.get("/download/{file_name}")
|
| 60 |
async def download_file(file_name: str):
|
|
|
|
| 61 |
file_path = os.path.join("fir_reports", file_name)
|
| 62 |
if not os.path.exists(file_path):
|
| 63 |
raise HTTPException(status_code=404, detail="File not found")
|
|
|
|
| 67 |
"""
|
| 68 |
Sends the description_offense to an external service and retrieves the response.
|
| 69 |
"""
|
| 70 |
+
url = "http://0.0.0.0:7860/lawgpt/chat" # Replace with the actual URL
|
| 71 |
try:
|
| 72 |
async with httpx.AsyncClient() as client:
|
| 73 |
+
response = await client.post(url, json={"question": description_offense})
|
| 74 |
+
response.raise_for_status() # Raise an error for HTTP codes >= 400
|
| 75 |
data = response.json()
|
| 76 |
+
return data.get("response", description_offense) # Use original if no response
|
| 77 |
except Exception as e:
|
| 78 |
raise HTTPException(status_code=500, detail=f"Failed to get response from LawGPT: {str(e)}")
|
| 79 |
|
| 80 |
@router.post("/")
|
| 81 |
async def generate_fir(details: FIRDetails):
|
| 82 |
try:
|
| 83 |
+
# Get response from LawGPT for description_offense
|
| 84 |
updated_description = await get_lawgpt_response(details.description_offense)
|
| 85 |
+
|
| 86 |
+
# Replace the description_offense with the processed response
|
| 87 |
details.description_offense = updated_description
|
| 88 |
|
| 89 |
+
# Generate PDF with the updated description
|
| 90 |
file_path = generate_fir_pdf(details.dict())
|
| 91 |
+
|
| 92 |
+
return {
|
| 93 |
+
"message": "FIR PDF generated successfully!",
|
| 94 |
+
"download_url": f"http://0.0.0.0:7860/generate-fir/download/{os.path.basename(file_path)}"
|
| 95 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
except HTTPException as http_exc:
|
| 97 |
raise http_exc
|
| 98 |
except Exception as e:
|
| 99 |
+
raise HTTPException(status_code=500, detail=str(e))
|