File size: 4,283 Bytes
390cffd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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)

# Configure Gemini API
GEMINI_API_KEY = "AIzaSyCKKdpQ5Fuvbgjlr_tUFejUn0AWrA99BP0"
genai.configure(api_key=GEMINI_API_KEY)

# Initialize the model
model = genai.GenerativeModel('gemini-1.5-flash')

# OncoConnect knowledge base (from your document)
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:
        # Create the prompt with context
        prompt = f"{KNOWLEDGE_BASE}\n\nUser question: {user_message}\n\nProvide a helpful, concise answer:"
       
        # Generate response using Gemini
        response = model.generate_content(prompt)
       
        # Clean up the response
        answer = response.text.strip()
       
        # Limit response length
        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
       
        # Generate response
        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)