Ziad Meligy commited on
Commit
34332a8
·
1 Parent(s): 4a58046

adding apicall

Browse files
Files changed (3) hide show
  1. api.py +33 -0
  2. generate_report.py +7 -3
  3. utils.py +23 -0
api.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+
4
+ # Environment variable for the token
5
+ token = "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"
6
+ endpoint = "https://models.github.ai/inference"
7
+ model = "openai/gpt-4.1"
8
+
9
+ def API_call(report):
10
+ headers = {
11
+ "Authorization": f"Bearer {token}",
12
+ "Content-Type": "application/json"
13
+ }
14
+
15
+ body = {
16
+ "messages": [
17
+ {"role": "system", "content": ""},
18
+ {"role": "user", "content": f"please structure this report into findings and impressions. Please be precise and just output the findings and impressions with no other text.{report}"}
19
+ ],
20
+ "temperature": 1,
21
+ "top_p": 1,
22
+ "model": model
23
+ }
24
+
25
+ response = requests.post(f"{endpoint}/chat/completions", headers=headers, json=body)
26
+
27
+ if not response.ok:
28
+ raise Exception(response.json().get("error", "Unknown error"))
29
+
30
+ data = response.json()
31
+ return(data["choices"][0]["message"]["content"])
32
+
33
+
generate_report.py CHANGED
@@ -6,9 +6,10 @@ import numpy as np
6
  from CNN_encoder import CNN_Encoder
7
  from distil_gpt2 import DistilGPT2
8
  from configs import argHandler
9
- from utils import load_image
10
  from tokenizer_wrapper import TokenizerWrapper
11
  from huggingface_hub import hf_hub_download
 
12
  # from src.models.cnn_encoder import
13
  # from src.models.distil_gpt2 import DistilGPT2
14
  # from src.configs import argHandler
@@ -91,5 +92,8 @@ def generate_report(image_bytes):
91
  sentence = tokenizer_wrapper.GPT2_decode(tokens[0])
92
  sentence = tokenizer_wrapper.filter_special_words(sentence)
93
  print(sentence)
94
-
95
- return {"report": sentence}
 
 
 
 
6
  from CNN_encoder import CNN_Encoder
7
  from distil_gpt2 import DistilGPT2
8
  from configs import argHandler
9
+ from utils import load_image, split_report_sections
10
  from tokenizer_wrapper import TokenizerWrapper
11
  from huggingface_hub import hf_hub_download
12
+ from api import API_call
13
  # from src.models.cnn_encoder import
14
  # from src.models.distil_gpt2 import DistilGPT2
15
  # from src.configs import argHandler
 
92
  sentence = tokenizer_wrapper.GPT2_decode(tokens[0])
93
  sentence = tokenizer_wrapper.filter_special_words(sentence)
94
  print(sentence)
95
+ # Call the API to structure the report
96
+ structured_report = API_call(sentence)
97
+ print(structured_report)
98
+ structured_report =split_report_sections(structured_report)
99
+ return structured_report
utils.py CHANGED
@@ -5,7 +5,30 @@ import pydicom
5
  import torch
6
  from fastapi import HTTPException, UploadFile
7
  from skimage.transform import resize
 
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  def load_image(image):
11
  image = image.convert("RGB")
 
5
  import torch
6
  from fastapi import HTTPException, UploadFile
7
  from skimage.transform import resize
8
+ import re
9
 
10
+ def clean_paragraph(text):
11
+ # Remove leading dashes, numbers, and extra whitespace
12
+ lines = text.strip().splitlines()
13
+ cleaned_lines = [re.sub(r"^\s*[-•\d.]*\s*", "", line) for line in lines if line.strip()]
14
+ return " ".join(cleaned_lines)
15
+
16
+ def split_report_sections(report_text):
17
+ #strip any * in any place in the report
18
+ report_text = report_text.replace("*", "")
19
+ # Use regex to extract findings and impression sections
20
+ findings_match = re.search(r"(?i)findings:\s*(.*?)(?=(impressions?:))", report_text, re.DOTALL)
21
+ impression_match = re.search(r"(?i)impressions?:\s*(.*)", report_text, re.DOTALL)
22
+
23
+ findings_raw = findings_match.group(1).strip() if findings_match else ""
24
+ impression = impression_match.group(1).strip() if impression_match else ""
25
+
26
+ findings = clean_paragraph(findings_raw)
27
+
28
+ return {
29
+ "findings": findings,
30
+ "impression": impression # Keep impression formatting as-is (or you can also clean it similarly)
31
+ }
32
 
33
  def load_image(image):
34
  image = image.convert("RGB")