3martini commited on
Commit
fd6f0a4
1 Parent(s): ef12564

Upload folder using huggingface_hub

Browse files
.devcontainer/dc.env ADDED
@@ -0,0 +1 @@
 
 
1
+ OPENAI_API_KEY=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
.devcontainer/docker-compose.yml CHANGED
@@ -4,6 +4,8 @@ services:
4
  solar_eyes:
5
  shm_size: '2gb'
6
  init: true
 
 
7
  build:
8
  context: .
9
  dockerfile: Dockerfile
 
4
  solar_eyes:
5
  shm_size: '2gb'
6
  init: true
7
+ env_file:
8
+ - dc.env
9
  build:
10
  context: .
11
  dockerfile: Dockerfile
.gitignore CHANGED
@@ -12,4 +12,5 @@ yolo*.pt
12
  *.jpg
13
  **/.jpg
14
  pdf_downloads
 
15
 
 
12
  *.jpg
13
  **/.jpg
14
  pdf_downloads
15
+ node_modules
16
 
app.py CHANGED
@@ -1,4 +1,6 @@
1
  import os
 
 
2
 
3
  from fastapi import FastAPI, UploadFile
4
  from fastapi.responses import FileResponse, HTMLResponse, RedirectResponse
@@ -15,7 +17,7 @@ import uuid
15
  from functools import partial
16
  from openai import OpenAI
17
 
18
- PROMPT = "You are analyzing the spec sheet of a solar panel. Plese answer the following questions, format them with bullets. \n"
19
 
20
  # from solareyes.sam import SAM
21
 
