erayman09 commited on
Commit
efbd192
·
verified ·
1 Parent(s): dc200ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -65
app.py CHANGED
@@ -1,129 +1,105 @@
1
- import streamlit as st
2
- from PIL import Image
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 = "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"
11
 
12
 
13
- # Function to extract text from the image
 
 
 
14
  def extract_text(image):
15
  """
16
  Extract text from the image using Tesseract.
17
- """
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,
34
- )
35
- if match:
36
- component = match.group(1).strip()
37
- value = float(match.group(2))
38
- range_match = match.group(4)
39
- if range_match:
40
- ranges = re.findall(r"[\d.]+", range_match)
41
- min_val = float(ranges[0]) if len(ranges) > 0 else None
42
- max_val = float(ranges[1]) if len(ranges) > 1 else None
43
- else:
44
- min_val = None
45
- max_val = None
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",
62
- "% Platelet Count": "Platelet Count",
63
- "ymphocyte %": "Lymphocyte %",
64
- "L Differential Type Automated": "Differential Type",
65
- }
66
- df["Component"] = df["Component"].replace(correction_map)
67
-
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
 
111
- if uploaded_file is not None:
112
- try:
113
- # Load the image
114
- image = Image.open(uploaded_file)
115
 
116
- # Display the uploaded image
117
- st.image(image, caption="Uploaded Image", use_container_width=True)
 
118
 
119
- # Extract text from the image
120
- extracted_text = extract_text(image)
121
 
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:
129
- st.error(f"An error occurred: {e}")
 
 
 
1
  import pytesseract
2
  import pandas as pd
3
  import re
 
4
 
5
 
 
 
6
 
7
 
8
+
9
+
10
+
11
+
12
  def extract_text(image):
13
  """
14
  Extract text from the image using Tesseract.
 
15
  return pytesseract.image_to_string(image)
16
 
17
 
18
+
19
  def clean_and_parse_extracted_text(raw_text):
20
  """
21
  Parse and clean the raw text to extract structured data.
22
  """
23
+ # Split the text into lines and clean up
24
  lines = raw_text.split("\n")
25
  lines = [line.strip() for line in lines if line.strip()]
26
 
27
+ # Identify and extract rows with valid components
28
  data = []
29
  for line in lines:
30
+ # Match rows containing numeric ranges and values
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,
 
 
 
 
 
 
 
 
 
 
 
 
34
  unit = match.group(7)
35
  flag = "Normal" # Default flag
36
 
37
+ # Determine the flag based on value and range
38
  if min_val is not None and max_val is not None:
39
  if value < min_val:
40
  flag = "L"
41
  elif value > max_val:
42
  flag = "H"
43
 
44
+ # Only append the data if the flag is abnormal (L or H)
45
  if flag != "Normal":
46
  data.append([component, value, min_val, max_val, unit, flag])
47
 
48
+ # Create a DataFrame
49
  df = pd.DataFrame(data, columns=["Component", "Your Value", "Min", "Max", "Units", "Flag"])
50
+
51
+ # Fix misspellings and inconsistencies (if any known issues exist)
52
  correction_map = {
53
  "emoglobin": "Hemoglobin",
54
  "ematocrit": "Hematocrit",
 
 
 
 
 
 
55
  return df
56
 
57
 
 
 
 
 
 
 
 
58
 
 
 
 
 
 
59
 
 
60
 
61
 
62
+
63
+
64
+
65
+
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
  def display_results(df):
77
  """
78
+ Display the flagged abnormalities in a table format.
79
  """
80
+
81
+
82
+
83
+
84
+
85
+
86
  st.dataframe(df, use_container_width=True)
 
 
 
87
 
88
 
 
 
 
89
 
 
90
 
 
 
 
 
91
 
92
+ # Streamlit app
93
+ st.title("Blood Report Analyzer")
94
+ st.write("Upload an image of a blood test report to analyze.")
95
 
96
+ uploaded_file = st.file_uploader("Upload Image", type=["png", "jpg", "jpeg"])
 
97
 
98
  # Parse the extracted text into a structured format
99
  parsed_data = clean_and_parse_extracted_text(extracted_text)
100
 
101
+ # Display the structured data (only abnormalities)
102
+ st.subheader("Flagged Abnormalities")
103
  display_results(parsed_data)
104
 
105
+ except Exception as e: