Spaces:
Runtime error
Runtime error
rohithk-03
commited on
Commit
·
75c33bc
1
Parent(s):
d587b98
add report generate api
Browse files- a.py +97 -0
- app.py +38 -12
- requirements.txt +6 -1
a.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import fitz # PyMuPDF
|
| 2 |
+
import requests
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
from fpdf import FPDF
|
| 5 |
+
import cloudinary
|
| 6 |
+
import cloudinary.uploader
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def download_pdf(url, save_path):
|
| 10 |
+
"""Download a PDF from a given URL and save it locally."""
|
| 11 |
+
response = requests.get(url)
|
| 12 |
+
with open(save_path, "wb") as f:
|
| 13 |
+
f.write(response.content)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def extract_text_from_pdf(pdf_path):
|
| 17 |
+
"""Extract text from a PDF file."""
|
| 18 |
+
doc = fitz.open(pdf_path)
|
| 19 |
+
text = "".join(page.get_text() for page in doc)
|
| 20 |
+
return text
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def generate_structured_summary(text):
|
| 24 |
+
client = OpenAI(
|
| 25 |
+
base_url="https://openrouter.ai/api/v1",
|
| 26 |
+
api_key="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",
|
| 27 |
+
)
|
| 28 |
+
print(text)
|
| 29 |
+
prompt = (
|
| 30 |
+
"Generate a well-structured professional report from the following text. "
|
| 31 |
+
"Include appropriate headings, subheadings, and bullet points, but do NOT include any instructions on converting the output into a PDF or formatting recommendations.Dont add conclusion"
|
| 32 |
+
f" /n/n{text}"
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
completion = client.chat.completions.create(
|
| 36 |
+
|
| 37 |
+
extra_body={},
|
| 38 |
+
model="deepseek/deepseek-r1:free",
|
| 39 |
+
messages=[
|
| 40 |
+
{"role": "user", "content": prompt}
|
| 41 |
+
]
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
return completion.choices[0].message.content
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def save_structured_pdf(structured_text, output_pdf):
|
| 48 |
+
print(structured_text)
|
| 49 |
+
"""Save the structured text into a well-formatted PDF file using FPDF."""
|
| 50 |
+
pdf = FPDF()
|
| 51 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
| 52 |
+
pdf.add_page()
|
| 53 |
+
pdf.set_font("Arial", size=12)
|
| 54 |
+
|
| 55 |
+
for line in structured_text.split("\n"):
|
| 56 |
+
print(line)
|
| 57 |
+
pdf.multi_cell(0, 10, line)
|
| 58 |
+
|
| 59 |
+
pdf.output(output_pdf)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def upload_to_cloudinary(file_path):
|
| 63 |
+
|
| 64 |
+
cloudinary.config(
|
| 65 |
+
cloud_name="dfdu3nobj",
|
| 66 |
+
api_key="521777423999182",
|
| 67 |
+
api_secret="cYnNmTOJahnLxTz80wrzzXuvZ88"
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
upload_result = cloudinary.uploader.upload(
|
| 71 |
+
"requirements.txt", resource_type="raw")
|
| 72 |
+
print(upload_result)
|
| 73 |
+
return upload_result.get("secure_url")
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def main(pdf1_url, pdf2_url, output_pdf):
|
| 77 |
+
"""Download, extract, summarize, and save summaries in a structured format to a PDF, then upload it."""
|
| 78 |
+
pdf1_path = "pdf1.pdf"
|
| 79 |
+
pdf2_path = "pdf2.pdf"
|
| 80 |
+
|
| 81 |
+
download_pdf(pdf1_url, pdf1_path)
|
| 82 |
+
download_pdf(pdf2_url, pdf2_path)
|
| 83 |
+
|
| 84 |
+
text1 = extract_text_from_pdf(pdf1_path)
|
| 85 |
+
text2 = extract_text_from_pdf(pdf2_path)
|
| 86 |
+
|
| 87 |
+
structured_summary1 = generate_structured_summary(text1)
|
| 88 |
+
structured_summary2 = generate_structured_summary(text2)
|
| 89 |
+
|
| 90 |
+
full_summary = f"Report 1:\n{structured_summary1}\n\nReport 2:\n{structured_summary2}"
|
| 91 |
+
save_structured_pdf(full_summary, output_pdf)
|
| 92 |
+
print(f"Structured summaries saved to {output_pdf}")
|
| 93 |
+
|
| 94 |
+
# Upload to Cloudinary
|
| 95 |
+
pdf_url = upload_to_cloudinary(output_pdf)
|
| 96 |
+
print(f"PDF uploaded to: {pdf_url}")
|
| 97 |
+
return pdf_url
|
app.py
CHANGED
|
@@ -19,6 +19,14 @@ import tempfile
|
|
| 19 |
import os
|
| 20 |
import numpy as np
|
| 21 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Initialize Flask app
|
| 24 |
app = Flask(__name__)
|
|
@@ -48,15 +56,15 @@ def fetchImage():
|
|
| 48 |
|
| 49 |
# Save the image to the current directory
|
| 50 |
if response.status_code == 200:
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
else:
|
| 61 |
print(f"Failed to download image. Status code: {response.status_code}")
|
| 62 |
# Load image
|
|
@@ -114,7 +122,7 @@ def fetchImage():
|
|
| 114 |
|
| 115 |
# Iterate through damages
|
| 116 |
for i in range(num_damages):
|
| 117 |
-
x1, y1, x2, y2 = coordinates[i * 4
|
| 118 |
|
| 119 |
# Ensure the coordinates are within image bounds
|
| 120 |
x1, y1 = max(0, x1), max(0, y1)
|
|
@@ -133,7 +141,8 @@ def fetchImage():
|
|
| 133 |
cv2.imwrite(damage_image_path, cropped_damage)
|
| 134 |
|
| 135 |
# Run the parts detection model on the cropped damage
|
| 136 |
-
result_parts = model_parts.predict(
|
|
|
|
| 137 |
detections_parts = sv.Detections.from_inference(result_parts)
|
| 138 |
|
| 139 |
# Calculate repair cost for each detected part
|
|
@@ -159,7 +168,8 @@ def fetchImage():
|
|
| 159 |
annotated_parts_image = part_annotator.annotate(
|
| 160 |
scene=cropped_damage, detections=detections_parts
|
| 161 |
)
|
| 162 |
-
annotated_parts_path = os.path.join(
|
|
|
|
| 163 |
cv2.imwrite(annotated_parts_path, annotated_parts_image)
|
| 164 |
|
| 165 |
# Save the overall annotated image
|
|
@@ -173,5 +183,21 @@ def fetchImage():
|
|
| 173 |
return jsonify(result)
|
| 174 |
|
| 175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
if __name__ == "__main__":
|
| 177 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 19 |
import os
|
| 20 |
import numpy as np
|
| 21 |
import requests
|
| 22 |
+
import fitz # PyMuPDF
|
| 23 |
+
import requests
|
| 24 |
+
from openai import OpenAI
|
| 25 |
+
from fpdf import FPDF
|
| 26 |
+
import cloudinary
|
| 27 |
+
import cloudinary.uploader
|
| 28 |
+
from PyPDF2 import PdfMerger
|
| 29 |
+
from a import main
|
| 30 |
|
| 31 |
# Initialize Flask app
|
| 32 |
app = Flask(__name__)
|
|
|
|
| 56 |
|
| 57 |
# Save the image to the current directory
|
| 58 |
if response.status_code == 200:
|
| 59 |
+
file_name = "downloaded_image.jpg"
|
| 60 |
+
|
| 61 |
+
image = Image.open(io.BytesIO(response.content))
|
| 62 |
+
|
| 63 |
+
if image.mode == "RGBA":
|
| 64 |
+
image = image.convert("RGB")
|
| 65 |
+
|
| 66 |
+
image.save(file_name, "JPEG", quality=100)
|
| 67 |
+
print(f"Image downloaded and saved as {file_name}")
|
| 68 |
else:
|
| 69 |
print(f"Failed to download image. Status code: {response.status_code}")
|
| 70 |
# Load image
|
|
|
|
| 122 |
|
| 123 |
# Iterate through damages
|
| 124 |
for i in range(num_damages):
|
| 125 |
+
x1, y1, x2, y2 = coordinates[i * 4: (i + 1) * 4]
|
| 126 |
|
| 127 |
# Ensure the coordinates are within image bounds
|
| 128 |
x1, y1 = max(0, x1), max(0, y1)
|
|
|
|
| 141 |
cv2.imwrite(damage_image_path, cropped_damage)
|
| 142 |
|
| 143 |
# Run the parts detection model on the cropped damage
|
| 144 |
+
result_parts = model_parts.predict(
|
| 145 |
+
damage_image_path, confidence=15).json()
|
| 146 |
detections_parts = sv.Detections.from_inference(result_parts)
|
| 147 |
|
| 148 |
# Calculate repair cost for each detected part
|
|
|
|
| 168 |
annotated_parts_image = part_annotator.annotate(
|
| 169 |
scene=cropped_damage, detections=detections_parts
|
| 170 |
)
|
| 171 |
+
annotated_parts_path = os.path.join(
|
| 172 |
+
temp_dir, f"annotated_parts_{i}.png")
|
| 173 |
cv2.imwrite(annotated_parts_path, annotated_parts_image)
|
| 174 |
|
| 175 |
# Save the overall annotated image
|
|
|
|
| 183 |
return jsonify(result)
|
| 184 |
|
| 185 |
|
| 186 |
+
@app.route("/generate-report", methods=["POST"])
|
| 187 |
+
def generate_report():
|
| 188 |
+
file = None
|
| 189 |
+
if "url" in request.form:
|
| 190 |
+
report_url = request.form["report_url"]
|
| 191 |
+
insurance_url = request.form["insurance_url"]
|
| 192 |
+
url = main(report_url, insurance_url, "output.pdf")
|
| 193 |
+
result = {"url": url}
|
| 194 |
+
return jsonify(result)
|
| 195 |
+
|
| 196 |
+
elif "file" in request.files:
|
| 197 |
+
file = request.files["file"]
|
| 198 |
+
with open("uploaded_report.pdf", "wb") as f:
|
| 199 |
+
f.write(file.read())
|
| 200 |
+
|
| 201 |
+
|
| 202 |
if __name__ == "__main__":
|
| 203 |
app.run(host="0.0.0.0", port=7860)
|
requirements.txt
CHANGED
|
@@ -16,4 +16,9 @@ scikit-image
|
|
| 16 |
roboflow
|
| 17 |
supervision
|
| 18 |
opencv-python
|
| 19 |
-
requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
roboflow
|
| 17 |
supervision
|
| 18 |
opencv-python
|
| 19 |
+
requests
|
| 20 |
+
PyMuPDF
|
| 21 |
+
openai
|
| 22 |
+
fpdf
|
| 23 |
+
cloudinary
|
| 24 |
+
PyPDF2
|