Spaces:
Runtime error
Runtime error
Pranit commited on
Commit ·
3f35fb7
1
Parent(s): be8aeda
- .gitignore +3 -1
- app.py +38 -6
.gitignore
CHANGED
|
@@ -3,4 +3,6 @@ __pycache__/
|
|
| 3 |
.env
|
| 4 |
.venv
|
| 5 |
venv/
|
| 6 |
-
ENV/
|
|
|
|
|
|
|
|
|
| 3 |
.env
|
| 4 |
.venv
|
| 5 |
venv/
|
| 6 |
+
ENV/
|
| 7 |
+
*.log
|
| 8 |
+
.DS_Store
|
app.py
CHANGED
|
@@ -12,15 +12,25 @@ from dotenv import load_dotenv
|
|
| 12 |
|
| 13 |
app = Flask(__name__)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
# Configure logging
|
| 16 |
logging.basicConfig(level=logging.DEBUG)
|
| 17 |
logger = logging.getLogger(__name__)
|
| 18 |
|
| 19 |
-
# Configure Gemini API
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Updated prompt for dual-format report
|
| 26 |
prompt = """You are a professional campus facility inspector with over 15 years of experience in infrastructure assessment in India. Analyze the campus with total area of ${college_area} acres. Generate two reports based on the provided campus images:
|
|
@@ -268,6 +278,9 @@ def extract_maintenance_status(report_text):
|
|
| 268 |
@app.route('/generate_report', methods=['POST'])
|
| 269 |
def generate_report():
|
| 270 |
try:
|
|
|
|
|
|
|
|
|
|
| 271 |
data = request.json
|
| 272 |
images = data.get('images', [])
|
| 273 |
basic_info = data.get('basicInfo', {})
|
|
@@ -352,9 +365,12 @@ def generate_report():
|
|
| 352 |
} for img in image_contexts]
|
| 353 |
})
|
| 354 |
|
|
|
|
|
|
|
|
|
|
| 355 |
except Exception as e:
|
| 356 |
logger.error(f"Error generating report: {str(e)}")
|
| 357 |
-
return jsonify({'error':
|
| 358 |
|
| 359 |
@app.route('/download_pdf', methods=['POST'])
|
| 360 |
def download_pdf():
|
|
@@ -429,5 +445,21 @@ def generate_report(findings, inspector_name, location, weather):
|
|
| 429 |
|
| 430 |
return html_content
|
| 431 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
if __name__ == '__main__':
|
| 433 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 12 |
|
| 13 |
app = Flask(__name__)
|
| 14 |
|
| 15 |
+
# Load environment variables
|
| 16 |
+
load_dotenv()
|
| 17 |
+
|
| 18 |
# Configure logging
|
| 19 |
logging.basicConfig(level=logging.DEBUG)
|
| 20 |
logger = logging.getLogger(__name__)
|
| 21 |
|
| 22 |
+
# Configure Gemini API with error handling
|
| 23 |
+
api_key = os.getenv('GOOGLE_API_KEY')
|
| 24 |
+
if not api_key:
|
| 25 |
+
logger.error("No Google API key found. Please set GOOGLE_API_KEY environment variable.")
|
| 26 |
+
raise ValueError("GOOGLE_API_KEY environment variable is required")
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
genai.configure(api_key=api_key)
|
| 30 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 31 |
+
except Exception as e:
|
| 32 |
+
logger.error(f"Failed to configure Gemini API: {str(e)}")
|
| 33 |
+
raise
|
| 34 |
|
| 35 |
# Updated prompt for dual-format report
|
| 36 |
prompt = """You are a professional campus facility inspector with over 15 years of experience in infrastructure assessment in India. Analyze the campus with total area of ${college_area} acres. Generate two reports based on the provided campus images:
|
|
|
|
| 278 |
@app.route('/generate_report', methods=['POST'])
|
| 279 |
def generate_report():
|
| 280 |
try:
|
| 281 |
+
if not api_key:
|
| 282 |
+
raise ValueError("Google API key not configured")
|
| 283 |
+
|
| 284 |
data = request.json
|
| 285 |
images = data.get('images', [])
|
| 286 |
basic_info = data.get('basicInfo', {})
|
|
|
|
| 365 |
} for img in image_contexts]
|
| 366 |
})
|
| 367 |
|
| 368 |
+
except ValueError as ve:
|
| 369 |
+
logger.error(f"Validation error: {str(ve)}")
|
| 370 |
+
return jsonify({'error': str(ve)}), 400
|
| 371 |
except Exception as e:
|
| 372 |
logger.error(f"Error generating report: {str(e)}")
|
| 373 |
+
return jsonify({'error': 'Internal server error occurred'}), 500
|
| 374 |
|
| 375 |
@app.route('/download_pdf', methods=['POST'])
|
| 376 |
def download_pdf():
|
|
|
|
| 445 |
|
| 446 |
return html_content
|
| 447 |
|
| 448 |
+
@app.route('/health')
|
| 449 |
+
def health_check():
|
| 450 |
+
try:
|
| 451 |
+
# Simple test to verify API connection
|
| 452 |
+
response = model.generate_content("Test connection")
|
| 453 |
+
return jsonify({
|
| 454 |
+
'status': 'healthy',
|
| 455 |
+
'api_configured': True
|
| 456 |
+
})
|
| 457 |
+
except Exception as e:
|
| 458 |
+
logger.error(f"Health check failed: {str(e)}")
|
| 459 |
+
return jsonify({
|
| 460 |
+
'status': 'unhealthy',
|
| 461 |
+
'error': str(e)
|
| 462 |
+
}), 500
|
| 463 |
+
|
| 464 |
if __name__ == '__main__':
|
| 465 |
app.run(host='0.0.0.0', port=7860)
|