mirror of
https://github.com/gradio-app/gradio.git
synced 2025-03-07 11:46:51 +08:00
Merge pull request #297 from gradio-app/abidlabs/spaces
Integration with Hugging Face Spaces
This commit is contained in:
commit
bc2e3b6e52
BIN
demo/files/video.avi
Normal file
BIN
demo/files/video.avi
Normal file
Binary file not shown.
BIN
demo/files/video.mp4
Normal file
BIN
demo/files/video.mp4
Normal file
Binary file not shown.
@ -2,6 +2,10 @@ import json
|
||||
import tempfile
|
||||
import requests
|
||||
from gradio import inputs, outputs
|
||||
<<<<<<< HEAD
|
||||
import re
|
||||
=======
|
||||
>>>>>>> e2384b777c6b805bc6e295a8dc003b0a3d0f80c8
|
||||
|
||||
|
||||
def get_huggingface_interface(model_name, api_key, alias):
|
||||
@ -226,9 +230,53 @@ def load_interface(name, src=None, api_key=None, alias=None):
|
||||
interface_info = repos[src](name, api_key, alias)
|
||||
return interface_info
|
||||
|
||||
def interface_params_from_config(config_dict):
|
||||
## instantiate input component and output component
|
||||
config_dict["inputs"] = [inputs.get_input_instance(component) for component in config_dict["input_components"]]
|
||||
config_dict["outputs"] = [outputs.get_output_instance(component) for component in config_dict["output_components"]]
|
||||
# remove preprocessing and postprocessing (since they'll be performed remotely)
|
||||
for component in config_dict["inputs"]:
|
||||
component.preprocess = lambda x:x
|
||||
for component in config_dict["outputs"]:
|
||||
component.postprocess = lambda x:x
|
||||
# Remove keys that are not parameters to Interface() class
|
||||
not_parameters = ("allow_embedding", "allow_interpretation", "avg_durations", "function_count",
|
||||
"queue", "input_components", "output_components", "examples")
|
||||
for key in not_parameters:
|
||||
if key in config_dict:
|
||||
del config_dict[key]
|
||||
return config_dict
|
||||
|
||||
|
||||
def get_spaces_interface(model_name, api_key, alias):
|
||||
iframe_url = "https://huggingface.co/gradioiframe/{}/+".format(model_name)
|
||||
api_url = "https://huggingface.co/gradioiframe/{}/api/predict/".format(model_name)
|
||||
headers = {'Content-Type': 'application/json'}
|
||||
|
||||
r = requests.get(iframe_url)
|
||||
result = re.search('window.config =(.*?);\n', r.text) # some basic regex to extract the config
|
||||
config = json.loads(result.group(1))
|
||||
interface_info = interface_params_from_config(config)
|
||||
|
||||
# The function should call the API with preprocessed data
|
||||
def fn(*data):
|
||||
data = json.dumps({"data": data})
|
||||
response = requests.post(api_url, headers=headers, data=data)
|
||||
result = json.loads(response.content.decode("utf-8"))
|
||||
output = result["data"]
|
||||
if len(interface_info["outputs"])==1: # if the fn is supposed to return a single value, pop it
|
||||
output = output[0]
|
||||
return output
|
||||
interface_info["fn"] = fn
|
||||
|
||||
if alias is not None:
|
||||
interface_info["title"] = alias
|
||||
return interface_info
|
||||
|
||||
repos = {
|
||||
# for each repo, we have a method that returns the Interface given the model name & optionally an api_key
|
||||
"huggingface": get_huggingface_interface,
|
||||
"gradio": get_gradio_interface,
|
||||
"spaces": get_spaces_interface,
|
||||
}
|
||||
|
||||
|
@ -8,4 +8,4 @@
|
||||
window.config = {{ config|tojson }};
|
||||
} catch (e) {
|
||||
window.config = {};
|
||||
}</script><script src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.1/iframeResizer.contentWindow.min.js"></script><title>Gradio</title><link href="static/bundle.css" rel="stylesheet"><link href="static/css/main.c7f424fd.css" rel="stylesheet"></head><body style="height:100%"><div id="root" style="height:100%"></div><script src="static/bundle.js"></script></body></html>
|
||||
}</script><script src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.1/iframeResizer.contentWindow.min.js"></script><title>Gradio</title><link href="static/bundle.css" rel="stylesheet"><link href="static/css/main.c7f424fd.css" rel="stylesheet"></head><body style="height:100%"><div id="root" style="height:100%"></div><script src="static/bundle.js"></script></body></html>
|
||||
|
@ -505,6 +505,12 @@ class CheckboxGroup(InputComponent):
|
||||
final_scores.append(score_set)
|
||||
return final_scores
|
||||
|
||||
@classmethod
|
||||
def get_shortcut_implementations(cls):
|
||||
return {
|
||||
"checkboxgroup": {},
|
||||
}
|
||||
|
||||
def embed(self, x):
|
||||
if self.type == "value":
|
||||
return [float(choice in x) for choice in self.choices]
|
||||
@ -581,6 +587,12 @@ class Radio(InputComponent):
|
||||
scores.insert(self.choices.index(x), None)
|
||||
return scores
|
||||
|
||||
@classmethod
|
||||
def get_shortcut_implementations(cls):
|
||||
return {
|
||||
"radio": {},
|
||||
}
|
||||
|
||||
def embed(self, x):
|
||||
if self.type == "value":
|
||||
return [float(choice == x) for choice in self.choices]
|
||||
@ -648,6 +660,12 @@ class Dropdown(InputComponent):
|
||||
scores.insert(self.choices.index(x), None)
|
||||
return scores
|
||||
|
||||
@classmethod
|
||||
def get_shortcut_implementations(cls):
|
||||
return {
|
||||
"dropdown": {},
|
||||
}
|
||||
|
||||
def embed(self, x):
|
||||
if self.type == "value":
|
||||
return [float(choice == x) for choice in self.choices]
|
||||
@ -942,7 +960,8 @@ class Audio(InputComponent):
|
||||
def get_shortcut_implementations(cls):
|
||||
return {
|
||||
"audio": {},
|
||||
"microphone": {"source": "microphone"}
|
||||
"microphone": {"source": "microphone"},
|
||||
"mic": {"source": "microphone"}
|
||||
}
|
||||
|
||||
def preprocess(self, x):
|
||||
@ -1288,8 +1307,13 @@ def get_input_instance(iface):
|
||||
shortcut = InputComponent.get_all_shortcut_implementations()[
|
||||
iface]
|
||||
return shortcut[0](**shortcut[1])
|
||||
elif isinstance(iface, dict): # a dict with `name` as the input component type and other keys as parameters
|
||||
name = iface.pop('name')
|
||||
component, params = InputComponent.get_all_shortcut_implementations()[name]
|
||||
params.update(**iface)
|
||||
return component(**params)
|
||||
elif isinstance(iface, InputComponent):
|
||||
return iface
|
||||
else:
|
||||
raise ValueError("Input interface must be of type `str` or "
|
||||
raise ValueError("Input interface must be of type `str` or `dict` or "
|
||||
"`InputComponent` but is {}".format(iface))
|
||||
|
@ -569,6 +569,13 @@ class Carousel(OutputComponent):
|
||||
**super().get_template_context()
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def get_shortcut_implementations(cls):
|
||||
return {
|
||||
"carousel": {},
|
||||
}
|
||||
|
||||
|
||||
def postprocess(self, y):
|
||||
if isinstance(y, list):
|
||||
if len(y) != 0 and not isinstance(y[0], list):
|
||||
@ -597,12 +604,17 @@ def get_output_instance(iface):
|
||||
if isinstance(iface, str):
|
||||
shortcut = OutputComponent.get_all_shortcut_implementations()[iface]
|
||||
return shortcut[0](**shortcut[1])
|
||||
elif isinstance(iface, dict): # a dict with `name` as the output component type and other keys as parameters
|
||||
name = iface.pop('name')
|
||||
component, params = OutputComponent.get_all_shortcut_implementations()[name]
|
||||
params.update(**iface)
|
||||
return component(**params)
|
||||
elif isinstance(iface, OutputComponent):
|
||||
return iface
|
||||
else:
|
||||
raise ValueError(
|
||||
"Output interface must be of type `str` or "
|
||||
"`OutputComponent`"
|
||||
"Output interface must be of type `str` or `dict` or"
|
||||
"`OutputComponent` but is {}".format(iface)
|
||||
)
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user