erayman09 commited on
Commit
a8da724
·
verified ·
1 Parent(s): b97a1bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -13
app.py CHANGED
@@ -3,8 +3,14 @@ from PIL import Image
3
  import pytesseract
4
  import pandas as pd
5
  import re
 
6
 
7
 
 
 
 
 
 
8
  def extract_text(image):
9
  """
10
  Extract text from the image using Tesseract.
@@ -12,18 +18,16 @@ def extract_text(image):
12
  return pytesseract.image_to_string(image)
13
 
14
 
 
15
  def clean_and_parse_extracted_text(raw_text):
16
  """
17
  Parse and clean the raw text to extract structured data.
18
  """
19
- # Split the text into lines and clean up
20
  lines = raw_text.split("\n")
21
  lines = [line.strip() for line in lines if line.strip()]
22
 
23
- # Identify and extract rows with valid components
24
  data = []
25
  for line in lines:
26
- # Match rows containing numeric ranges and values
27
  match = re.match(
28
  r"^(.*?)(\d+(\.\d+)?)(\s*-?\s*\d+(\.\d+)?\s*-?\s*\d+(\.\d+)?)?\s*([a-zA-Z/%]+)?\s*(H|L|Normal)?$",
29
  line,
@@ -42,21 +46,16 @@ def clean_and_parse_extracted_text(raw_text):
42
  unit = match.group(7)
43
  flag = "Normal" # Default flag
44
 
45
- # Determine the flag based on value and range
46
  if min_val is not None and max_val is not None:
47
  if value < min_val:
48
  flag = "L"
49
  elif value > max_val:
50
  flag = "H"
51
 
52
- # Only append the data if the flag is abnormal (L or H)
53
  if flag != "Normal":
54
  data.append([component, value, min_val, max_val, unit, flag])
55
 
56
- # Create a DataFrame
57
  df = pd.DataFrame(data, columns=["Component", "Your Value", "Min", "Max", "Units", "Flag"])
58
-
59
- # Fix misspellings and inconsistencies (if any known issues exist)
60
  correction_map = {
61
  "emoglobin": "Hemoglobin",
62
  "ematocrit": "Hematocrit",
@@ -69,16 +68,43 @@ def clean_and_parse_extracted_text(raw_text):
69
  return df
70
 
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  def display_results(df):
73
  """
74
- Display the flagged abnormalities in a table format.
75
  """
 
 
 
 
 
 
76
  st.dataframe(df, use_container_width=True)
 
 
 
77
 
78
 
79
  # Streamlit app
80
- st.title("Blood Report Analyzer")
81
- st.write("Upload an image of a blood test report to analyze.")
82
 
83
  uploaded_file = st.file_uploader("Upload Image", type=["png", "jpg", "jpeg"])
84
 
@@ -96,8 +122,7 @@ if uploaded_file is not None:
96
  # Parse the extracted text into a structured format
97
  parsed_data = clean_and_parse_extracted_text(extracted_text)
98
 
99
- # Display the structured data (only abnormalities)
100
- st.subheader("Flagged Abnormalities")
101
  display_results(parsed_data)
102
 
103
  except Exception as e:
 
3
  import pytesseract
4
  import pandas as pd
5
  import re
6
+ import openai
7
 
8
 
9
+ # Setup OpenAI API key (replace with your OpenAI API key)
10
+ openai.api_key = "your-api-key-here"
11
+
12
+
13
+ # Function to extract text from the image
14
  def extract_text(image):
15
  """
16
  Extract text from the image using Tesseract.
 
18
  return pytesseract.image_to_string(image)
19
 
20
 
21
+ # Function to parse and clean the extracted text
22
  def clean_and_parse_extracted_text(raw_text):
23
  """
24
  Parse and clean the raw text to extract structured data.
25
  """
 
26
  lines = raw_text.split("\n")
27
  lines = [line.strip() for line in lines if line.strip()]
28
 
 
29
  data = []
30
  for line in lines:
 
31
  match = re.match(
32
  r"^(.*?)(\d+(\.\d+)?)(\s*-?\s*\d+(\.\d+)?\s*-?\s*\d+(\.\d+)?)?\s*([a-zA-Z/%]+)?\s*(H|L|Normal)?$",
33
  line,
 
46
  unit = match.group(7)
47
  flag = "Normal" # Default flag
48
 
 
49
  if min_val is not None and max_val is not None:
50
  if value < min_val:
51
  flag = "L"
52
  elif value > max_val:
53
  flag = "H"
54
 
 
55
  if flag != "Normal":
56
  data.append([component, value, min_val, max_val, unit, flag])
57
 
 
58
  df = pd.DataFrame(data, columns=["Component", "Your Value", "Min", "Max", "Units", "Flag"])
 
 
59
  correction_map = {
60
  "emoglobin": "Hemoglobin",
61
  "ematocrit": "Hematocrit",
 
68
  return df
69
 
70
 
71
+ # Function to generate AI-powered recommendations using OpenAI GPT
72
+ def generate_medical_recommendation(test_results):
73
+ """
74
+ Generate medical recommendations using OpenAI GPT model based on abnormal test results.
75
+ """
76
+ # Create a structured input for the model
77
+ prompt = f"Given the following blood test results: {test_results}, provide medical recommendations for a patient."
78
+
79
+ response = openai.Completion.create(
80
+ model="gpt-4", # Use GPT-4 for medical-based responses
81
+ prompt=prompt,
82
+ max_tokens=150
83
+ )
84
+
85
+ return response.choices[0].text.strip()
86
+
87
+
88
+ # Function to display results
89
  def display_results(df):
90
  """
91
+ Display the flagged abnormalities and medical recommendations in a table format.
92
  """
93
+ # Generate a summary of the abnormal test results for the AI model
94
+ abnormal_results = df[df['Flag'] != 'Normal'].to_string(index=False)
95
+ recommendation = generate_medical_recommendation(abnormal_results)
96
+
97
+ # Display the DataFrame and the recommendation
98
+ st.subheader("Flagged Abnormalities")
99
  st.dataframe(df, use_container_width=True)
100
+
101
+ st.subheader("Medical Recommendations from AI")
102
+ st.write(recommendation)
103
 
104
 
105
  # Streamlit app
106
+ st.title("Blood Report Analyzer with AI Recommendations")
107
+ st.write("Upload an image of a blood test report to analyze and get AI-powered recommendations.")
108
 
109
  uploaded_file = st.file_uploader("Upload Image", type=["png", "jpg", "jpeg"])
110
 
 
122
  # Parse the extracted text into a structured format
123
  parsed_data = clean_and_parse_extracted_text(extracted_text)
124
 
125
+ # Display the structured data with AI recommendations
 
126
  display_results(parsed_data)
127
 
128
  except Exception as e: