revert to flask once more
Browse files- app.py +28 -41
- app_gradio.py +49 -0
- test.ipynb +18 -87
- test_gradio.ipynb +169 -0
- update_valdata.py +34 -0
app.py
CHANGED
@@ -1,49 +1,36 @@
|
|
1 |
-
|
2 |
-
from threading import Thread
|
3 |
-
import time
|
4 |
-
import anvil.server
|
5 |
import os
|
6 |
-
anvil.server.connect('55MH4EBKM22EP4E6D5T6CVSL-VGO5X4SM6JEXGJVT')
|
7 |
-
import json
|
8 |
-
import ast
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
with open('./out.txt','r') as f: output=f.read()
|
14 |
return output
|
15 |
|
16 |
-
@
|
17 |
-
def
|
18 |
-
|
19 |
-
with open('./out.txt','r') as f: output=f.read()
|
20 |
-
return output
|
21 |
-
|
22 |
-
@anvil.server.callable
|
23 |
-
def get_file(filename):
|
24 |
-
m = BlobMedia('text/plain', 'Hello, world!', name='hello.txt')
|
25 |
-
return m
|
26 |
-
|
27 |
-
gradio_interface = gr.Interface(
|
28 |
-
fn=run_script,
|
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 |
-
data = response["data"])
|
45 |
|
46 |
-
|
|
|
47 |
|
48 |
-
gradio_interface.launch()
|
49 |
|
|
|
1 |
+
from flask import Flask,request,render_template,send_file
|
|
|
|
|
|
|
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("/file/<string:filename>")
|
9 |
+
def return_pdf(filename):
|
10 |
+
return send_file(filename)
|
11 |
+
|
12 |
+
@app.route('/run',methods=['GET','POST'])
|
13 |
+
def run_script():
|
14 |
+
script=''
|
15 |
+
# print(request.method)
|
16 |
+
print(request)
|
17 |
+
if request.method=='GET':
|
18 |
+
script=request.args.get('script')
|
19 |
+
print('I am in get')
|
20 |
+
elif request.method=='POST':
|
21 |
+
print('I am in post')
|
22 |
+
data=request.get_json()
|
23 |
+
if 'script' in data: script=data['script']
|
24 |
+
if script=='' or script is None: return 'INVALID'
|
25 |
+
os.system(script+' > ./out.txt')
|
26 |
with open('./out.txt','r') as f: output=f.read()
|
27 |
return output
|
28 |
|
29 |
+
@app.route('/',methods=['GET', 'POST'])
|
30 |
+
def home():
|
31 |
+
return render_template('home.html',messageD=MESSAGED)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
if __name__=='__main__':
|
34 |
+
app.run(host="0.0.0.0", port=7860)
|
35 |
|
|
|
36 |
|
app_gradio.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from threading import Thread
|
3 |
+
import time
|
4 |
+
import anvil.server
|
5 |
+
import os
|
6 |
+
anvil.server.connect('55MH4EBKM22EP4E6D5T6CVSL-VGO5X4SM6JEXGJVT')
|
7 |
+
import json
|
8 |
+
import ast
|
9 |
+
|
10 |
+
def run_script(scriptname):
|
11 |
+
# return scriptname
|
12 |
+
os.system(scriptname+' > ./out.txt')
|
13 |
+
with open('./out.txt','r') as f: output=f.read()
|
14 |
+
return output
|
15 |
+
|
16 |
+
@anvil.server.callable
|
17 |
+
def run_command(scriptname):
|
18 |
+
os.system(scriptname+' > ./out.txt')
|
19 |
+
with open('./out.txt','r') as f: output=f.read()
|
20 |
+
return output
|
21 |
+
|
22 |
+
@anvil.server.callable
|
23 |
+
def get_file(filename):
|
24 |
+
m = BlobMedia('text/plain', 'Hello, world!', name='hello.txt')
|
25 |
+
return m
|
26 |
+
|
27 |
+
gradio_interface = gr.Interface(
|
28 |
+
fn=run_script,
|
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 |
+
data = response["data"])
|
45 |
+
|
46 |
+
''')
|
47 |
+
|
48 |
+
gradio_interface.launch()
|
49 |
+
|
test.ipynb
CHANGED
@@ -2,7 +2,7 @@
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
-
"execution_count":
|
6 |
"metadata": {
|
7 |
"tags": []
|
8 |
},
|
@@ -14,88 +14,6 @@
|
|
14 |
"import pandas as pd"
|
15 |
]
|
16 |
},
|
17 |
-
{
|
18 |
-
"cell_type": "code",
|
19 |
-
"execution_count": 15,
|
20 |
-
"metadata": {
|
21 |
-
"tags": []
|
22 |
-
},
|
23 |
-
"outputs": [],
|
24 |
-
"source": [
|
25 |
-
"url='https://gmshroff-gmserver.hf.space/run/predict'\n",
|
26 |
-
"# url='http://127.0.0.1:7860'\n",
|
27 |
-
"hf_token='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'"
|
28 |
-
]
|
29 |
-
},
|
30 |
-
{
|
31 |
-
"cell_type": "code",
|
32 |
-
"execution_count": 16,
|
33 |
-
"metadata": {
|
34 |
-
"tags": []
|
35 |
-
},
|
36 |
-
"outputs": [
|
37 |
-
{
|
38 |
-
"name": "stdout",
|
39 |
-
"output_type": "stream",
|
40 |
-
"text": [
|
41 |
-
"Loaded as API: https://gmshroff-gmserver.hf.space/run/predict/ ✔\n"
|
42 |
-
]
|
43 |
-
},
|
44 |
-
{
|
45 |
-
"ename": "ValueError",
|
46 |
-
"evalue": "Could not get Gradio config from: https://gmshroff-gmserver.hf.space/run/predict/",
|
47 |
-
"output_type": "error",
|
48 |
-
"traceback": [
|
49 |
-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
50 |
-
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
|
51 |
-
"\u001b[0;32m/opt/homebrew/Caskroom/miniforge/base/envs/py38algo/lib/python3.8/site-packages/gradio_client/client.py\u001b[0m in \u001b[0;36m_get_config\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 594\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 595\u001b[0;31m \u001b[0mconfig\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mjson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mloads\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgroup\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# type: ignore\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 596\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mAttributeError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mae\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
52 |
-
"\u001b[0;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'group'",
|
53 |
-
"\nThe above exception was the direct cause of the following exception:\n",
|
54 |
-
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
|
55 |
-
"\u001b[0;32m/var/folders/jz/466kbks91q3glsb0kx0kfwk80000gq/T/ipykernel_75812/2087672778.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mgradio_client\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mClient\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mclient\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mClient\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0murl\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mclient\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpredict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"pwd\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mapi_name\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"/predict\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
56 |
-
"\u001b[0;32m/opt/homebrew/Caskroom/miniforge/base/envs/py38algo/lib/python3.8/site-packages/gradio_client/client.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, src, hf_token, max_workers, serialize, output_dir, verbose)\u001b[0m\n\u001b[1;32m 125\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupload_url\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0murllib\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mparse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0murljoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msrc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mutils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mUPLOAD_URL\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 126\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreset_url\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0murllib\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mparse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0murljoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msrc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mutils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mRESET_URL\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 127\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconfig\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_config\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 128\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msession_hash\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0muuid\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0muuid4\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 129\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
57 |
-
"\u001b[0;32m/opt/homebrew/Caskroom/miniforge/base/envs/py38algo/lib/python3.8/site-packages/gradio_client/client.py\u001b[0m in \u001b[0;36m_get_config\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 595\u001b[0m \u001b[0mconfig\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mjson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mloads\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgroup\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# type: ignore\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 596\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mAttributeError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mae\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 597\u001b[0;31m raise ValueError(\n\u001b[0m\u001b[1;32m 598\u001b[0m \u001b[0;34mf\"Could not get Gradio config from: {self.src}\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 599\u001b[0m ) from ae\n",
|
58 |
-
"\u001b[0;31mValueError\u001b[0m: Could not get Gradio config from: https://gmshroff-gmserver.hf.space/run/predict/"
|
59 |
-
]
|
60 |
-
}
|
61 |
-
],
|
62 |
-
"source": [
|
63 |
-
"from gradio_client import Client\n",
|
64 |
-
"\n",
|
65 |
-
"client = Client(url)\n",
|
66 |
-
"result = client.predict(\"pwd\",api_name=\"/predict\",hf_token=hf_token)\n",
|
67 |
-
"result"
|
68 |
-
]
|
69 |
-
},
|
70 |
-
{
|
71 |
-
"cell_type": "code",
|
72 |
-
"execution_count": 35,
|
73 |
-
"metadata": {},
|
74 |
-
"outputs": [],
|
75 |
-
"source": [
|
76 |
-
"response = requests.post(url,json=json.dumps({\"data\":\"ls\"}),headers={'Content-Type':'application/json'})"
|
77 |
-
]
|
78 |
-
},
|
79 |
-
{
|
80 |
-
"cell_type": "code",
|
81 |
-
"execution_count": 36,
|
82 |
-
"metadata": {},
|
83 |
-
"outputs": [
|
84 |
-
{
|
85 |
-
"data": {
|
86 |
-
"text/plain": [
|
87 |
-
"b'{\"detail\":\"Method Not Allowed\"}'"
|
88 |
-
]
|
89 |
-
},
|
90 |
-
"execution_count": 36,
|
91 |
-
"metadata": {},
|
92 |
-
"output_type": "execute_result"
|
93 |
-
}
|
94 |
-
],
|
95 |
-
"source": [
|
96 |
-
"response.content"
|
97 |
-
]
|
98 |
-
},
|
99 |
{
|
100 |
"cell_type": "code",
|
101 |
"execution_count": 3,
|
@@ -128,11 +46,24 @@
|
|
128 |
},
|
129 |
{
|
130 |
"cell_type": "code",
|
131 |
-
"execution_count":
|
132 |
-
"metadata": {
|
133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
"source": [
|
135 |
-
"urlretrieve(url='http://127.0.0.1:
|
136 |
]
|
137 |
},
|
138 |
{
|
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
"metadata": {
|
7 |
"tags": []
|
8 |
},
|
|
|
14 |
"import pandas as pd"
|
15 |
]
|
16 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
{
|
18 |
"cell_type": "code",
|
19 |
"execution_count": 3,
|
|
|
46 |
},
|
47 |
{
|
48 |
"cell_type": "code",
|
49 |
+
"execution_count": 2,
|
50 |
+
"metadata": {
|
51 |
+
"tags": []
|
52 |
+
},
|
53 |
+
"outputs": [
|
54 |
+
{
|
55 |
+
"data": {
|
56 |
+
"text/plain": [
|
57 |
+
"('./returned_file.txt', <http.client.HTTPMessage at 0x122375df0>)"
|
58 |
+
]
|
59 |
+
},
|
60 |
+
"execution_count": 2,
|
61 |
+
"metadata": {},
|
62 |
+
"output_type": "execute_result"
|
63 |
+
}
|
64 |
+
],
|
65 |
"source": [
|
66 |
+
"urlretrieve(url='http://127.0.0.1:7860/file/update.txt',filename='./returned_file.txt')"
|
67 |
]
|
68 |
},
|
69 |
{
|
test_gradio.ipynb
ADDED
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 5,
|
6 |
+
"metadata": {
|
7 |
+
"tags": []
|
8 |
+
},
|
9 |
+
"outputs": [],
|
10 |
+
"source": [
|
11 |
+
"import requests\n",
|
12 |
+
"import json\n",
|
13 |
+
"from urllib.request import urlretrieve\n",
|
14 |
+
"import pandas as pd"
|
15 |
+
]
|
16 |
+
},
|
17 |
+
{
|
18 |
+
"cell_type": "code",
|
19 |
+
"execution_count": 15,
|
20 |
+
"metadata": {
|
21 |
+
"tags": []
|
22 |
+
},
|
23 |
+
"outputs": [],
|
24 |
+
"source": [
|
25 |
+
"url='https://gmshroff-gmserver.hf.space/run/predict'\n",
|
26 |
+
"# url='http://127.0.0.1:7860'\n",
|
27 |
+
"hf_token='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'"
|
28 |
+
]
|
29 |
+
},
|
30 |
+
{
|
31 |
+
"cell_type": "code",
|
32 |
+
"execution_count": 16,
|
33 |
+
"metadata": {
|
34 |
+
"tags": []
|
35 |
+
},
|
36 |
+
"outputs": [
|
37 |
+
{
|
38 |
+
"name": "stdout",
|
39 |
+
"output_type": "stream",
|
40 |
+
"text": [
|
41 |
+
"Loaded as API: https://gmshroff-gmserver.hf.space/run/predict/ ✔\n"
|
42 |
+
]
|
43 |
+
},
|
44 |
+
{
|
45 |
+
"ename": "ValueError",
|
46 |
+
"evalue": "Could not get Gradio config from: https://gmshroff-gmserver.hf.space/run/predict/",
|
47 |
+
"output_type": "error",
|
48 |
+
"traceback": [
|
49 |
+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
50 |
+
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
|
51 |
+
"\u001b[0;32m/opt/homebrew/Caskroom/miniforge/base/envs/py38algo/lib/python3.8/site-packages/gradio_client/client.py\u001b[0m in \u001b[0;36m_get_config\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 594\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 595\u001b[0;31m \u001b[0mconfig\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mjson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mloads\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgroup\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# type: ignore\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 596\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mAttributeError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mae\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
52 |
+
"\u001b[0;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'group'",
|
53 |
+
"\nThe above exception was the direct cause of the following exception:\n",
|
54 |
+
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
|
55 |
+
"\u001b[0;32m/var/folders/jz/466kbks91q3glsb0kx0kfwk80000gq/T/ipykernel_75812/2087672778.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mgradio_client\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mClient\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mclient\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mClient\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0murl\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mclient\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpredict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"pwd\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mapi_name\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"/predict\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
56 |
+
"\u001b[0;32m/opt/homebrew/Caskroom/miniforge/base/envs/py38algo/lib/python3.8/site-packages/gradio_client/client.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, src, hf_token, max_workers, serialize, output_dir, verbose)\u001b[0m\n\u001b[1;32m 125\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupload_url\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0murllib\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mparse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0murljoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msrc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mutils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mUPLOAD_URL\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 126\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreset_url\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0murllib\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mparse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0murljoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msrc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mutils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mRESET_URL\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 127\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconfig\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_config\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 128\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msession_hash\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0muuid\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0muuid4\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 129\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
57 |
+
"\u001b[0;32m/opt/homebrew/Caskroom/miniforge/base/envs/py38algo/lib/python3.8/site-packages/gradio_client/client.py\u001b[0m in \u001b[0;36m_get_config\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 595\u001b[0m \u001b[0mconfig\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mjson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mloads\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgroup\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# type: ignore\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 596\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mAttributeError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mae\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 597\u001b[0;31m raise ValueError(\n\u001b[0m\u001b[1;32m 598\u001b[0m \u001b[0;34mf\"Could not get Gradio config from: {self.src}\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 599\u001b[0m ) from ae\n",
|
58 |
+
"\u001b[0;31mValueError\u001b[0m: Could not get Gradio config from: https://gmshroff-gmserver.hf.space/run/predict/"
|
59 |
+
]
|
60 |
+
}
|
61 |
+
],
|
62 |
+
"source": [
|
63 |
+
"from gradio_client import Client\n",
|
64 |
+
"\n",
|
65 |
+
"client = Client(url)\n",
|
66 |
+
"result = client.predict(\"pwd\",api_name=\"/predict\",hf_token=hf_token)\n",
|
67 |
+
"result"
|
68 |
+
]
|
69 |
+
},
|
70 |
+
{
|
71 |
+
"cell_type": "code",
|
72 |
+
"execution_count": 35,
|
73 |
+
"metadata": {},
|
74 |
+
"outputs": [],
|
75 |
+
"source": [
|
76 |
+
"response = requests.post(url,json=json.dumps({\"data\":\"ls\"}),headers={'Content-Type':'application/json'})"
|
77 |
+
]
|
78 |
+
},
|
79 |
+
{
|
80 |
+
"cell_type": "code",
|
81 |
+
"execution_count": 36,
|
82 |
+
"metadata": {},
|
83 |
+
"outputs": [
|
84 |
+
{
|
85 |
+
"data": {
|
86 |
+
"text/plain": [
|
87 |
+
"b'{\"detail\":\"Method Not Allowed\"}'"
|
88 |
+
]
|
89 |
+
},
|
90 |
+
"execution_count": 36,
|
91 |
+
"metadata": {},
|
92 |
+
"output_type": "execute_result"
|
93 |
+
}
|
94 |
+
],
|
95 |
+
"source": [
|
96 |
+
"response.content"
|
97 |
+
]
|
98 |
+
},
|
99 |
+
{
|
100 |
+
"cell_type": "code",
|
101 |
+
"execution_count": 3,
|
102 |
+
"metadata": {},
|
103 |
+
"outputs": [],
|
104 |
+
"source": [
|
105 |
+
"headers = {'Content-Type': 'application/json'}\n",
|
106 |
+
"# url='http://127.0.0.1:5000/run'\n",
|
107 |
+
"url='https://huggingface.co/spaces/gmshroff/gmserver'\n",
|
108 |
+
"body={\"script\":\"python update_valdata.py\"}"
|
109 |
+
]
|
110 |
+
},
|
111 |
+
{
|
112 |
+
"cell_type": "code",
|
113 |
+
"execution_count": null,
|
114 |
+
"metadata": {},
|
115 |
+
"outputs": [],
|
116 |
+
"source": [
|
117 |
+
"response=requests.post(url=url,data=json.dumps(body),headers = {'Content-Type': 'application/json'})"
|
118 |
+
]
|
119 |
+
},
|
120 |
+
{
|
121 |
+
"cell_type": "code",
|
122 |
+
"execution_count": null,
|
123 |
+
"metadata": {},
|
124 |
+
"outputs": [],
|
125 |
+
"source": [
|
126 |
+
"response.content"
|
127 |
+
]
|
128 |
+
},
|
129 |
+
{
|
130 |
+
"cell_type": "code",
|
131 |
+
"execution_count": null,
|
132 |
+
"metadata": {},
|
133 |
+
"outputs": [],
|
134 |
+
"source": [
|
135 |
+
"urlretrieve(url='http://127.0.0.1:5000/static/validation_subset_int8.parquet',filename='./validation_subset_int8.parquet')"
|
136 |
+
]
|
137 |
+
},
|
138 |
+
{
|
139 |
+
"cell_type": "code",
|
140 |
+
"execution_count": null,
|
141 |
+
"metadata": {},
|
142 |
+
"outputs": [],
|
143 |
+
"source": [
|
144 |
+
"df=pd.read_parquet('/tmp/validation_subset_int8.parquet')"
|
145 |
+
]
|
146 |
+
}
|
147 |
+
],
|
148 |
+
"metadata": {
|
149 |
+
"kernelspec": {
|
150 |
+
"display_name": "Python 3 (ipykernel)",
|
151 |
+
"language": "python",
|
152 |
+
"name": "python3"
|
153 |
+
},
|
154 |
+
"language_info": {
|
155 |
+
"codemirror_mode": {
|
156 |
+
"name": "ipython",
|
157 |
+
"version": 3
|
158 |
+
},
|
159 |
+
"file_extension": ".py",
|
160 |
+
"mimetype": "text/x-python",
|
161 |
+
"name": "python",
|
162 |
+
"nbconvert_exporter": "python",
|
163 |
+
"pygments_lexer": "ipython3",
|
164 |
+
"version": "3.8.12"
|
165 |
+
}
|
166 |
+
},
|
167 |
+
"nbformat": 4,
|
168 |
+
"nbformat_minor": 4
|
169 |
+
}
|
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='./data/'
|
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 |
+
|