File size: 10,430 Bytes
583741e
 
 
 
 
 
 
 
 
 
 
78f6650
583741e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78f6650
583741e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78f6650
 
583741e
d3ff7fa
583741e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3ff7fa
 
 
583741e
d3ff7fa
 
 
583741e
d3ff7fa
583741e
d3ff7fa
 
 
 
 
 
 
 
 
 
583741e
 
 
 
 
 
 
d3ff7fa
 
 
583741e
d3ff7fa
 
 
583741e
d3ff7fa
583741e
d3ff7fa
 
 
 
 
 
 
 
 
 
583741e
 
 
 
 
78f6650
583741e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations
import os
import sys
import base64
import os
import json
import asyncio
from typing import Any, Dict, List, Optional
from pathlib import Path
from datetime import datetime

from anthropic import AsyncAnthropic
from anthropic.types import ToolUseBlock
from langgraph.graph import END, StateGraph
from pydantic import BaseModel, Field


from src.agents.prompt import REVIEWER_SYSTEM_PROMPT, EVALUATION_PROMPT_TEMPLATE, TOOLS, TOOL_CHOICE
from src.database import db
from src.config import config
from src.logger import logger


class ConversationState(BaseModel):
    """State for the conversation graph"""
    messages: List[Dict[str, Any]] = Field(default_factory=list)
    response_text: str = ""
    tool_result: Optional[Dict[str, Any]] = None
    arxiv_id: Optional[str] = None
    pdf_path: Optional[str] = None
    output_file: Optional[str] = None


def _load_pdf_as_content(pdf_path: str) -> Dict[str, Any]:
    if os.path.exists(pdf_path):
        with open(pdf_path, "rb") as f:
            data_b64 = base64.b64encode(f.read()).decode("utf-8")
        return {
            "type": "document",
            "source": {
                "type": "base64",
                "media_type": "application/pdf",
                "data": data_b64,
            },
        }
    if pdf_path.startswith("http"):
        return {
            "type": "document",
            "source": {
                "type": "url",
                "url": pdf_path,
            },
        }
    raise FileNotFoundError(f"PDF not found or invalid path: {pdf_path}")


class Evaluator:
    def __init__(self, api_key: Optional[str] = None):
        api_key = api_key or os.getenv("ANTHROPIC_API_KEY")
        if not api_key:
            raise ValueError("Anthropic API key is required. Please set HF_SECRET_ANTHROPIC_API_KEY in Hugging Face Spaces secrets or ANTHROPIC_API_KEY environment variable.")
        self.client = AsyncAnthropic(api_key=api_key)
        self.system_prompt = REVIEWER_SYSTEM_PROMPT
        self.eval_template = EVALUATION_PROMPT_TEMPLATE

    async def __call__(self, state: ConversationState) -> ConversationState:
        """Evaluate the paper using the conversation state"""
        # Prepare messages for the API call
        messages = []
        messages.extend(state.messages)
        
        # Load PDF content if pdf_path is provided
        if state.pdf_path:
            try:
                pdf_content = _load_pdf_as_content(state.pdf_path)
                messages.append({
                    "role": "user",
                    "content": [
                        {"type": "text", "text": "Please evaluate this academic paper:"},
                        pdf_content
                    ]
                })
            except Exception as e:
                state.response_text = f"Error loading PDF: {str(e)}"
                return state
        
        # Add the evaluation prompt
        messages.append({
            "role": "user", 
            "content": self.eval_template
        })
        
        try:
            # Call Anthropic API with tools (async)
            response = await self.client.messages.create(
                model=config.model_id,
                max_tokens=10000,
                system=self.system_prompt,
                messages=messages,
                tools=TOOLS,
                tool_choice=TOOL_CHOICE
            )
            
            # Process the response
            # Check if response is a tool use or text
            if response.content and isinstance(response.content[0], ToolUseBlock):
                # This is a tool use response
                tool_use = response.content[0]
                if tool_use:
                    tool_result = tool_use.input
                    
                    # set metadata
                    tool_result['metadata'] = {
                        'assessed_at': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
                        'model': config.model_id,
                        'version': config.version,
                        'paper_path': state.pdf_path
                    }
                    
                    state.tool_result = tool_result
                    state.response_text = json.dumps(tool_result, ensure_ascii=False, indent=4)
                    
                    # Add tool use to messages
                    state.messages.append({
                        "role": "assistant",
                        "content": f"Tool use: {tool_use.name}"
                    })
                else:
                    state.response_text = "Error: Tool use response but no tool_use found"
            else:
                # This is a text response
                text_content = response.content[0].text if response.content else ""
                state.messages.append({
                    "role": "assistant",
                    "content": text_content
                })
                state.response_text = text_content
                
        except Exception as e:
            state.response_text = f"Error during evaluation: {str(e)}"
        
        return state


