File size: 6,646 Bytes
e3994d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d293d8
 
 
e3994d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d293d8
 
e3994d1
 
 
 
 
 
1d293d8
e3994d1
 
1d293d8
 
 
 
 
e3994d1
 
 
 
 
 
 
 
 
 
 
 
 
 
aec9ccf
 
 
 
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
import os
import shutil
import tempfile
from typing import List, Dict, Any, Optional
from fastapi import FastAPI, File, UploadFile, HTTPException, Body
from fastapi.staticfiles import StaticFiles
from fastapi.responses import JSONResponse, FileResponse
from pydantic import BaseModel

from src.ingestion import ingest_paper
from src.indexer import load_index
from src.pipeline import ask_question
from src.intelligence import (
    summarize_paper, 
    detect_contradictions, 
    generate_comparison_table, 
    generate_literature_review,
    generate_hypotheses
)
from src.utils import UnifiedIndex, PaperResult

app = FastAPI(title="ResearchLens Premium API")

# --- Global State (For a local single-user app) ---
GLOBAL_STATE = {
    "unified_indices": {},   # paper_id -> UnifiedIndex
    "paper_results": {},     # paper_id -> PaperResult
}

# --- Pydantic Models ---
class ChatRequest(BaseModel):
    query: str
    history: List[Dict[str, str]] = []

class SummarizeRequest(BaseModel):
    paper_id: str

class IntelligenceRequest(BaseModel):
    action: str  # "compare", "contradictions", "review", "hypotheses"

# --- API Endpoints ---

@app.post("/api/upload")
async def upload_pdf(file: UploadFile = File(...)):
    """Uploads a PDF, processes it, and adds it to the knowledge base."""
    if not file.filename.endswith(".pdf"):
        raise HTTPException(status_code=400, detail="File must be a PDF")
        
    try:
        # Save to temp and ensure it is fully closed before ingestion
        tmp_path = ""
        with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
            content = await file.read()
            tmp.write(content)
            tmp_path = tmp.name
            
        # Ingest
        result = ingest_paper(tmp_path)
        os.unlink(tmp_path)
        
        if not result:
            raise HTTPException(status_code=400, detail="Could not extract meaningful text from PDF.")
            
        # Load index
        unified, _, _ = load_index(result.paper_id)
        
        # Store in global state
        GLOBAL_STATE["unified_indices"][result.paper_id] = unified
        GLOBAL_STATE["paper_results"][result.paper_id] = result
        
        return {
            "success": True,
            "paper_id": result.paper_id,
            "title": result.metadata.title,
            "year": result.metadata.year,
            "n_pages": result.metadata.n_pages
        }
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/api/papers")
def list_papers():
    """List all loaded papers."""
    papers = []
    for pid, pr in GLOBAL_STATE["paper_results"].items():
        papers.append({
            "paper_id": pid,
            "title": pr.metadata.title,
            "year": pr.metadata.year
        })
    return {"papers": papers}

@app.post("/api/chat")
def chat(req: ChatRequest):
    """Ask a question across all papers."""
    indices = list(GLOBAL_STATE["unified_indices"].values())
    if not indices:
        return {"answer": "Please upload papers first."}
        
    try:
        answer = ask_question(req.query, indices, req.history)
        return {"answer": answer}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/api/summarize")
def summarize(req: SummarizeRequest):
    """Generate a detailed summary for a specific paper."""
    if req.paper_id not in GLOBAL_STATE["paper_results"]:
        raise HTTPException(status_code=404, detail="Paper not found.")
        
    pr = GLOBAL_STATE["paper_results"][req.paper_id]
    try:
        summary = summarize_paper(pr)
        return {
            "title": summary.title,
            "contribution": summary.contribution,
            "methodology": summary.methodology,
            "results": summary.results,
            "datasets": summary.datasets,
            "limitations": summary.limitations
        }
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/api/intelligence")
def run_intelligence(req: IntelligenceRequest):
    """Run cross-paper analytics."""
    prs = list(GLOBAL_STATE["paper_results"].values())
    if not prs:
        raise HTTPException(status_code=400, detail="Please upload at least 1 paper first.")
    if len(prs) < 2 and req.action != "hypotheses":
        raise HTTPException(status_code=400, detail="Please upload at least 2 papers for cross-paper intelligence.")
        
    try:
        if req.action == "compare":
            rows = generate_comparison_table(prs)
            result = [{"dimension": r.dimension, "values": r.values} for r in rows]
            return {"type": "table", "data": result}
            
        elif req.action == "contradictions":
            contradictions = detect_contradictions(prs)
            result = [{
                "paper_a": c.paper_a,
                "paper_b": c.paper_b,
                "claim_a": c.claim_a,
                "claim_b": c.claim_b,
                "explanation": c.explanation
            } for c in contradictions]
            return {"type": "contradictions", "data": result}
            
        elif req.action == "review":
            review = generate_literature_review(prs)
            return {"type": "text", "data": review}
            
        elif req.action == "hypotheses":
            hypotheses = generate_hypotheses(prs)
            return {"type": "text", "data": hypotheses}
            
        else:
            raise HTTPException(status_code=400, detail="Unknown action")
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/api/clear")
def clear_memory():
    """Clear all loaded papers and their disk indices."""
    GLOBAL_STATE["unified_indices"].clear()
    GLOBAL_STATE["paper_results"].clear()
    # Clean up disk indices to free storage
    indices_dir = "data/indices"
    if os.path.exists(indices_dir):
        shutil.rmtree(indices_dir)
        os.makedirs(indices_dir, exist_ok=True)
    return {"success": True}


# --- Frontend Serving ---
# Ensure frontend directory exists
os.makedirs("frontend", exist_ok=True)
app.mount("/static", StaticFiles(directory="frontend"), name="static")

@app.get("/")
def serve_frontend():
    return FileResponse("frontend/index.html")

if __name__ == "__main__":
    import uvicorn
    port = int(os.environ.get("PORT", 8000))
    # Disable reload in production (when PORT is set via PaaS)
    reload = os.environ.get("PORT") is None
    uvicorn.run("src.server:app", host="0.0.0.0", port=port, reload=reload)