Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os, json
|
2 |
+
import requests
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
from flask import Flask, request
|
6 |
+
app=Flask(__name__)
|
7 |
+
@app.route("/")
|
8 |
+
def hello():
|
9 |
+
return "welcome!"
|
10 |
+
|
11 |
+
def parse_params():
|
12 |
+
input=request.args.get('input')
|
13 |
+
kargs=request.args.get('kargs')
|
14 |
+
try:
|
15 |
+
input = json.load(input) if input is not None and input.length>0
|
16 |
+
except:
|
17 |
+
pass
|
18 |
+
try:
|
19 |
+
kargs = json.load(kargs) if kargs is not None and kargs.length>0
|
20 |
+
except:
|
21 |
+
kargs={}
|
22 |
+
return input, kargs
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
+
@app.route("/<task>")
|
27 |
+
def run_task(task):
|
28 |
+
(input, kargs)=parse_params()
|
29 |
+
pipe=pipeline(task, **kargs)
|
30 |
+
return pipe(input)
|
31 |
+
|
32 |
+
@app.route("/<user>/<model>")
|
33 |
+
def run_model():
|
34 |
+
(input, kargs)=parse_params()
|
35 |
+
pipe=pipeline(model=f'{user}/{model}', **kargs)
|
36 |
+
return pipe(input)
|
37 |
+
|
38 |
+
@app.route("/<task>/<user>/<model>")
|
39 |
+
def run_task_model():
|
40 |
+
(input, kargs)=parse_params()
|
41 |
+
pipe=pipeline(task, model=f'{user}/{model}', **kargs)
|
42 |
+
return pipe(input)
|
43 |
+
|
44 |
+
|
45 |
+
@app.route("/search")
|
46 |
+
def search():
|
47 |
+
response = requests.get(
|
48 |
+
"https://huggingface.co/api/models",
|
49 |
+
params={"limit":20,"full":"True","config":"True", "search": request.args.get("search")},
|
50 |
+
headers={"Authorization":f"Bearer {os.environ.get("token")}"}
|
51 |
+
)
|
52 |
+
return response
|
53 |
+
|
54 |
+
@app.route("/task_list")
|
55 |
+
def tasks():
|
56 |
+
return [item.strip() for item in
|
57 |
+
'''audio-classification
|
58 |
+
automatic-speech-recognition
|
59 |
+
conversational
|
60 |
+
depth-estimation
|
61 |
+
document-question-answering
|
62 |
+
feature-extraction
|
63 |
+
fill-mask
|
64 |
+
image-classification
|
65 |
+
image-feature-extraction
|
66 |
+
image-segmentation
|
67 |
+
image-to-image
|
68 |
+
image-to-text
|
69 |
+
mask-generation
|
70 |
+
object-detection
|
71 |
+
question-answering
|
72 |
+
summarization
|
73 |
+
table-question-answering
|
74 |
+
text2text-generation
|
75 |
+
text-classification (alias sentiment-analysis available)
|
76 |
+
text-generation
|
77 |
+
text-to-audio (alias text-to-speech available)
|
78 |
+
token-classification (alias ner available)
|
79 |
+
translation
|
80 |
+
translation_xx_to_yy
|
81 |
+
video-classification
|
82 |
+
visual-question-answering
|
83 |
+
zero-shot-classification
|
84 |
+
zero-shot-image-classification
|
85 |
+
zero-shot-audio-classification
|
86 |
+
zero-shot-object-detection'''.split("\n")
|
87 |
+
]
|
88 |
+
|
89 |
+
logging.info("xtts is ready")
|