ABAO77 commited on
Commit
532ef46
·
1 Parent(s): 930ec8d

update: file app

Browse files
Files changed (1) hide show
  1. app.py +49 -12
app.py CHANGED
@@ -6,7 +6,7 @@ from fastapi.middleware.cors import CORSMiddleware
6
  import cv2
7
  import numpy as np
8
  from src.config.llm import llm
9
- from src.prompt.promt import format_prompt
10
  from langchain_core.output_parsers import JsonOutputParser
11
  import uvicorn
12
  from io import BytesIO
@@ -18,6 +18,8 @@ import os
18
  import functools
19
  import threading
20
  from src.inference.segment_inference import inference
 
 
21
  load_dotenv()
22
  app = FastAPI(docs_url="/")
23
  app.add_middleware(
@@ -28,6 +30,7 @@ app.add_middleware(
28
  allow_headers=["*"],
29
  )
30
  executor = ThreadPoolExecutor(max_workers=int(os.cpu_count() + 4))
 
31
 
32
 
33
  def run_in_thread(func, *args, **kwargs):
@@ -43,7 +46,6 @@ def run_in_thread(func, *args, **kwargs):
43
 
44
 
45
  def predict_func(threshold_confidence, threshold_iou, image):
46
-
47
  image = np.frombuffer(image, np.uint8)
48
  image = cv2.imdecode(image, cv2.IMREAD_COLOR)
49
  outputs = inference(
@@ -52,11 +54,19 @@ def predict_func(threshold_confidence, threshold_iou, image):
52
  threshold_iou=threshold_iou,
53
  )
54
  text = extract_text(outputs=outputs, image_origin=image)
55
- image = draw_bounding_boxes(image, outputs)
 
 
 
 
 
 
 
 
 
56
  buffer = BytesIO()
57
- image.save(buffer, format="JPEG")
58
  buffer.seek(0)
59
-
60
  image_base64 = base64.b64encode(buffer.getvalue()).decode("utf-8")
61
  response = {"outputs": text, "image_base64": image_base64}
62
  return response
@@ -82,22 +92,49 @@ async def predict(
82
 
83
  class LLMRequest(BaseModel):
84
  text: str = Field(..., title="Text to generate completion")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
 
87
- def call_llm(data):
88
- input = format_prompt.format(input=data)
89
- response = llm.invoke(input)
90
- response = JsonOutputParser().parse(response)
 
 
 
 
91
  return response
92
 
93
 
94
- @app.post("/llm", status_code=status.HTTP_200_OK)
95
- async def llm_predict(data: LLMRequest):
 
 
 
96
  try:
97
- response = await run_in_thread(call_llm, data.text)
98
  return JSONResponse(content=response, status_code=status.HTTP_200_OK)
99
  except Exception as e:
100
  response = {"error": str(e)}
 
101
  return JSONResponse(content=response, status_code=status.HTTP_400_BAD_REQUEST)
102
 
103
 
 
6
  import cv2
7
  import numpy as np
8
  from src.config.llm import llm
9
+ from src.prompt.promt import format_prompt, matching_jd_prompt
10
  from langchain_core.output_parsers import JsonOutputParser
11
  import uvicorn
12
  from io import BytesIO
 
18
  import functools
19
  import threading
20
  from src.inference.segment_inference import inference
21
+ from PIL import Image
22
+
23
  load_dotenv()
24
  app = FastAPI(docs_url="/")
25
  app.add_middleware(
 
30
  allow_headers=["*"],
31
  )
32
  executor = ThreadPoolExecutor(max_workers=int(os.cpu_count() + 4))
33
+ parser = JsonOutputParser()
34
 
35
 
36
  def run_in_thread(func, *args, **kwargs):
 
46
 
47
 
48
  def predict_func(threshold_confidence, threshold_iou, image):
 
49
  image = np.frombuffer(image, np.uint8)
50
  image = cv2.imdecode(image, cv2.IMREAD_COLOR)
51
  outputs = inference(
 
54
  threshold_iou=threshold_iou,
55
  )
56
  text = extract_text(outputs=outputs, image_origin=image)
57
+ image_with_boxes = draw_bounding_boxes(image, outputs)
58
+ if isinstance(image_with_boxes, np.ndarray):
59
+ image_rgb = cv2.cvtColor(image_with_boxes, cv2.COLOR_BGR2RGB)
60
+ image_pil = Image.fromarray(image_rgb)
61
+ elif isinstance(image_with_boxes, Image.Image):
62
+ image_pil = image_with_boxes
63
+ else:
64
+ raise TypeError(f"Unsupported image type: {type(image_with_boxes)}")
65
+
66
+ # Encode image to base64
67
  buffer = BytesIO()
68
+ image_pil.save(buffer, format="JPEG")
69
  buffer.seek(0)
 
70
  image_base64 = base64.b64encode(buffer.getvalue()).decode("utf-8")
71
  response = {"outputs": text, "image_base64": image_base64}
72
  return response
 
92
 
93
  class LLMRequest(BaseModel):
94
  text: str = Field(..., title="Text to generate completion")
95
+ job_desciption: str = Field(
96
+ default=None, title="Job Description to match with resume"
97
+ )
98
+
99
+
100
+ def reformat_fn(data):
101
+ chain = format_prompt | llm | parser
102
+ response = chain.invoke({"user_input": data})
103
+ return response
104
+
105
+
106
+ @app.post("/reformat_output", status_code=status.HTTP_200_OK)
107
+ async def reformat_output(data: LLMRequest):
108
+ try:
109
+ response = await run_in_thread(reformat_fn, data.text)
110
+ return JSONResponse(content=response, status_code=status.HTTP_200_OK)
111
+ except Exception as e:
112
+ response = {"error": str(e)}
113
+ return JSONResponse(content=response, status_code=status.HTTP_400_BAD_REQUEST)
114
 
115
 
116
+ def matching_job_desciption_fn(data: LLMRequest):
117
+ job_description = data.job_desciption
118
+ resume_input = data.text
119
+ chain = matching_jd_prompt | llm | parser
120
+ response = chain.invoke(
121
+ {"job_description": job_description, "resume_input": resume_input}
122
+ )
123
+ print(response)
124
  return response
125
 
126
 
127
+ @app.post("/matching_job_desciption", status_code=status.HTTP_200_OK)
128
+ async def matching_job_desciption(data: LLMRequest):
129
+ if data.job_desciption is None:
130
+ response = {"error": "Job Description is required"}
131
+ return JSONResponse(content=response, status_code=status.HTTP_400_BAD_REQUEST)
132
  try:
133
+ response = await run_in_thread(matching_job_desciption_fn, data)
134
  return JSONResponse(content=response, status_code=status.HTTP_200_OK)
135
  except Exception as e:
136
  response = {"error": str(e)}
137
+ print(response)
138
  return JSONResponse(content=response, status_code=status.HTTP_400_BAD_REQUEST)
139
 
140