Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,185 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import fitz # PyMuPDF
|
3 |
+
import io
|
4 |
+
from PIL import Image
|
5 |
+
import google.generativeai as genai
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
import os
|
8 |
+
|
9 |
+
# Load environment variables
|
10 |
+
load_dotenv()
|
11 |
+
|
12 |
+
# Initialize Gemini model
|
13 |
+
genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))
|
14 |
+
gemini_model = genai.GenerativeModel('gemini-2.0-flash-exp')
|
15 |
+
|
16 |
+
class SequentialDrawingAnalyzer:
|
17 |
+
def __init__(self):
|
18 |
+
self.drawings_list = [] # Store all extracted images info
|
19 |
+
self.analyzed_drawings = [] # Store analyzed results
|
20 |
+
|
21 |
+
def extract_drawings_list(self, pdf_bytes):
|
22 |
+
"""First pass: Extract all drawings from PDF and create a list"""
|
23 |
+
try:
|
24 |
+
doc = fitz.open(stream=pdf_bytes, filetype="pdf")
|
25 |
+
|
26 |
+
for page_num in range(len(doc)):
|
27 |
+
page = doc[page_num]
|
28 |
+
image_list = page.get_images()
|
29 |
+
|
30 |
+
for img_idx, img_info in enumerate(image_list):
|
31 |
+
try:
|
32 |
+
xref = img_info[0]
|
33 |
+
base_image = doc.extract_image(xref)
|
34 |
+
image_bytes = base_image["image"]
|
35 |
+
|
36 |
+
self.drawings_list.append({
|
37 |
+
'page': page_num + 1,
|
38 |
+
'drawing_number': img_idx + 1,
|
39 |
+
'xref': xref,
|
40 |
+
'image_bytes': image_bytes
|
41 |
+
})
|
42 |
+
|
43 |
+
except Exception as img_error:
|
44 |
+
st.warning(f"Could not extract drawing {img_idx + 1} on page {page_num + 1}: {str(img_error)}")
|
45 |
+
|
46 |
+
doc.close()
|
47 |
+
return len(self.drawings_list)
|
48 |
+
|
49 |
+
except Exception as e:
|
50 |
+
st.error(f"Error extracting drawings: {str(e)}")
|
51 |
+
return 0
|
52 |
+
|
53 |
+
def analyze_drawing(self, drawing_info):
|
54 |
+
"""Analyze a single drawing"""
|
55 |
+
try:
|
56 |
+
image = Image.open(io.BytesIO(drawing_info['image_bytes']))
|
57 |
+
|
58 |
+
engineering_prompt = """
|
59 |
+
Analyze this engineering drawing in detail. Please provide:
|
60 |
+
1. Drawing Type and Purpose
|
61 |
+
- Identify the type of drawing (assembly, detail, section view, etc.)
|
62 |
+
- Main purpose and function of the depicted component/system
|
63 |
+
|
64 |
+
2. Dimensional Analysis
|
65 |
+
- Key dimensions and measurements
|
66 |
+
- Scale and proportions
|
67 |
+
- Tolerances if specified
|
68 |
+
|
69 |
+
3. Component Details
|
70 |
+
- List all visible components and parts
|
71 |
+
- Materials specifications if indicated
|
72 |
+
- Surface finish markings
|
73 |
+
|
74 |
+
4. Technical Specifications
|
75 |
+
- Any technical notes or special instructions
|
76 |
+
- Welding symbols or special instructions
|
77 |
+
- Reference standards mentioned
|
78 |
+
|
79 |
+
5. Critical Features
|
80 |
+
- Important geometric features
|
81 |
+
- Key interfaces or connections
|
82 |
+
- Safety-critical aspects
|
83 |
+
"""
|
84 |
+
|
85 |
+
response = gemini_model.generate_content([
|
86 |
+
engineering_prompt,
|
87 |
+
image
|
88 |
+
])
|
89 |
+
|
90 |
+
return {
|
91 |
+
'page': drawing_info['page'],
|
92 |
+
'drawing_number': drawing_info['drawing_number'],
|
93 |
+
'image': image,
|
94 |
+
'analysis': response.text
|
95 |
+
}
|
96 |
+
|
97 |
+
except Exception as e:
|
98 |
+
st.error(f"Error analyzing drawing {drawing_info['drawing_number']}: {str(e)}")
|
99 |
+
return None
|
100 |
+
|
101 |
+
# Streamlit UI
|
102 |
+
st.title("Sequential Engineering Drawing Analyzer")
|
103 |
+
|
104 |
+
# Initialize session state
|
105 |
+
if "processed" not in st.session_state:
|
106 |
+
st.session_state.processed = False
|
107 |
+
if "analyzer" not in st.session_state:
|
108 |
+
st.session_state.analyzer = SequentialDrawingAnalyzer()
|
109 |
+
if "current_analysis_index" not in st.session_state:
|
110 |
+
st.session_state.current_analysis_index = 0
|
111 |
+
if "analyzed_drawings" not in st.session_state:
|
112 |
+
st.session_state.analyzed_drawings = []
|
113 |
+
|
114 |
+
# File upload
|
115 |
+
pdf_file = st.file_uploader("Upload PDF containing engineering drawings", type="pdf")
|
116 |
+
|
117 |
+
if pdf_file is not None:
|
118 |
+
# First pass: Extract all drawings if not already processed
|
119 |
+
if not st.session_state.processed:
|
120 |
+
try:
|
121 |
+
with st.spinner("Extracting drawings from PDF..."):
|
122 |
+
pdf_bytes = pdf_file.getvalue()
|
123 |
+
total_drawings = st.session_state.analyzer.extract_drawings_list(pdf_bytes)
|
124 |
+
st.session_state.processed = True
|
125 |
+
|
126 |
+
st.success(f"Found {total_drawings} drawings in the PDF!")
|
127 |
+
|
128 |
+
# Display list of all drawings
|
129 |
+
st.subheader("List of Extracted Drawings:")
|
130 |
+
for drawing in st.session_state.analyzer.drawings_list:
|
131 |
+
st.write(f"Drawing {drawing['drawing_number']} on Page {drawing['page']}")
|
132 |
+
|
133 |
+
st.markdown("---")
|
134 |
+
|
135 |
+
except Exception as e:
|
136 |
+
st.error(f"Failed to process PDF: {str(e)}")
|
137 |
+
st.session_state.processed = False
|
138 |
+
|
139 |
+
# Process drawings sequentially
|
140 |
+
if st.session_state.processed:
|
141 |
+
remaining_drawings = min(5, len(st.session_state.analyzer.drawings_list)) - st.session_state.current_analysis_index
|
142 |
+
|
143 |
+
if remaining_drawings > 0:
|
144 |
+
st.subheader(f"Analyzing Drawing {st.session_state.current_analysis_index + 1} of {min(5, len(st.session_state.analyzer.drawings_list))}")
|
145 |
+
|
146 |
+
# Analyze current drawing
|
147 |
+
current_drawing = st.session_state.analyzer.drawings_list[st.session_state.current_analysis_index]
|
148 |
+
|
149 |
+
with st.spinner(f"Analyzing drawing {current_drawing['drawing_number']} from page {current_drawing['page']}..."):
|
150 |
+
analysis_result = st.session_state.analyzer.analyze_drawing(current_drawing)
|
151 |
+
|
152 |
+
if analysis_result:
|
153 |
+
# Store analysis result
|
154 |
+
st.session_state.analyzed_drawings.append(analysis_result)
|
155 |
+
|
156 |
+
# Increment counter
|
157 |
+
st.session_state.current_analysis_index += 1
|
158 |
+
|
159 |
+
# Auto-rerun to process next drawing
|
160 |
+
if remaining_drawings > 1:
|
161 |
+
st.rerun()
|
162 |
+
else:
|
163 |
+
st.success("Completed analysis of first 5 drawings!")
|
164 |
+
|
165 |
+
elif len(st.session_state.analyzer.drawings_list) > 5:
|
166 |
+
st.info("First 5 drawings have been analyzed. Reload the page to analyze a different set of drawings.")
|
167 |
+
|
168 |
+
# Display all analyzed drawings
|
169 |
+
if st.session_state.analyzed_drawings:
|
170 |
+
st.subheader("Analyzed Drawings:")
|
171 |
+
for analysis in st.session_state.analyzed_drawings:
|
172 |
+
col1, col2 = st.columns([1, 1])
|
173 |
+
|
174 |
+
with col1:
|
175 |
+
st.image(analysis['image'],
|
176 |
+
use_column_width=True,
|
177 |
+
caption=f"Drawing {analysis['drawing_number']} (Page {analysis['page']})")
|
178 |
+
|
179 |
+
with col2:
|
180 |
+
st.markdown("### Analysis Results")
|
181 |
+
st.markdown(analysis['analysis'])
|
182 |
+
|
183 |
+
st.markdown("---")
|
184 |
+
else:
|
185 |
+
st.info("Please upload a PDF file containing engineering drawings to begin analysis.")
|