| from flask import Flask, request, jsonify |
| from flask_cors import CORS |
| import google.generativeai as genai |
| import os |
| import re |
|
|
| app = Flask(__name__) |
| CORS(app) |
|
|
| |
| GEMINI_API_KEY = "AIzaSyCKKdpQ5Fuvbgjlr_tUFejUn0AWrA99BP0" |
| genai.configure(api_key=GEMINI_API_KEY) |
|
|
| |
| model = genai.GenerativeModel('gemini-1.5-flash') |
|
|
| |
| KNOWLEDGE_BASE = """ |
| You are a helpful support chatbot for OncoConnect, a pathology research platform. Answer questions based on this information: |
| |
| ONBOARDING & ACCOUNTS: |
| - Upload pathology images β get cancer/chemo-response indications and auto-matched trials |
| - Create account: Hit Log In β email or OAuth (Google/GitHub). Verify email |
| - Roles: Researcher, Clinician, Student, Admin |
| - Edit profile: Profile β Edit β update bio, avatar, institution, tags β Save |
| - Change notifications: Profile β Settings β Notifications |
| - Delete account: Profile β Settings β Privacy β Delete account |
| |
| PATHOLOGY ANALYZER: |
| - File types: PNG/JPEG for demo; WSI via SVS/TIFF/OME-TIFF |
| - Max size: PNG/JPEG β€ 25MB; WSI β€ 2GB |
| - Anonymize slides: Remove PHI, crop labels, strip metadata |
| - "Cancer: Yes/No" with confidence (0-100%) - exploratory only, not diagnostic |
| - Saved cases: Analyzer β Saved Cases β rename/delete with β― menu |
| |
| CLINICAL TRIALS: |
| - Trials appear automatically after analysis |
| - Filter by phase, status, distance, biomarkers |
| - Demo data refreshes weekly |
| - Contact trials: Open trial β Site details β email/phone |
| - Export matched trials: Export CSV from Trials panel |
| |
| SCORES & RISK: |
| - Risk score (0-100): Research composite risk, exploration only |
| - NOT a diagnostic tool - research/education only |
| - Model explanations via saliency heatmap |
| - Cite as: OncoConnect (version X), research prototype, non-diagnostic |
| |
| CHALLENGES: |
| - View: Go to Challenges; filter by cancer type/skill |
| - Enroll: Open challenge β Enroll |
| - Submit: Upload artifact (CSV/zip/notebook) |
| - Teams: Create/join teams; invite collaborators |
| - Leaderboard shows top-10 + your position |
| |
| LEADERBOARD & PROFILES: |
| - Monthly rankings, solved challenges, average scores, badges |
| - Connect with others via profiles |
| - Filter by specialty/cancer type |
| |
| DATA & PRIVACY: |
| - Demo: browser local storage; Enterprise: encrypted at rest |
| - Private by default; can mark Public/Team |
| - NO PHI allowed - de-identified data only |
| - NOT FDA-cleared - research/education only |
| - Free demo tier with limits; academic access available |
| |
| Keep answers concise and helpful. If unsure, suggest contacting support. |
| """ |
|
|
| def get_response(user_message): |
| try: |
| |
| prompt = f"{KNOWLEDGE_BASE}\n\nUser question: {user_message}\n\nProvide a helpful, concise answer:" |
| |
| |
| response = model.generate_content(prompt) |
| |
| |
| answer = response.text.strip() |
| |
| |
| if len(answer) > 500: |
| answer = answer[:500] + "..." |
| |
| return answer |
| |
| except Exception as e: |
| print(f"Error generating response: {e}") |
| return "I apologize, but I'm having trouble processing your request right now. Please try asking again or contact support for assistance." |
|
|
| @app.route('/chat', methods=['POST']) |
| def chat(): |
| try: |
| data = request.get_json() |
| user_message = data.get('message', '').strip() |
| |
| if not user_message: |
| return jsonify({'error': 'No message provided'}), 400 |
| |
| |
| response = get_response(user_message) |
| |
| return jsonify({'response': response}) |
| |
| except Exception as e: |
| print(f"Error in chat endpoint: {e}") |
| return jsonify({'error': 'Internal server error'}), 500 |
|
|
| @app.route('/health', methods=['GET']) |
| def health(): |
| return jsonify({'status': 'healthy'}) |
|
|
| if __name__ == '__main__': |
| print("π€ OncoConnect Support Chatbot starting...") |
| print("π± Frontend: Open any page in your browser") |
| print("π Backend: Running on http://localhost:5000") |
| print("π¬ Chatbot: Available on all pages as floating button") |
| |
| app.run(debug=True, host='0.0.0.0', port=5000) |
|
|