-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharxiv_utils.py
More file actions
184 lines (161 loc) · 7.63 KB
/
Copy patharxiv_utils.py
File metadata and controls
184 lines (161 loc) · 7.63 KB
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
import requests
import time
import os
from xml.etree import ElementTree
import configparser
import google.generativeai as genai
import traceback
import logging
from concurrent.futures import ThreadPoolExecutor, TimeoutError
import subprocess
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Load API keys and settings
config = configparser.ConfigParser()
with open('key.ini') as config_file:
config.read_file(config_file)
def fetch_arxiv_papers(query, max_results=5, retries=3):
base_url = "http://export.arxiv.org/api/query?"
search_query = f"search_query=all:{query}&start=0&max_results={max_results}"
for attempt in range(retries):
try:
response = requests.get(base_url + search_query)
response.raise_for_status()
return parse_arxiv_response(response.content)
except requests.exceptions.HTTPError as e:
if response.status_code == 502:
logging.warning("Received 502, retrying...")
time.sleep(2)
else:
logging.error(f"Error fetching papers: {e}")
break
return None
def parse_arxiv_response(content):
root = ElementTree.fromstring(content)
papers = []
for entry in root.findall("{http://www.w3.org/2005/Atom}entry"):
paper_id = entry.find("{http://www.w3.org/2005/Atom}id").text.split("/")[-1]
published_date = entry.find("{http://www.w3.org/2005/Atom}published").text.split("T")[0]
authors = [author.find("{http://www.w3.org/2005/Atom}name").text for author in entry.findall("{http://www.w3.org/2005/Atom}author")]
paper = {
"id": paper_id,
"title": entry.find("{http://www.w3.org/2005/Atom}title").text,
"summary": entry.find("{http://www.w3.org/2005/Atom}summary").text,
"pdf_url": next(link.get('href') for link in entry.findall("{http://www.w3.org/2005/Atom}link") if link.get('title') == 'pdf'),
"published_date": published_date,
"authors": authors,
}
papers.append(paper)
return papers
def download_paper(pdf_url, paper_id):
response = requests.get(pdf_url)
if response.status_code == 200:
filename = f"{paper_id}.pdf"
with open(filename, 'wb') as f:
f.write(response.content)
return filename
else:
return None
def open_pdf(filename):
if os.name == 'nt': # For Windows
os.startfile(filename)
elif os.name == 'posix': # For macOS and Linux
opener = 'open' if sys.platform == 'darwin' else 'xdg-open'
subprocess.call([opener, filename])
def api_request(api_key, api_base, model, role, content, max_tokens=500):
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data = {
"model": model,
"messages": [
{"role": "system", "content": role},
{"role": "user", "content": content}
],
"max_tokens": max_tokens
}
try:
response = requests.post(f"{api_base}/chat/completions", headers=headers, json=data, timeout=30)
response.raise_for_status()
return response.json().get('choices', [{}])[0].get('message', {}).get('content', '')
except requests.exceptions.RequestException as e:
logging.error(f"Error in API request: {str(e)}")
return f"Error in API request: {str(e)}"
def summarize_with_groq(text):
api_key = config['Groq']['API_KEY']
api_base = config['Groq']['API_BASE']
model = config['Groq']['GROQ_MODEL']
return api_request(api_key, api_base, model,
"Your goal is to summarize the provided content from an academic paper. Your summary should be concise and focus on the key information of the academic paper, do not miss any important point.",
f"Please summarize the following scientific paper:\n\n{text}")
def polish_with_groq(text):
api_key = config['Groq']['API_KEY']
api_base = config['Groq']['API_BASE']
model = config['Groq']['GROQ_MODEL']
return api_request(api_key, api_base, model,
"You are a helpful assistant that polishes and improves text.",
f"Please polish and improve the following text:\n\n{text}")
def talk_to_paper_with_groq(paper_content, question):
api_key = config['Groq']['API_KEY']
api_base = config['Groq']['API_BASE']
model = config['Groq']['GROQ_MODEL'] # Adjust as necessary for the chat model
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data = {
"model": model,
"messages": [
{"role": "system", "content": "You are a professional academic paper reviewer and mentor named Garxt. As a professional academic paper reviewer and helpful mentor, you possess exceptional logical and critical thinking skills, enabling you to provide concise and insightful responses."},
{"role": "system", "content": "You are not allowed to discuss anything about politics, do not comment on anything about that."},
{"role": "user", "content": f"You will be asked to answer questions about the paper with deep knowledge about it, providing clear and concise explanations in a helpful, friendly manner, using the asker's language, answer this question:\n\nQuestion: {question}\n\nPaper content: {paper_content}"}
],
"max_tokens": 700
}
try:
logging.info("Sending request to Groq API for paper chat")
response = requests.post(f"{api_base}/chat/completions", headers=headers, json=data, timeout=30)
response.raise_for_status()
chat_response = response.json()['choices'][0]['message']['content']
logging.info("Successfully received response from Groq API for paper chat")
return chat_response
except requests.exceptions.RequestException as e:
logging.error(f"Error in talking to paper with Groq: {str(e)}")
return f"Error in talking to paper with Groq: {str(e)}"
def run_with_timeout(func, args, timeout):
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(func, *args)
try:
return future.result(timeout=timeout)
except TimeoutError:
logging.error(f"Operation timed out for function: {func.__name__}")
return "Error: Operation timed out"
def translate_with_groq(text, target_language="en"):
api_key = config['Groq']['API_KEY']
api_base = config['Groq']['API_BASE']
model = config['Groq']['GROQ_MODEL'] # Adjust as necessary for the translation model
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data = {
"model": model,
"messages": [
{"role": "system", "content": "You are a translation assistant."},
{"role": "user", "content": f"Translate the following text to {target_language}:\n\n{text}"}
],
"max_tokens": 500
}
try:
logging.info("Sending request to Groq API for translation")
response = requests.post(f"{api_base}/chat/completions", headers=headers, json=data, timeout=30)
response.raise_for_status()
translated_text = response.json()['choices'][0]['message']['content']
logging.info("Successfully received translation from Groq API")
return translated_text
except requests.exceptions.RequestException as e:
logging.error(f"Error in translation with Groq: {str(e)}")
return f"Error in translation with Groq: {str(e)}"
def summarize_paper(text):
return summarize_with_groq(text)