Spaces:
Sleeping
Sleeping
added claude support + annotations display
Browse files
app.py
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import ast
|
| 4 |
-
import openai
|
| 5 |
-
from openai import OpenAI
|
| 6 |
import PyPDF2
|
| 7 |
from PyPDF2 import PdfReader
|
| 8 |
import openreview
|
|
@@ -11,9 +9,27 @@ import urllib.request
|
|
| 11 |
import requests
|
| 12 |
import os
|
| 13 |
import pickle
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Check if the password matches the env variable
|
| 19 |
def check_password():
|
|
@@ -100,7 +116,7 @@ def parse_openreview_id(submissions, paper_id, reviewer_id):
|
|
| 100 |
return review_content, pdf, pdf_text
|
| 101 |
|
| 102 |
|
| 103 |
-
def create_feedback(review, pdf_text, agent_prompt):
|
| 104 |
|
| 105 |
messages = [{
|
| 106 |
"role": "user",
|
|
@@ -127,28 +143,33 @@ def create_feedback(review, pdf_text, agent_prompt):
|
|
| 127 |
}
|
| 128 |
]}]
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
feedback =
|
| 144 |
-
except Exception as e:
|
| 145 |
-
print(f"An unexpected error occurred: {e}")
|
| 146 |
-
feedback = "an error occured"
|
| 147 |
|
| 148 |
return feedback
|
| 149 |
|
| 150 |
|
| 151 |
-
def critic(review, feedback, pdf_text, critic_prompt):
|
| 152 |
|
| 153 |
messages = [{
|
| 154 |
"role": "user",
|
|
@@ -183,23 +204,28 @@ def critic(review, feedback, pdf_text, critic_prompt):
|
|
| 183 |
}
|
| 184 |
]}]
|
| 185 |
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
revised_feedback =
|
| 200 |
-
except Exception as e:
|
| 201 |
-
print(f"An unexpected error occurred: {e}")
|
| 202 |
-
revised_feedback = "an error occured"
|
| 203 |
|
| 204 |
return revised_feedback
|
| 205 |
|
|
@@ -385,6 +411,7 @@ if option == "OpenReview paper ID":
|
|
| 385 |
paper_id = paper_id.strip()
|
| 386 |
reviewer_id = st.text_input("Enter reviewer ID (e.g. gNxe):")
|
| 387 |
reviewer_id = reviewer_id.strip()
|
|
|
|
| 388 |
if paper_id and reviewer_id:
|
| 389 |
upload_file = False
|
| 390 |
user_input = True
|
|
@@ -395,6 +422,7 @@ if option == "OpenReview paper ID":
|
|
| 395 |
elif option == "Upload PDF with Review":
|
| 396 |
user_text = st.text_area("Enter review:")
|
| 397 |
uploaded_file = st.file_uploader("Upload PDF", type="pdf")
|
|
|
|
| 398 |
if user_text and uploaded_file:
|
| 399 |
upload_file = True
|
| 400 |
user_input = True
|
|
@@ -406,34 +434,68 @@ elif option == "Upload PDF with Review":
|
|
| 406 |
# st.write(f"Uploaded file: {uploaded_file.name}")
|
| 407 |
|
| 408 |
if user_input:
|
|
|
|
|
|
|
| 409 |
# Run your pipeline to generate the dataframe based on user input
|
| 410 |
-
feedback = create_feedback(review, pdf_text, agent_prompt)
|
| 411 |
|
| 412 |
-
revised_feedback = critic(review, feedback, pdf_text, critic_prompt)
|
| 413 |
|
| 414 |
st.title(f'Review feedback')
|
| 415 |
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 437 |
|
| 438 |
else:
|
| 439 |
st.title('Please enter OpenReview ID or upload PDF with review to generate feedback')
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import ast
|
|
|
|
|
|
|
| 4 |
import PyPDF2
|
| 5 |
from PyPDF2 import PdfReader
|
| 6 |
import openreview
|
|
|
|
| 9 |
import requests
|
| 10 |
import os
|
| 11 |
import pickle
|
| 12 |
+
import openai
|
| 13 |
+
from openai import OpenAI
|
| 14 |
+
import anthropic
|
| 15 |
+
from anthropic import Anthropic
|
| 16 |
|
| 17 |
+
openai_api_key = os.environ["OPENAI_API_KEY"]
|
| 18 |
+
client_openai = OpenAI(api_key=openai_api_key)
|
| 19 |
+
|
| 20 |
+
anthropic_api_key = os.environ["ANTHROPIC_API_KEY"]
|
| 21 |
+
client_anthropic = Anthropic(api_key=anthropic_api_key)
|
| 22 |
+
|
| 23 |
+
# load manual (human) annotations
|
| 24 |
+
def load_annotations():
|
| 25 |
+
path = './annotations_8_26.csv'
|
| 26 |
+
annotations = pd.read_csv(path)
|
| 27 |
+
annotations.columns = annotations.columns.str.replace(' ', '_').str.lower()
|
| 28 |
+
|
| 29 |
+
# 36 0, 15 1
|
| 30 |
+
annotations['annotation_label'] = annotations['annotation_label'].astype(int)
|
| 31 |
+
|
| 32 |
+
return annotations
|
| 33 |
|
| 34 |
# Check if the password matches the env variable
|
| 35 |
def check_password():
|
|
|
|
| 116 |
return review_content, pdf, pdf_text
|
| 117 |
|
| 118 |
|
| 119 |
+
def create_feedback(review, pdf_text, agent_prompt, model):
|
| 120 |
|
| 121 |
messages = [{
|
| 122 |
"role": "user",
|
|
|
|
| 143 |
}
|
| 144 |
]}]
|
| 145 |
|
| 146 |
+
if model == 'gpt':
|
| 147 |
+
headers = {
|
| 148 |
+
"Content-Type": "application/json",
|
| 149 |
+
"Authorization": f"Bearer {openai_api_key}"
|
| 150 |
+
}
|
| 151 |
+
payload = {
|
| 152 |
+
"model": "gpt-4o",
|
| 153 |
+
"messages": messages,
|
| 154 |
+
"max_tokens": 1000,
|
| 155 |
+
"temperature": 0.2
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
try:
|
| 159 |
+
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
| 160 |
+
feedback = response.json()["choices"][0]["message"]["content"]
|
| 161 |
+
except Exception as e:
|
| 162 |
+
print(f"An unexpected error occurred: {e}")
|
| 163 |
+
feedback = "an error occured"
|
| 164 |
|
| 165 |
+
else:
|
| 166 |
+
message = client_anthropic.messages.create(max_tokens=1024, messages=messages, model="claude-3-5-sonnet-20240620")
|
| 167 |
+
feedback = message.content[0].text
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
return feedback
|
| 170 |
|
| 171 |
|
| 172 |
+
def critic(review, feedback, pdf_text, critic_prompt, model):
|
| 173 |
|
| 174 |
messages = [{
|
| 175 |
"role": "user",
|
|
|
|
| 204 |
}
|
| 205 |
]}]
|
| 206 |
|
| 207 |
+
if model == 'gpt':
|
| 208 |
+
headers = {
|
| 209 |
+
"Content-Type": "application/json",
|
| 210 |
+
"Authorization": f"Bearer {openai_api_key}"
|
| 211 |
+
}
|
| 212 |
+
payload = {
|
| 213 |
+
"model": "gpt-4o",
|
| 214 |
+
"messages": messages,
|
| 215 |
+
"max_tokens": 1000,
|
| 216 |
+
"temperature": 0.2
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
try:
|
| 220 |
+
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
| 221 |
+
revised_feedback = response.json()["choices"][0]["message"]["content"]
|
| 222 |
+
except Exception as e:
|
| 223 |
+
print(f"An unexpected error occurred: {e}")
|
| 224 |
+
revised_feedback = "an error occured"
|
| 225 |
|
| 226 |
+
else:
|
| 227 |
+
message = client_anthropic.messages.create(max_tokens=1024, messages=messages, model="claude-3-5-sonnet-20240620")
|
| 228 |
+
revised_feedback = message.content[0].text
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
return revised_feedback
|
| 231 |
|
|
|
|
| 411 |
paper_id = paper_id.strip()
|
| 412 |
reviewer_id = st.text_input("Enter reviewer ID (e.g. gNxe):")
|
| 413 |
reviewer_id = reviewer_id.strip()
|
| 414 |
+
model = st.text_input("Which model? ('GPT' or 'Claude')")
|
| 415 |
if paper_id and reviewer_id:
|
| 416 |
upload_file = False
|
| 417 |
user_input = True
|
|
|
|
| 422 |
elif option == "Upload PDF with Review":
|
| 423 |
user_text = st.text_area("Enter review:")
|
| 424 |
uploaded_file = st.file_uploader("Upload PDF", type="pdf")
|
| 425 |
+
model = st.text_input("Which model? ('GPT' or 'Claude')")
|
| 426 |
if user_text and uploaded_file:
|
| 427 |
upload_file = True
|
| 428 |
user_input = True
|
|
|
|
| 434 |
# st.write(f"Uploaded file: {uploaded_file.name}")
|
| 435 |
|
| 436 |
if user_input:
|
| 437 |
+
annotations = load_annotations()
|
| 438 |
+
|
| 439 |
# Run your pipeline to generate the dataframe based on user input
|
| 440 |
+
feedback = create_feedback(review, pdf_text, agent_prompt, model)
|
| 441 |
|
| 442 |
+
revised_feedback = critic(review, feedback, pdf_text, critic_prompt, model)
|
| 443 |
|
| 444 |
st.title(f'Review feedback')
|
| 445 |
|
| 446 |
+
if paper_id in annotations['submission_id']:
|
| 447 |
+
# Create four columns
|
| 448 |
+
col1, col2, col3, col4 = st.columns(4)
|
| 449 |
+
|
| 450 |
+
# Column 1: Display review fields
|
| 451 |
+
with col1:
|
| 452 |
+
st.subheader("Review")
|
| 453 |
+
st.write(json.dumps(review))
|
| 454 |
+
|
| 455 |
+
if not upload_file:
|
| 456 |
+
pdf_url = f"https://openreview.net/pdf?id={paper_id}"
|
| 457 |
+
st.markdown(f"[Click here to view PDF]({pdf_url})")
|
| 458 |
+
|
| 459 |
+
# Column 2: Display human annotations
|
| 460 |
+
with col2:
|
| 461 |
+
st.subheader('Human annotations')
|
| 462 |
+
if annotations['annotation_label'] == 1:
|
| 463 |
+
st.write(annotations['annotation_sentences'])
|
| 464 |
+
else:
|
| 465 |
+
st.write("No human feedback on this review")
|
| 466 |
+
|
| 467 |
+
# Column 3: Display feedback
|
| 468 |
+
with col3:
|
| 469 |
+
st.subheader('Feedback')
|
| 470 |
+
st.write(feedback)
|
| 471 |
+
|
| 472 |
+
# Column 4: Display revised feedback (from critic)
|
| 473 |
+
with col4:
|
| 474 |
+
st.subheader('Revised Feedback')
|
| 475 |
+
st.write(revised_feedback)
|
| 476 |
+
else:
|
| 477 |
|
| 478 |
+
# Create three columns
|
| 479 |
+
col1, col2, col3 = st.columns(3)
|
| 480 |
+
|
| 481 |
+
# Column 1: Display review fields
|
| 482 |
+
with col1:
|
| 483 |
+
st.subheader("Review")
|
| 484 |
+
st.write(json.dumps(review))
|
| 485 |
+
|
| 486 |
+
if not upload_file:
|
| 487 |
+
pdf_url = f"https://openreview.net/pdf?id={paper_id}"
|
| 488 |
+
st.markdown(f"[Click here to view PDF]({pdf_url})")
|
| 489 |
+
|
| 490 |
+
# Column 2: Display feedback
|
| 491 |
+
with col2:
|
| 492 |
+
st.subheader('Feedback')
|
| 493 |
+
st.write(feedback)
|
| 494 |
+
|
| 495 |
+
# Column 3: Display revised feedback (from critic)
|
| 496 |
+
with col3:
|
| 497 |
+
st.subheader('Revised Feedback')
|
| 498 |
+
st.write(revised_feedback)
|
| 499 |
|
| 500 |
else:
|
| 501 |
st.title('Please enter OpenReview ID or upload PDF with review to generate feedback')
|