Spaces:
Sleeping
Sleeping
Update Lab_report_analysis.py
Browse files- Lab_report_analysis.py +72 -72
Lab_report_analysis.py
CHANGED
|
@@ -1,72 +1,72 @@
|
|
| 1 |
-
import base64
|
| 2 |
-
import os
|
| 3 |
-
from huggingface_hub import InferenceClient
|
| 4 |
-
from tkinter import Tk, filedialog
|
| 5 |
-
|
| 6 |
-
def select_image():
|
| 7 |
-
|
| 8 |
-
root = Tk()
|
| 9 |
-
root.withdraw()
|
| 10 |
-
file_path = filedialog.askopenfilename(
|
| 11 |
-
title="Select a Lab Report Image",
|
| 12 |
-
filetypes=[("Image Files", "*.jpg;*.jpeg;*.png;*.bmp;*.tiff;*.webp")]
|
| 13 |
-
)
|
| 14 |
-
return file_path
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
client = InferenceClient(
|
| 18 |
-
provider="nebius",
|
| 19 |
-
api_key=os.getenv("HUGGINGFACE_API_KEY", "your-api-key-here"),
|
| 20 |
-
)
|
| 21 |
-
img = select_image()
|
| 22 |
-
|
| 23 |
-
with open(img, "rb") as f:
|
| 24 |
-
image_bytes = f.read()
|
| 25 |
-
image_b64 = base64.b64encode(image_bytes).decode("utf-8")
|
| 26 |
-
|
| 27 |
-
prompt = """
|
| 28 |
-
You are a medical analysis assistant.
|
| 29 |
-
|
| 30 |
-
Analyze the following lab report image and give a structured, professional summary
|
| 31 |
-
following these steps:
|
| 32 |
-
|
| 33 |
-
1. Extract the results (with normal ranges if available).
|
| 34 |
-
2. Highlight abnormal values clearly.
|
| 35 |
-
3. Explain what the results suggest in simple terms.
|
| 36 |
-
4. Provide an overall summary of health findings.
|
| 37 |
-
5. End with the disclaimer:
|
| 38 |
-
"This analysis is for educational purposes only and should not replace professional medical advice."
|
| 39 |
-
|
| 40 |
-
If the image is unreadable, respond: "The image text is unclear."
|
| 41 |
-
"""
|
| 42 |
-
|
| 43 |
-
completion = client.chat.completions.create(
|
| 44 |
-
model="google/gemma-
|
| 45 |
-
messages=[
|
| 46 |
-
{
|
| 47 |
-
"role": "user",
|
| 48 |
-
"content": [
|
| 49 |
-
{"type": "text", "text": """
|
| 50 |
-
Analyze this lab report and give me a brief, structured summary.
|
| 51 |
-
|
| 52 |
-
Format your response as follows:
|
| 53 |
-
|
| 54 |
-
Summary: (2–3 sentences explaining what the report shows)
|
| 55 |
-
Key Findings: (3–5 bullet points with main abnormal or notable values)
|
| 56 |
-
Interpretation: (1–2 sentences explaining what the findings suggest)
|
| 57 |
-
Note: (One line disclaimer that it’s not medical advice)
|
| 58 |
-
|
| 59 |
-
Keep it short, clear, and professional — like a medical summary written for quick review.
|
| 60 |
-
"""},
|
| 61 |
-
{
|
| 62 |
-
"type": "image_url",
|
| 63 |
-
"image_url": {
|
| 64 |
-
"url": f"data:image/jpeg;base64,{image_b64}"
|
| 65 |
-
}
|
| 66 |
-
}
|
| 67 |
-
]
|
| 68 |
-
}
|
| 69 |
-
],
|
| 70 |
-
)
|
| 71 |
-
|
| 72 |
-
print(completion.choices[0].message.content.strip())
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import os
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
from tkinter import Tk, filedialog
|
| 5 |
+
|
| 6 |
+
def select_image():
|
| 7 |
+
|
| 8 |
+
root = Tk()
|
| 9 |
+
root.withdraw()
|
| 10 |
+
file_path = filedialog.askopenfilename(
|
| 11 |
+
title="Select a Lab Report Image",
|
| 12 |
+
filetypes=[("Image Files", "*.jpg;*.jpeg;*.png;*.bmp;*.tiff;*.webp")]
|
| 13 |
+
)
|
| 14 |
+
return file_path
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
client = InferenceClient(
|
| 18 |
+
provider="nebius",
|
| 19 |
+
api_key=os.getenv("HUGGINGFACE_API_KEY", "your-api-key-here"),
|
| 20 |
+
)
|
| 21 |
+
img = select_image()
|
| 22 |
+
|
| 23 |
+
with open(img, "rb") as f:
|
| 24 |
+
image_bytes = f.read()
|
| 25 |
+
image_b64 = base64.b64encode(image_bytes).decode("utf-8")
|
| 26 |
+
|
| 27 |
+
prompt = """
|
| 28 |
+
You are a medical analysis assistant.
|
| 29 |
+
|
| 30 |
+
Analyze the following lab report image and give a structured, professional summary
|
| 31 |
+
following these steps:
|
| 32 |
+
|
| 33 |
+
1. Extract the results (with normal ranges if available).
|
| 34 |
+
2. Highlight abnormal values clearly.
|
| 35 |
+
3. Explain what the results suggest in simple terms.
|
| 36 |
+
4. Provide an overall summary of health findings.
|
| 37 |
+
5. End with the disclaimer:
|
| 38 |
+
"This analysis is for educational purposes only and should not replace professional medical advice."
|
| 39 |
+
|
| 40 |
+
If the image is unreadable, respond: "The image text is unclear."
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
completion = client.chat.completions.create(
|
| 44 |
+
model="google/gemma-2b-it",
|
| 45 |
+
messages=[
|
| 46 |
+
{
|
| 47 |
+
"role": "user",
|
| 48 |
+
"content": [
|
| 49 |
+
{"type": "text", "text": """
|
| 50 |
+
Analyze this lab report and give me a brief, structured summary.
|
| 51 |
+
|
| 52 |
+
Format your response as follows:
|
| 53 |
+
|
| 54 |
+
Summary: (2–3 sentences explaining what the report shows)
|
| 55 |
+
Key Findings: (3–5 bullet points with main abnormal or notable values)
|
| 56 |
+
Interpretation: (1–2 sentences explaining what the findings suggest)
|
| 57 |
+
Note: (One line disclaimer that it’s not medical advice)
|
| 58 |
+
|
| 59 |
+
Keep it short, clear, and professional — like a medical summary written for quick review.
|
| 60 |
+
"""},
|
| 61 |
+
{
|
| 62 |
+
"type": "image_url",
|
| 63 |
+
"image_url": {
|
| 64 |
+
"url": f"data:image/jpeg;base64,{image_b64}"
|
| 65 |
+
}
|
| 66 |
+
}
|
| 67 |
+
]
|
| 68 |
+
}
|
| 69 |
+
],
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
print(completion.choices[0].message.content.strip())
|