samwjya commited on
Commit
76f8bb5
·
1 Parent(s): 483f7b5
Files changed (1) hide show
  1. api.py +105 -0
api.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ from dotenv import load_dotenv
4
+ load_dotenv()
5
+
6
+ from fastapi import FastAPI, HTTPException
7
+ from pydantic import BaseModel
8
+
9
+ from retriever import fetch_papers
10
+ from reranker import load_model, rerank, AVAILABLE_MODELS, DEFAULT_MODEL
11
+
12
+ app = FastAPI(title="PaperRank API")
13
+
14
+ _model_cache: dict = {}
15
+
16
+
17
+ def _get_model(model_name: str):
18
+ if model_name not in _model_cache:
19
+ _model_cache[model_name] = load_model(model_name)
20
+ return _model_cache[model_name]
21
+
22
+
23
+ class SearchRequest(BaseModel):
24
+ query: str
25
+ limit: int = 50
26
+ model_name: str = DEFAULT_MODEL
27
+
28
+
29
+ class SummarizeRequest(BaseModel):
30
+ title: str
31
+ abstract: str
32
+
33
+
34
+ class ResearchSummaryRequest(BaseModel):
35
+ query: str
36
+ papers: list[dict]
37
+
38
+
39
+ @app.post("/search")
40
+ def search(req: SearchRequest):
41
+ if req.model_name not in AVAILABLE_MODELS:
42
+ raise HTTPException(
43
+ status_code=400,
44
+ detail=f"Unknown model. Choose from: {AVAILABLE_MODELS}",
45
+ )
46
+ try:
47
+ papers = fetch_papers(req.query, limit=req.limit)
48
+ except ValueError as e:
49
+ raise HTTPException(status_code=404, detail=str(e))
50
+ except Exception as e:
51
+ raise HTTPException(status_code=502, detail=f"OpenAlex fetch failed: {e}")
52
+ model = _get_model(req.model_name)
53
+ reranked = rerank(req.query, papers, model)
54
+ return {"papers": reranked}
55
+
56
+
57
+ @app.post("/summarize")
58
+ def summarize(req: SummarizeRequest):
59
+ from groq import Groq
60
+ api_key = os.getenv("GROQ_API_KEY")
61
+ if not api_key:
62
+ raise HTTPException(status_code=500, detail="GROQ_API_KEY not configured")
63
+ prompt = (
64
+ "Summarize this academic paper in 2-3 sentences focusing "
65
+ "on the key contribution and findings:\n"
66
+ f"Title: {req.title}\n"
67
+ f"Abstract: {req.abstract}"
68
+ )
69
+ client = Groq(api_key=api_key)
70
+ resp = client.chat.completions.create(
71
+ model="llama-3.3-70b-versatile",
72
+ messages=[{"role": "user", "content": prompt}],
73
+ max_tokens=150,
74
+ )
75
+ return {"summary": resp.choices[0].message.content}
76
+
77
+
78
+ @app.post("/research-summary")
79
+ def research_summary(req: ResearchSummaryRequest):
80
+ from groq import Groq
81
+ api_key = os.getenv("GROQ_API_KEY")
82
+ if not api_key:
83
+ raise HTTPException(status_code=500, detail="GROQ_API_KEY not configured")
84
+ papers_text = "\n".join(
85
+ f"{i}. {p.get('title', 'N/A')}: {p.get('abstract') or 'No abstract available'}"
86
+ for i, p in enumerate(req.papers, 1)
87
+ )
88
+ prompt = (
89
+ f"Based on these top 5 papers retrieved for the query: {req.query}\n\n"
90
+ f"Papers:\n{papers_text}\n\n"
91
+ "Write a concise 150 word synthesis of the key themes and "
92
+ "findings relevant to the query. Focus on what the papers "
93
+ "collectively say about the topic."
94
+ )
95
+ client = Groq(api_key=api_key)
96
+ resp = client.chat.completions.create(
97
+ model="llama-3.3-70b-versatile",
98
+ messages=[{"role": "user", "content": prompt}],
99
+ max_tokens=300,
100
+ )
101
+ return {"summary": resp.choices[0].message.content}
102
+
103
+ @app.get("/health")
104
+ def health_check():
105
+ return {"status": "ok"}