@@ -64,6 +66,34 @@ HTML = """
64
  # results: Results = model(image)
65
  # return results
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  def segment_solar_panel(image) -> Results:
69
  # Perform inference
@@ -205,7 +235,6 @@ with gr.Blocks() as demo:
205
  image = process_pdf_core(pdf)
206
  return segment_image(image)
207
 
208
-
209
  with gr.Row():
210
  img_input = gr.Image(label="Upload Image", height=400)
211
  img_output = gr.AnnotatedImage(height=400)
@@ -221,16 +250,22 @@ with gr.Blocks() as demo:
221
 
222
  with gr.Row():
223
  pdf_input = gr.File(label="Upload PDF", file_types=['pdf'], height=200)
 
 
 
 
224
 
225
  pdf_btn = gr.Button("Identify Solar Panel from PDF")
 
226
 
227
  gr.Examples(
228
  inputs = pdf_input,
229
  examples = [os.path.join(pdf_dir, file) for file in os.listdir(pdf_dir)],
230
  )
231
-
232
  section_btn.click(segment_image, [img_input], img_output)
233
- pdf_btn.click(process_pdf, [pdf_input], img_output)
 
 
234
 
235
  #Accept a PDF file, return a jpeg image
236
  @app.post("/uploadPdf", response_class=FileResponse)
@@ -243,40 +278,17 @@ def extract_image(uploadFile: UploadFile) -> FileResponse:
243
  jpeg_image.save(filename)
244
  return FileResponse(filename)
245
 
246
- def parse_pdf_text(file):
247
- pdf = pdfium.PdfDocument(file)
248
- all_text = "PDF Extract Text Contents Below: \n\n"
249
- for page in pdf:
250
- textpage = page.get_textpage()
251
- text_all = textpage.get_text_range()
252
- all_text += text_all
253
-
254
- #use openai to ask questions about text
255
- q1 = "What are module dimensions in L x W x H?"
256
- q2 = "What is the module weight in kilograms?"
257
- q3 = "What are the cable lengthes in millimeters?"
258
- q4 = "What brand, name, or model are the connectors?"
259
- q5 = "How many pieces per container? Prefer 40' HQ or HC, if not available try 53'"
260
- q6 = "What is the model number?"
261
- question = PROMPT + q1 + "\n" + q2 + "\n" + q3 + "\n" + q4 + "\n" + q5 + "\n" + q6 + "\n" + all_text
262
- chat_completion = client.chat.completions.create(
263
- messages=[
264
- {
265
- "role": "user",
266
- "content": question,
267
- }
268
- ],
269
- model="gpt-3.5-turbo",
270
- )
271
- return chat_completion.choices[0].message.content
272
 
273
  #Accept a PDF file, return a text summary
274
  @app.post("/parsePdf")
275
  def parse_info(uploadFile: UploadFile):
276
  file = uploadFile.file.read()
 
277
  answer = parse_pdf_text(file)
278
- return {"answer": answer}
 
279
 
 
280
  app = gr.mount_gradio_app(app, demo, path="/")
281
 
282
  if __name__ == "__main__":
 
1
  import os
2
+ import logging
3
+ import json
4
 
5
  from fastapi import FastAPI, UploadFile
6
  from fastapi.responses import FileResponse, HTMLResponse, RedirectResponse
 
17
  from functools import partial
18
  from openai import OpenAI
19
 
20
+ PROMPT = "You are analyzing the spec sheet of a solar panel. Plese answer the following questions, format them as a JSON dictionary.\n"
21
 
22
  # from solareyes.sam import SAM
23
 
 
66
  # results: Results = model(image)
67
  # return results
68
 
69
+ def parse_pdf_text(file):
70
+ pdf = pdfium.PdfDocument(file)
71
+ all_text = "PDF Extract Text Contents Below: \n\n"
72
+ for page in pdf:
73
+ textpage = page.get_textpage()
74
+ text_all = textpage.get_text_bounded()
75
+ all_text += text_all
76
+
77
+ #use openai to ask questions about text
78
+ q1 = "What are module dimensions in L x W x H? Result key should be \"module_dimensions\""
79
+ q2 = "What is the module weight in kilograms? Result key should be \"module_weight\""
80
+ q3 = "What are the cable lengths in millimeters? Result key should be \"cable_length\""
81
+ q4 = "What brand, name, or model are the connectors? Result key should be \"connector\""
82
+ q5 = "How many pieces per container? Prefer 40' HQ or HC, if not available try 53' Result key should be \"pieces_per_container\""
83
+ q6 = "What is the model number? Result key should be \"model_number\""
84
+ question = PROMPT + q1 + "\n" + q2 + "\n" + q3 + "\n" + q4 + "\n" + q5 + "\n" + q6 + "\n" + all_text
85
+ chat_completion = client.chat.completions.create(
86
+ messages=[
87
+ {
88
+ "role": "user",
89
+ "content": question,
90
+ }
91
+ ],
92
+ model="gpt-3.5-turbo",
93
+ response_format={ "type": "json_object"}
94
+ )
95
+ return chat_completion.choices[0].message.content
96
+
97
 
98
  def segment_solar_panel(image) -> Results:
99
  # Perform inference
 
235
  image = process_pdf_core(pdf)
236
  return segment_image(image)
237
 
 
238
  with gr.Row():
239
  img_input = gr.Image(label="Upload Image", height=400)
240
  img_output = gr.AnnotatedImage(height=400)
 
250
 
251
  with gr.Row():
252
  pdf_input = gr.File(label="Upload PDF", file_types=['pdf'], height=200)
253
+ img_output_pdf = gr.AnnotatedImage(height=400)
254
+ with gr.Row():
255
+ text_input = gr.Textbox(label="Enter Text", placeholder=PROMPT)
256
+ text_output = gr.Textbox(label="Output", placeholder="Spec analysis will appear here")
257
 
258
  pdf_btn = gr.Button("Identify Solar Panel from PDF")
259
+ pdf_text_btn = gr.Button("Extract specs from PDF Text")
260
 
261
  gr.Examples(
262
  inputs = pdf_input,
263
  examples = [os.path.join(pdf_dir, file) for file in os.listdir(pdf_dir)],
264
  )
 
265
  section_btn.click(segment_image, [img_input], img_output)
266
+ pdf_btn.click(process_pdf, [pdf_input], img_output_pdf)
267
+ pdf_text_btn.click(parse_pdf_text, [pdf_input], text_output)
268
+
269
 
270
  #Accept a PDF file, return a jpeg image
271
  @app.post("/uploadPdf", response_class=FileResponse)
 
278
  jpeg_image.save(filename)
279
  return FileResponse(filename)
280
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
281
 
282
  #Accept a PDF file, return a text summary
283
  @app.post("/parsePdf")
284
  def parse_info(uploadFile: UploadFile):
285
  file = uploadFile.file.read()
286
+ logging.info(f"Received file {file}")
287
  answer = parse_pdf_text(file)
288
+ logging.info(f"Answer: {answer}")
289
+ return {"answer": json.loads(answer)}
290
 
291
+
292
  app = gr.mount_gradio_app(app, demo, path="/")
293
 
294
  if __name__ == "__main__":
client/run.js CHANGED
@@ -66,7 +66,8 @@ class PDFExtractor {
66
 
67
  // Example usage wrapped in an async IIFE
68
  (async () => {
69
- const converter = new PDFExtractor('http://localhost:7860');
 
70
  const pdfPath = '/workspaces/solar_eyes/pdf_downloads/0a0824f0-a5e7-4643-a834-0cea06f36c49.pdf';
71
  const outputPath = __dirname + '/outputImage2.jpg';
72
  await converter.convert(pdfPath, outputPath); // Ensure this is awaited if you need sequential execution
 
66
 
67
  // Example usage wrapped in an async IIFE
68
  (async () => {
69
+ // const converter = new PDFExtractor('http://localhost:7860');
70
+ const converter = new PDFExtractor();
71
  const pdfPath = '/workspaces/solar_eyes/pdf_downloads/0a0824f0-a5e7-4643-a834-0cea06f36c49.pdf';
72
  const outputPath = __dirname + '/outputImage2.jpg';
73
  await converter.convert(pdfPath, outputPath); // Ensure this is awaited if you need sequential execution
client/run.mjs CHANGED
@@ -67,7 +67,9 @@ class PDFExtractor {
67
  }
68
 
69
  // Usage example
70
- const extractor = new PDFExtractor('http://localhost:7860');
 
 
71
  const pdfPath = join(__dirname, '../pdf_downloads/0a0824f0-a5e7-4643-a834-0cea06f36c49.pdf');
72
  const outputPath = join(__dirname, 'outputImage.jpg');
73
  extractor.extract_panel_image(pdfPath, outputPath);
 
67
  }
68
 
69
  // Usage example
70
+ // const extractor = new PDFExtractor('http://localhost:7860');
71
+
72
+ const extractor = new PDFExtractor();
73
  const pdfPath = join(__dirname, '../pdf_downloads/0a0824f0-a5e7-4643-a834-0cea06f36c49.pdf');
74
  const outputPath = join(__dirname, 'outputImage.jpg');
75
  extractor.extract_panel_image(pdfPath, outputPath);