async def save_node(state: ConversationState) -> ConversationState:
    """Save the evaluation result to database"""
    try:
        if not state.arxiv_id:
            state.response_text += f"\n\nError: No arxiv_id provided for database save"
            return state
        
        # Parse the evaluation result
        evaluation_content = state.response_text
        evaluation_score = None
        overall_score = None
        evaluation_tags = None
        
        # Try to extract score and tags from tool_result if available
        if state.tool_result:
            try:
                # Extract overall automatability score from scores
                if 'scores' in state.tool_result and 'overall_automatability' in state.tool_result['scores']:
                    evaluation_score = state.tool_result['scores']['overall_automatability']
                
                # Extract overall score from scores
                if 'scores' in state.tool_result and 'overall_automatability' in state.tool_result['scores']:
                    overall_score = state.tool_result['scores']['overall_automatability']
                
                # Create tags from key dimensions in scores
                tags = []
                if 'scores' in state.tool_result:
                    scores = state.tool_result['scores']
                    if 'three_year_feasibility_pct' in scores:
                        tags.append(f"3yr_feasibility:{scores['three_year_feasibility_pct']}%")
                    if 'task_formalization' in scores:
                        tags.append(f"task_formalization:{scores['task_formalization']}/4")
                    if 'data_resource_availability' in scores:
                        tags.append(f"data_availability:{scores['data_resource_availability']}/4")
                    
                    evaluation_tags = ",".join(tags) if tags else None
                
            except Exception as e:
                logger.warning(f"Warning: Could not extract structured data from tool_result: {e}")
        else:
            # Try to parse evaluation_content as JSON to extract structured data
            try:
                evaluation_json = json.loads(evaluation_content)
                # Extract overall automatability score from scores
                if 'scores' in evaluation_json and 'overall_automatability' in evaluation_json['scores']:
                    evaluation_score = evaluation_json['scores']['overall_automatability']
                
                # Extract overall score from scores
                if 'scores' in evaluation_json and 'overall_automatability' in evaluation_json['scores']:
                    overall_score = evaluation_json['scores']['overall_automatability']
                
                # Create tags from key dimensions in scores
                tags = []
                if 'scores' in evaluation_json:
                    scores = evaluation_json['scores']
                    if 'three_year_feasibility_pct' in scores:
                        tags.append(f"3yr_feasibility:{scores['three_year_feasibility_pct']}%")
                    if 'task_formalization' in scores:
                        tags.append(f"task_formalization:{scores['task_formalization']}/4")
                    if 'data_resource_availability' in scores:
                        tags.append(f"data_availability:{scores['data_resource_availability']}/4")
                    
                    evaluation_tags = ",".join(tags) if tags else None
                
            except Exception as e:
                logger.warning(f"Warning: Could not parse evaluation_content as JSON: {e}")
        
        # Save to database
        await db.update_paper_evaluation(
            arxiv_id=state.arxiv_id,
            evaluation_content=evaluation_content,
            evaluation_score=evaluation_score,
            overall_score=overall_score,
            evaluation_tags=evaluation_tags
        )
        
        state.response_text += f"\n\nEvaluation saved to database for paper: {state.arxiv_id}"
        
    except Exception as e:
        state.response_text += f"\n\nError saving evaluation to database: {str(e)}"
    
    return state


def build_graph(api_key: Optional[str] = None):
    """Build the evaluation graph"""
    graph = StateGraph(ConversationState)
    evaluator = Evaluator(api_key=api_key)
    graph.add_node("evaluate", evaluator)
    graph.add_node("save", save_node)
    
    # Define the flow
    graph.set_entry_point("evaluate")
    graph.add_edge("evaluate", "save")
    graph.add_edge("save", END)
    
    return graph.compile()


async def run_evaluation(pdf_path: str, arxiv_id: Optional[str] = None, output_file: Optional[str] = None, api_key: Optional[str] = None) -> str:
    app = build_graph(api_key=api_key)
    initial = ConversationState(pdf_path=pdf_path, arxiv_id=arxiv_id, output_file=output_file)
    # Ensure compatibility with LangGraph's dict-based state
    final_state = await app.ainvoke(initial.model_dump())
    if isinstance(final_state, dict):
        return str(final_state.get("response_text", ""))
    if isinstance(final_state, ConversationState):
        return final_state.response_text
    return str(getattr(final_state, "response_text", ""))