File size: 8,413 Bytes
4a04295
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import os
import io
import json
import base64
from fastapi import FastAPI, HTTPException, Header, Depends
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List, Optional
import anthropic
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload

app = FastAPI(title="Dr. Gini DocRAG Service")

# CORS - Allow your frontend domains
app.add_middleware(
    CORSMiddleware,
    allow_origins=[
        "https://your-frontend.netlify.app",
        "https://your-space.hf.space",
        "http://localhost:3000",
        "http://localhost:5173",
        "*"  # Remove in production, use specific domains
    ],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Simple API key auth (optional but recommended)
API_KEY = os.environ.get("DOCRAG_API_KEY", "")

def verify_api_key(x_api_key: str = Header(None, alias="X-API-Key")):
    """Verify API key if configured"""
    if API_KEY and x_api_key != API_KEY:
        raise HTTPException(status_code=401, detail="Invalid API key")
    return True

# Initialize Claude client
claude_client = anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))

# Google Drive Service Account
def get_drive_service():
    """Initialize Google Drive service with service account"""
    service_account_info = json.loads(os.environ.get("GOOGLE_SERVICE_ACCOUNT", "{}"))
    
    if not service_account_info:
        raise HTTPException(status_code=500, detail="Google Service Account not configured")
    
    credentials = service_account.Credentials.from_service_account_info(
        service_account_info,
        scopes=['https://www.googleapis.com/auth/drive.readonly']
    )
    
    return build('drive', 'v3', credentials=credentials)


# ============ Request/Response Models ============

class Document(BaseModel):
    driveFileId: str
    fileName: str
    mimeType: str


class DocRAGRequest(BaseModel):
    userId: str
    sessionId: str
    query: str
    selectedDocs: List[Document]


class DocRAGResponse(BaseModel):
    success: bool
    query: str
    answer: Optional[str] = None
    documentsUsed: List[str] = []
    error: Optional[str] = None


# ============ Helper Functions ============

def download_from_drive(drive_service, file_id: str, file_name: str) -> bytes:
    """Download file from Google Drive using service account"""
    try:
        request = drive_service.files().get_media(fileId=file_id)
        
        file_buffer = io.BytesIO()
        downloader = MediaIoBaseDownload(file_buffer, request)
        
        done = False
        while not done:
            status, done = downloader.next_chunk()
        
        file_buffer.seek(0)
        return file_buffer.read()
        
    except Exception as e:
        raise HTTPException(
            status_code=400,
            detail=f"Failed to download {file_name}: {str(e)}"
        )


def get_claude_media_type(mime_type: str) -> tuple[str, str]:
    """Map MIME type to Claude's supported types"""
    
    if mime_type == "application/pdf":
        return "document", "application/pdf"
    
    if mime_type in ["image/jpeg", "image/png", "image/gif", "image/webp"]:
        return "image", mime_type
    
    if mime_type in ["text/plain", "text/csv", "text/html", "text/markdown",
                     "application/vnd.openxmlformats-officedocument.wordprocessingml.document"]:
        return "text", mime_type
    
    return "document", "application/pdf"


# ============ API Endpoints ============

@app.post("/docrag", response_model=DocRAGResponse)
async def chat_with_documents(
    request: DocRAGRequest,
    authenticated: bool = Depends(verify_api_key)
):
    """Chat with uploaded documents using Claude"""
    
    try:
        drive_service = get_drive_service()
        
        # Download all selected documents
        documents_content = []
        documents_used = []
        
        for doc in request.selectedDocs:
            try:
                file_bytes = download_from_drive(
                    drive_service, 
                    doc.driveFileId, 
                    doc.fileName
                )
                documents_content.append({
                    "fileName": doc.fileName,
                    "content": file_bytes,
                    "mimeType": doc.mimeType
                })
                documents_used.append(doc.fileName)
                print(f"✓ Downloaded: {doc.fileName}")
                
            except Exception as e:
                print(f"✗ Error downloading {doc.fileName}: {e}")
                continue
        
        if not documents_content:
            return DocRAGResponse(
                success=False,
                query=request.query,
                error="Could not download any documents. Check if folder is shared with service account."
            )
        
        # Build Claude message
        content = []
        
        for doc in documents_content:
            content_type, media_type = get_claude_media_type(doc["mimeType"])
            
            if content_type == "document":
                content.append({
                    "type": "document",
                    "source": {
                        "type": "base64",
                        "media_type": media_type,
                        "data": base64.b64encode(doc["content"]).decode("utf-8")
                    }
                })
                
            elif content_type == "image":
                content.append({
                    "type": "image",
                    "source": {
                        "type": "base64",
                        "media_type": media_type,
                        "data": base64.b64encode(doc["content"]).decode("utf-8")
                    }
                })
                
            else:
                try:
                    text_content = doc["content"].decode("utf-8")
                    content.append({
                        "type": "text",
                        "text": f"=== Document: {doc['fileName']} ===\n\n{text_content}\n\n=== End ==="
                    })
                except UnicodeDecodeError:
                    continue
        
        # Add query
        content.append({
            "type": "text",
            "text": request.query
        })
        
        # Call Claude
        response = claude_client.messages.create(
            model="claude-sonnet-4-20250514",
            max_tokens=4096,
            system="""You are Dr. Gini, a research copilot for drug discovery and pharmaceutical research.

When answering:
- Be precise and cite specific sections when relevant
- If information is not in the documents, say so clearly
- For multiple documents, compare and synthesize across them
- Use scientific terminology appropriately
- Highlight key findings, methods, and limitations""",
            messages=[{"role": "user", "content": content}]
        )
        
        return DocRAGResponse(
            success=True,
            query=request.query,
            answer=response.content[0].text,
            documentsUsed=documents_used
        )
        
    except anthropic.APIError as e:
        return DocRAGResponse(
            success=False,
            query=request.query,
            error=f"Claude API error: {str(e)}"
        )
    except Exception as e:
        import traceback
        traceback.print_exc()
        return DocRAGResponse(
            success=False,
            query=request.query,
            error=f"Error: {str(e)}"
        )


@app.get("/health")
async def health_check():
    return {"status": "healthy", "service": "Dr. Gini DocRAG"}


@app.get("/test-drive")
async def test_drive_connection():
    """Test Google Drive connection"""
    try:
        drive_service = get_drive_service()
        results = drive_service.files().list(
            pageSize=5,
            fields="files(id, name)"
        ).execute()
        
        files = results.get('files', [])
        return {
            "status": "connected",
            "files_visible": len(files),
            "sample_files": [f["name"] for f in files[:5]]
        }
    except Exception as e:
        return {"status": "error", "error": str(e)}


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)