File size: 3,715 Bytes
a703588
7f07db8
a703588
46cde66
778d09e
 
ceb0714
df3fc47
ceb0714
0f79c21
46cde66
274ad4c
 
f9f8d44
80732d4
 
 
 
 
 
 
 
 
 
dcfa366
80732d4
 
587246e
 
dcfa366
a703588
 
 
054c3f5
 
df3fc47
 
054c3f5
ceb0714
 
 
 
 
 
 
 
 
 
 
778d09e
 
ceb0714
 
 
 
 
 
 
 
 
 
 
 
 
 
281b6e6
46cde66
 
 
 
 
 
 
 
 
55ae956
 
 
 
 
 
 
 
 
 
28a9672
55ae956
a703588
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcfa366
 
 
5f32ca7
dcfa366
 
5f32ca7
5ba3309
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
from flask import Flask,request,render_template,send_file,jsonify
import os
from transformers import AutoTokenizer, AutoModel
import anvil.server
import pathlib
import textwrap
import import_ipynb
from library import call_gpt, call_gemini, compress_bool_list, uncompress_bool_list
from background_service import BackgroundTaskService
import numpy as np

# anvil.server.connect('PLMOIU5VCGGUOJH2XORIBWV3-ZXZVFLWX7QFIIAF4')
anvil.server.connect("S3SLHUQ2BB33NVTP7FWRAOHS-NDSRD7CDALRPSPLL")

from sentence_transformers import SentenceTransformer
from sentence_transformers.util import cos_sim
# model = SentenceTransformer('thenlper/gte-large')
model = SentenceTransformer('BAAI/bge-large-en')

@anvil.server.callable
def encode(sentence = None):
    vec = model.encode(sentence)
    return [float(val) if isinstance(val, (int, float, np.float32)) else 0.0 for val in vec]

app=Flask(__name__)
MESSAGED={'title':'API Server for ICAPP',
          'messageL':['published server functions:','encode_anvil(text)', 'encode(sentence)',
                      'call_gemini(text,key)','call_gpt(text,key,model)',
                      'task_id<=launch(func_name,*args)','poll(task_id)']}

tokenizer = AutoTokenizer.from_pretrained('allenai/specter')
encoder = AutoModel.from_pretrained('allenai/specter')

anvil.server.callable(call_gpt)
anvil.server.callable(call_gemini)
anvil.server.callable(compress_bool_list)
anvil.server.callable(uncompress_bool_list)

service=BackgroundTaskService(max_tasks=10)
service.register(call_gpt)
service.register(call_gemini)

@anvil.server.callable
def launch(func_name,*args):
    global service
    # Launch task
    task_id = service.launch_task(func_name, *args)
    print(f"Task launched with ID: {task_id}")
    return task_id

@anvil.server.callable
def poll(task_id):
    global service
    # Poll for completion; if not complete return "In Progress" else return result
    result = service.get_result(task_id)
    if result=='No such task': return str(result)
    elif result!='In Progress': 
        del service.results[task_id]
        if isinstance(result, (int, float, str, list, dict, tuple)): 
            return result
        else: 
            print(str(result))
            return str(result)
    else: return str(result)

@anvil.server.callable
def encode_anvil(text):
    inputs = tokenizer(text, padding=True, truncation=True, 
                       return_tensors="pt", max_length=512)
    result = encoder(**inputs)
    embeddings = result.last_hidden_state[:, 0, :]
    emb_array = embeddings.detach().numpy()
    embedding=emb_array.tolist()
    return embedding

@anvil.server.callable
def reset_service():
    global call_gpt, call_gemini, service
    service=BackgroundTaskService(max_tasks=10)
    service.register(call_gpt)
    service.register(call_gemini)

@anvil.server.callable
def print_results_table():
    global service
    return(service.results)

@app.route('/encode',methods=['GET','POST'])
def encode():
    print(request)
    if request.method=='GET':
        text=request.args.get('text')
    elif request.method=='POST':
        data=request.get_json()
        if 'text' in data: text=data["text"]
    if text=='' or text is None: return -1
    inputs = tokenizer(text, padding=True, truncation=True, 
                       return_tensors="pt", max_length=512)
    result = encoder(**inputs)
    embeddings = result.last_hidden_state[:, 0, :]
    emb_array = embeddings.detach().numpy()
    embedding=emb_array.tolist()
    return jsonify({'embedding': embedding})

@app.route('/',methods=['GET', 'POST'])
def home():
    return render_template('home.html',messageD=MESSAGED)

if __name__=='__main__':
    app.run(host="0.0.0.0", port=7860)