new flask-based server for running scripts on huggingface
Browse files- .DS_Store +0 -0
- .ipynb_checkpoints/app-checkpoint.py +18 -0
- README.md +2 -6
- __pycache__/app.cpython-38.pyc +0 -0
- anvilserver.py +0 -9
- app.py +24 -42
- client.py +0 -8
- library.py +0 -26
- out.txt +1 -1
- registration.py +0 -21
- requirements.txt +2 -2
- static/dummy.txt +1 -0
- templates/home.html +40 -0
- test.ipynb +84 -0
- test_rest.py +0 -9
- update_valdata.py +34 -0
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
.ipynb_checkpoints/app-checkpoint.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask,request,render_template
|
2 |
+
import os
|
3 |
+
|
4 |
+
app=Flask(__name__)
|
5 |
+
|
6 |
+
@app.route('/', methods=['GET', 'POST'])
|
7 |
+
def home():
|
8 |
+
return render_template
|
9 |
+
def run_script():
|
10 |
+
script=request.args.get('script')
|
11 |
+
os.system(script+' > ./out.txt')
|
12 |
+
with open('./out.txt','r') as f: output=f.read()
|
13 |
+
return output
|
14 |
+
|
15 |
+
if __name__=='__main__':
|
16 |
+
app.run()
|
17 |
+
|
18 |
+
|
README.md
CHANGED
@@ -12,9 +12,5 @@ license: unknown
|
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
Just push app.py and requirements.txt to gradio and
|
18 |
-
pip install anvil_uplink on your server (e.g. laptop) machine
|
19 |
-
and run anvilserver.py there. The gradio UI calls the server_function
|
20 |
-
published within anvilserver.py. This uses anvil as a router.
|
|
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
14 |
|
15 |
+
Flask server to run on huggingface.
|
16 |
+
Just push app.py and requirements.txt to repo and call via REST API
|
|
|
|
|
|
|
|
__pycache__/app.cpython-38.pyc
ADDED
Binary file (569 Bytes). View file
|
|
anvilserver.py
DELETED
@@ -1,9 +0,0 @@
|
|
1 |
-
import anvil.server
|
2 |
-
|
3 |
-
anvil.server.connect('55MH4EBKM22EP4E6D5T6CVSL-VGO5X4SM6JEXGJVT')
|
4 |
-
|
5 |
-
@anvil.server.callable
|
6 |
-
def server_function(arg):
|
7 |
-
return arg+' called anvilserver.py'
|
8 |
-
|
9 |
-
anvil.server.wait_forever()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
@@ -1,50 +1,32 @@
|
|
1 |
-
|
2 |
-
from threading import Thread
|
3 |
-
import time
|
4 |
-
import anvil.server
|
5 |
-
from registration import register,get_register,func_reg
|
6 |
-
from library import get_file,get_files
|
7 |
import os
|
8 |
-
anvil.server.connect('55MH4EBKM22EP4E6D5T6CVSL-VGO5X4SM6JEXGJVT')
|
9 |
-
register(get_file)
|
10 |
-
register(get_files)
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
with open('./out.txt','r') as f: output=f.read()
|
24 |
return output
|
25 |
-
# return "Input Data: " + name + ", stay tuned for ML models from this API"
|
26 |
-
|
27 |
-
gradio_interface = gr.Interface(
|
28 |
-
fn=my_inference_function,
|
29 |
-
inputs="text",
|
30 |
-
outputs="text",
|
31 |
-
title="REST API with Gradio and Huggingface Spaces",
|
32 |
-
description='''Inputs should be json of test item e.g., as a dictionary;
|
33 |
-
output right now is just returning the input; later label will be returned.
|
34 |
-
|
35 |
-
This is how to call the API from Python:
|
36 |
-
|
37 |
-
import requests
|
38 |
-
|
39 |
-
response = requests.post("https://gmshroff-gmserver.hf.space/run/predict", json={
|
40 |
-
"data": [
|
41 |
-
"\<put some json string here\>",
|
42 |
-
]}).json()
|
43 |
|
44 |
-
|
|
|
|
|
45 |
|
46 |
-
|
|
|
47 |
|
48 |
-
gradio_interface.launch()
|
49 |
-
# anvil.server.wait_forever()
|
50 |
|
|
|
1 |
+
from flask import Flask,request,render_template
|
|
|
|
|
|
|
|
|
|
|
2 |
import os
|
|
|
|
|
|
|
3 |
|
4 |
+
app=Flask(__name__)
|
5 |
+
MESSAGED={'title':'Script Server',
|
6 |
+
'messageL':['script in "script" parameter or field of json input to /run will be run']}
|
7 |
+
|
8 |
+
@app.route('/run',methods=['GET','POST'])
|
9 |
+
def run_script():
|
10 |
+
script=''
|
11 |
+
# print(request.method)
|
12 |
+
print(request)
|
13 |
+
if request.method=='GET':
|
14 |
+
script=request.args.get('script')
|
15 |
+
print('I am in get')
|
16 |
+
elif request.method=='POST':
|
17 |
+
print('I am in post')
|
18 |
+
data=request.get_json()
|
19 |
+
if 'script' in data: script=data['script']
|
20 |
+
if script=='' or script is None: return 'INVALID'
|
21 |
+
os.system(script+' > ./out.txt')
|
22 |
with open('./out.txt','r') as f: output=f.read()
|
23 |
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
@app.route('/',methods=['GET', 'POST'])
|
26 |
+
def home():
|
27 |
+
return render_template('home.html',messageD=MESSAGED)
|
28 |
|
29 |
+
if __name__=='__main__':
|
30 |
+
app.run()
|
31 |
|
|
|
|
|
32 |
|
client.py
DELETED
@@ -1,8 +0,0 @@
|
|
1 |
-
from gradio_client import Client
|
2 |
-
|
3 |
-
client = Client("https://gmshroff-gmserver.hf.space/")
|
4 |
-
result = client.predict(
|
5 |
-
"{'name':'Gautam'}", # str in 'name' Textbox component
|
6 |
-
api_name="/predict"
|
7 |
-
)
|
8 |
-
print(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
library.py
DELETED
@@ -1,26 +0,0 @@
|
|
1 |
-
from registration import update_reg
|
2 |
-
from anvil.tables import app_tables
|
3 |
-
from PyPDF2 import PdfReader
|
4 |
-
from io import BytesIO
|
5 |
-
# from notebook_funcs import get_message
|
6 |
-
|
7 |
-
def get_files():
|
8 |
-
update_reg('get_files')
|
9 |
-
retval=''
|
10 |
-
query=app_tables.files.search()
|
11 |
-
for r in query:
|
12 |
-
retval+=r['filename']+'\n'
|
13 |
-
return retval
|
14 |
-
|
15 |
-
def get_file(name):
|
16 |
-
update_reg('get_file')
|
17 |
-
retval=''
|
18 |
-
r=app_tables.files.get(filename=name)
|
19 |
-
bts=r['file'].get_bytes()
|
20 |
-
sio=BytesIO(bts)
|
21 |
-
with sio as f:
|
22 |
-
reader=PdfReader(f)
|
23 |
-
fpage=reader.pages[0].extract_text()
|
24 |
-
retval+=r['filename']+'\n'+ fpage
|
25 |
-
return retval
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out.txt
CHANGED
@@ -1 +1 @@
|
|
1 |
-
/
|
|
|
1 |
+
Now please copy the file to server via: scp ../../data/validation_subset_int8.parquet gms@gms1:/home/gms/numerai/data/.
|
registration.py
DELETED
@@ -1,21 +0,0 @@
|
|
1 |
-
import anvil.server
|
2 |
-
|
3 |
-
func_reg={}
|
4 |
-
|
5 |
-
def register(function):
|
6 |
-
global func_reg
|
7 |
-
func={}
|
8 |
-
func['func']=function
|
9 |
-
func['calls']=0
|
10 |
-
func_reg[function.__name__]=func
|
11 |
-
anvil.server.callable(function)
|
12 |
-
|
13 |
-
def update_reg(name):
|
14 |
-
global func_reg
|
15 |
-
func_reg[name]['calls']+=1
|
16 |
-
|
17 |
-
def get_register():
|
18 |
-
regstr=''
|
19 |
-
for f in func_reg:
|
20 |
-
regstr+=f + ' called ' + str(func_reg[f]['calls']) + ' times\n'
|
21 |
-
return regstr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,2 +1,2 @@
|
|
1 |
-
|
2 |
-
|
|
|
1 |
+
numerapi
|
2 |
+
flask
|
static/dummy.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
dummy file
|
templates/home.html
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<html>
|
2 |
+
<head>
|
3 |
+
<title>gmserver</title>
|
4 |
+
<style>
|
5 |
+
* {
|
6 |
+
box-sizing: border-box;
|
7 |
+
}
|
8 |
+
/* Create two equal columns that floats next to each other */
|
9 |
+
.columnleft {
|
10 |
+
float: left;
|
11 |
+
padding: 10px;
|
12 |
+
width: 15%;
|
13 |
+
}
|
14 |
+
.columncenter {
|
15 |
+
float: left;
|
16 |
+
padding: 10px;
|
17 |
+
width: 80%;
|
18 |
+
}
|
19 |
+
/* Clear floats after the columns */
|
20 |
+
.row:after {
|
21 |
+
content: "";
|
22 |
+
display: table;
|
23 |
+
clear: both;
|
24 |
+
}
|
25 |
+
</style>
|
26 |
+
</head>
|
27 |
+
<body>
|
28 |
+
<div class="row">
|
29 |
+
<div class="columnleft">
|
30 |
+
<img src="/static/logo.jpeg" width="50px">
|
31 |
+
</div>
|
32 |
+
<div class="columncenter" align="center">
|
33 |
+
<h1>{{messageD['title']}}</h1>
|
34 |
+
{% for message in messageD['messageL']:%}
|
35 |
+
<h4>{{message}}</h4>
|
36 |
+
{% endfor %}
|
37 |
+
</div>
|
38 |
+
</div>
|
39 |
+
</body>
|
40 |
+
</html>
|
test.ipynb
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"import requests\n",
|
10 |
+
"import json\n",
|
11 |
+
"from urllib.request import urlretrieve\n",
|
12 |
+
"import pandas as pd"
|
13 |
+
]
|
14 |
+
},
|
15 |
+
{
|
16 |
+
"cell_type": "code",
|
17 |
+
"execution_count": null,
|
18 |
+
"metadata": {},
|
19 |
+
"outputs": [],
|
20 |
+
"source": [
|
21 |
+
"headers = {'Content-Type': 'application/json'}\n",
|
22 |
+
"url='http://127.0.0.1:5000/run'\n",
|
23 |
+
"body={\"script\":\"python update_valdata.py\"}"
|
24 |
+
]
|
25 |
+
},
|
26 |
+
{
|
27 |
+
"cell_type": "code",
|
28 |
+
"execution_count": null,
|
29 |
+
"metadata": {},
|
30 |
+
"outputs": [],
|
31 |
+
"source": [
|
32 |
+
"response=requests.post(url=url,data=json.dumps(body),headers = {'Content-Type': 'application/json'})"
|
33 |
+
]
|
34 |
+
},
|
35 |
+
{
|
36 |
+
"cell_type": "code",
|
37 |
+
"execution_count": null,
|
38 |
+
"metadata": {},
|
39 |
+
"outputs": [],
|
40 |
+
"source": [
|
41 |
+
"response.content"
|
42 |
+
]
|
43 |
+
},
|
44 |
+
{
|
45 |
+
"cell_type": "code",
|
46 |
+
"execution_count": null,
|
47 |
+
"metadata": {},
|
48 |
+
"outputs": [],
|
49 |
+
"source": [
|
50 |
+
"urlretrieve(url='http://127.0.0.1:5000/static/validation_subset_int8.parquet',filename='./validation_subset_int8.parquet')"
|
51 |
+
]
|
52 |
+
},
|
53 |
+
{
|
54 |
+
"cell_type": "code",
|
55 |
+
"execution_count": null,
|
56 |
+
"metadata": {},
|
57 |
+
"outputs": [],
|
58 |
+
"source": [
|
59 |
+
"df=pd.read_parquet('/tmp/validation_subset_int8.parquet')"
|
60 |
+
]
|
61 |
+
}
|
62 |
+
],
|
63 |
+
"metadata": {
|
64 |
+
"kernelspec": {
|
65 |
+
"display_name": "py39numerai",
|
66 |
+
"language": "python",
|
67 |
+
"name": "python3"
|
68 |
+
},
|
69 |
+
"language_info": {
|
70 |
+
"codemirror_mode": {
|
71 |
+
"name": "ipython",
|
72 |
+
"version": 3
|
73 |
+
},
|
74 |
+
"file_extension": ".py",
|
75 |
+
"mimetype": "text/x-python",
|
76 |
+
"name": "python",
|
77 |
+
"nbconvert_exporter": "python",
|
78 |
+
"pygments_lexer": "ipython3",
|
79 |
+
"version": "3.9.9"
|
80 |
+
}
|
81 |
+
},
|
82 |
+
"nbformat": 4,
|
83 |
+
"nbformat_minor": 2
|
84 |
+
}
|
test_rest.py
DELETED
@@ -1,9 +0,0 @@
|
|
1 |
-
import requests
|
2 |
-
|
3 |
-
response = requests.post("https://gmshroff-gmserver.hf.space/run/predict", json={
|
4 |
-
"data": [
|
5 |
-
"<some json string>",
|
6 |
-
]}).json()
|
7 |
-
|
8 |
-
data = response["data"]
|
9 |
-
print(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
update_valdata.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[ ]:
|
5 |
+
|
6 |
+
|
7 |
+
from numerapi import NumerAPI
|
8 |
+
import os
|
9 |
+
import pandas as pd
|
10 |
+
import numpy as np
|
11 |
+
|
12 |
+
|
13 |
+
# In[ ]:
|
14 |
+
|
15 |
+
|
16 |
+
napi = NumerAPI()
|
17 |
+
data_path='./static/'
|
18 |
+
|
19 |
+
|
20 |
+
# In[ ]:
|
21 |
+
|
22 |
+
|
23 |
+
napi.download_dataset("v4.2/validation_int8.parquet", data_path+"validation_int8.parquet")
|
24 |
+
validation_data=pd.read_parquet(data_path+"validation_int8.parquet")
|
25 |
+
recent_eras=list(validation_data.loc[validation_data['data_type']=='validation']['era'].unique()[-2:])
|
26 |
+
validation_subset=validation_data[validation_data['era'].isin(recent_eras)]
|
27 |
+
validation_subset.to_parquet(data_path+"validation_subset_int8.parquet",index=False)
|
28 |
+
|
29 |
+
|
30 |
+
# In[ ]:
|
31 |
+
|
32 |
+
|
33 |
+
print("Now please copy the file to server via: scp ../../data/validation_subset_int8.parquet gms@gms1:/home/gms/numerai/data/.")
|
34 |
+
|