Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ import io
|
|
| 8 |
import base64
|
| 9 |
import google.generativeai as genai
|
| 10 |
import pytesseract # OCR library for extracting text from images
|
| 11 |
-
import markdown #
|
| 12 |
|
| 13 |
app = Flask(__name__)
|
| 14 |
app.secret_key = 'your_secret_key_here'
|
|
@@ -97,7 +97,7 @@ def index():
|
|
| 97 |
logging.debug(f"Saving file to {file_path}")
|
| 98 |
file.save(file_path)
|
| 99 |
|
| 100 |
-
# Extract text
|
| 101 |
text = ""
|
| 102 |
with open(file_path, "rb") as pdf_file:
|
| 103 |
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
|
@@ -107,7 +107,7 @@ def index():
|
|
| 107 |
text += page_text
|
| 108 |
logging.debug(f"Extracted text from page {page_num}")
|
| 109 |
|
| 110 |
-
# Handle image upload
|
| 111 |
image_text = ""
|
| 112 |
if 'image' in request.files:
|
| 113 |
image_file = request.files['image']
|
|
@@ -124,15 +124,15 @@ def index():
|
|
| 124 |
# Determine scan type
|
| 125 |
scan_type = get_scan_type(combined_text)
|
| 126 |
|
| 127 |
-
# Extract images
|
| 128 |
images_data = extract_images_from_pdf(file_path)
|
| 129 |
image_count = len(images_data)
|
| 130 |
logging.debug(f"Extracted {image_count} images from PDF")
|
| 131 |
|
| 132 |
-
# Construct the
|
| 133 |
prompt = f"""
|
| 134 |
You are a professional medical imaging specialist analyzing a {scan_type} scan report.
|
| 135 |
-
Please provide a comprehensive analysis of this report just tell directly about following points no intro
|
| 136 |
1. Key findings and observations
|
| 137 |
2. Any significant abnormalities or concerns
|
| 138 |
3. Technical quality of the scan
|
|
@@ -145,21 +145,22 @@ def index():
|
|
| 145 |
Please note any limitations in your analysis if the image quality or content is unclear.
|
| 146 |
"""
|
| 147 |
|
| 148 |
-
#
|
| 149 |
-
|
| 150 |
|
| 151 |
logging.debug("Calling Gemini API for content generation.")
|
| 152 |
-
response =
|
| 153 |
model="gemini-2.0-flash",
|
| 154 |
-
|
| 155 |
)
|
| 156 |
|
| 157 |
-
|
|
|
|
| 158 |
analysis_html = markdown.markdown(analysis_text) # convert markdown to HTML
|
| 159 |
|
| 160 |
result = {
|
| 161 |
-
'analysis': analysis_text,
|
| 162 |
-
'analysis_html': analysis_html,
|
| 163 |
'scan_type': scan_type,
|
| 164 |
'image_count': image_count,
|
| 165 |
'images': images_data
|
|
@@ -171,7 +172,7 @@ def index():
|
|
| 171 |
flash(f"Error processing file: {str(e)}", 'error')
|
| 172 |
return redirect(request.url)
|
| 173 |
finally:
|
| 174 |
-
# Clean up
|
| 175 |
if os.path.exists(file_path):
|
| 176 |
os.remove(file_path)
|
| 177 |
logging.debug("Temporary file removed after processing.")
|
|
|
|
| 8 |
import base64
|
| 9 |
import google.generativeai as genai
|
| 10 |
import pytesseract # OCR library for extracting text from images
|
| 11 |
+
import markdown # Convert markdown to HTML
|
| 12 |
|
| 13 |
app = Flask(__name__)
|
| 14 |
app.secret_key = 'your_secret_key_here'
|
|
|
|
| 97 |
logging.debug(f"Saving file to {file_path}")
|
| 98 |
file.save(file_path)
|
| 99 |
|
| 100 |
+
# Extract text from PDF
|
| 101 |
text = ""
|
| 102 |
with open(file_path, "rb") as pdf_file:
|
| 103 |
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
|
|
|
| 107 |
text += page_text
|
| 108 |
logging.debug(f"Extracted text from page {page_num}")
|
| 109 |
|
| 110 |
+
# Handle image upload (if provided)
|
| 111 |
image_text = ""
|
| 112 |
if 'image' in request.files:
|
| 113 |
image_file = request.files['image']
|
|
|
|
| 124 |
# Determine scan type
|
| 125 |
scan_type = get_scan_type(combined_text)
|
| 126 |
|
| 127 |
+
# Extract images from PDF
|
| 128 |
images_data = extract_images_from_pdf(file_path)
|
| 129 |
image_count = len(images_data)
|
| 130 |
logging.debug(f"Extracted {image_count} images from PDF")
|
| 131 |
|
| 132 |
+
# Construct the prompt for the Gemini API
|
| 133 |
prompt = f"""
|
| 134 |
You are a professional medical imaging specialist analyzing a {scan_type} scan report.
|
| 135 |
+
Please provide a comprehensive analysis of this report just tell directly about following points no intro, including:
|
| 136 |
1. Key findings and observations
|
| 137 |
2. Any significant abnormalities or concerns
|
| 138 |
3. Technical quality of the scan
|
|
|
|
| 145 |
Please note any limitations in your analysis if the image quality or content is unclear.
|
| 146 |
"""
|
| 147 |
|
| 148 |
+
# Configure the Gemini API key
|
| 149 |
+
genai.configure(api_key="AIzaSyArihOGcyK5KcQR4ntIqNga6bSoq7kM7Yo")
|
| 150 |
|
| 151 |
logging.debug("Calling Gemini API for content generation.")
|
| 152 |
+
response = genai.generate_text(
|
| 153 |
model="gemini-2.0-flash",
|
| 154 |
+
prompt=prompt
|
| 155 |
)
|
| 156 |
|
| 157 |
+
# Access the generated content (adjust attribute as needed; here we use 'result')
|
| 158 |
+
analysis_text = response.result
|
| 159 |
analysis_html = markdown.markdown(analysis_text) # convert markdown to HTML
|
| 160 |
|
| 161 |
result = {
|
| 162 |
+
'analysis': analysis_text,
|
| 163 |
+
'analysis_html': analysis_html,
|
| 164 |
'scan_type': scan_type,
|
| 165 |
'image_count': image_count,
|
| 166 |
'images': images_data
|
|
|
|
| 172 |
flash(f"Error processing file: {str(e)}", 'error')
|
| 173 |
return redirect(request.url)
|
| 174 |
finally:
|
| 175 |
+
# Clean up the uploaded file
|
| 176 |
if os.path.exists(file_path):
|
| 177 |
os.remove(file_path)
|
| 178 |
logging.debug("Temporary file removed after processing.")
|