1
1
import os , math , re , requests , io , time
2
2
from typing import List , Optional , Union
3
3
4
+ import sys
5
+ from pathlib import Path
6
+ sys .path .append (str (Path (__file__ ).parent ))
7
+
4
8
from flask import Flask , abort , request , render_template , jsonify
5
9
from functools import lru_cache
6
10
7
11
from constants import INDEX_PATH , VENUES
8
-
9
12
from search import ColBERT
10
13
from db import create_database , query_paper_metadata
11
14
from utils import download_index_from_hf , print_estimate_cost
12
15
13
16
import PyPDF2
14
17
from openai import OpenAI
15
18
19
+ from threading import local
20
+ thread_local = local ()
21
+
16
22
PORT = int (os .getenv ("PORT" , 8080 ))
17
23
app = Flask (__name__ )
18
-
19
- # Load OpenAI API key
20
- if os .path .exists ('.openai-api-key' ):
21
- with open ('.openai-api-key' , 'r' ) as f :
22
- api_key = f .read ().strip ()
23
- client = OpenAI (api_key = api_key )
24
+ colbert = None
25
+ _is_initialized = False
24
26
25
27
@lru_cache (maxsize = 1000000 )
26
28
def api_search_query (query ):
@@ -114,17 +116,26 @@ def get_pdf_text(pdf_url: str) -> str:
114
116
return " " .join (page .extract_text () for page in pdf_reader .pages )
115
117
116
118
119
+ def get_openai_client ():
120
+ if not hasattr (thread_local , 'client' ):
121
+ if os .path .exists ('.openai-api-key' ):
122
+ with open ('.openai-api-key' , 'r' ) as f :
123
+ api_key = f .read ().strip ()
124
+ thread_local .client = OpenAI (api_key = api_key )
125
+ return thread_local .client
126
+
127
+
117
128
@app .route ('/api/llm' , methods = ['POST' ])
118
129
def query_llm ():
130
+ print (f'Started a new query!' )
131
+ client = get_openai_client ()
119
132
data = request .json
120
133
title = data ['title' ]
121
134
abstract = data ['abstract' ]
122
135
question = data ['question' ]
123
136
pdf_url = data ['pdf_url' ] if 'pdf_url' in data else None
124
137
125
138
try :
126
- print (f'Started a new query!' )
127
-
128
139
start_time = time .time ()
129
140
pdf_text = get_pdf_text (pdf_url )
130
141
print (f"PDF text extraction took { time .time () - start_time :.2f} seconds" )
@@ -135,7 +146,7 @@ def query_llm():
135
146
Question: { question }
136
147
Please provide a concise answer."""
137
148
138
- print_estimate_cost (prompt , model = 'gpt-4o-mini' , input_cost = 0.15 , output_cost = 0.6 , estimated_output_toks = 100 )
149
+ # print_estimate_cost(prompt, model='gpt-4o-mini', input_cost=0.15, output_cost=0.6, estimated_output_toks=100)
139
150
140
151
start_llm_time = time .time ()
141
152
response = client .chat .completions .create (
@@ -157,20 +168,29 @@ def query_llm():
157
168
return jsonify ({'error' : 'Failed to process request' }), 500
158
169
159
170
171
+ def init_app ():
172
+ global colbert , _is_initialized
173
+ if not _is_initialized :
174
+ download_index_from_hf ()
175
+ create_database ()
176
+ colbert = ColBERT (index_path = INDEX_PATH )
177
+ _is_initialized = True
178
+
179
+ # Remove client initialization from here since we're using thread-local storage
180
+
181
+
182
+ @app .before_request
183
+ def before_request ():
184
+ init_app ()
185
+
186
+
160
187
if __name__ == "__main__" :
161
188
"""
162
189
Example usage:
163
190
python server.py
164
191
http://localhost:8080/api/colbert?query=Information retrevial with BERT
165
192
http://localhost:8080/api/search?query=Information retrevial with BERT
166
193
"""
167
- download_index_from_hf ()
168
- create_database ()
169
- global colbert
170
- colbert = ColBERT (index_path = INDEX_PATH )
171
- print (colbert .search ('text simplificaiton' ))
172
- print (api_search_query ("text simplification" )['topk' ][:5 ])
173
-
174
194
# Watch web dirs for changes
175
195
extra_files = [os .path .join (dirname , filename ) for dirname , _ , files in os .walk ('templates' ) for filename in files ]
176
196
0 commit comments