sikeaditya commited on
Commit
c098cd6
·
verified ·
1 Parent(s): 0dde85b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -14
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 # add this import at the top
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 content for 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,21 +145,22 @@ def index():
145
  Please note any limitations in your analysis if the image quality or content is unclear.
146
  """
147
 
148
- # Initialize Gemini client
149
- client = genai.Client(api_key="AIzaSyArihOGcyK5KcQR4ntIqNga6bSoq7kM7Yo")
150
 
151
  logging.debug("Calling Gemini API for content generation.")
152
- response = client.models.generate_content(
153
  model="gemini-2.0-flash",
154
- contents=prompt
155
  )
156
 
157
- analysis_text = response.text
 
158
  analysis_html = markdown.markdown(analysis_text) # convert markdown to HTML
159
 
160
  result = {
161
- 'analysis': analysis_text, # original text (if needed)
162
- 'analysis_html': analysis_html, # HTML version for rendering
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.")