Spaces:
Build error
Build error
omerXfaruq
commited on
Commit
•
0345768
1
Parent(s):
9b90cbd
The general structures is created.
Browse files- app.py +105 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import requests
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
from huggingface_hub import (
|
8 |
+
hf_hub_url,
|
9 |
+
cached_download,
|
10 |
+
create_repo,
|
11 |
+
get_full_repo_name,
|
12 |
+
upload_file,
|
13 |
+
)
|
14 |
+
|
15 |
+
|
16 |
+
def file_as_a_string(name_list: List[str]) -> str:
|
17 |
+
"""
|
18 |
+
Returns the file that is going to be created in the new space as string.
|
19 |
+
|
20 |
+
:param name_list: list of space names
|
21 |
+
:return: file as a string
|
22 |
+
"""
|
23 |
+
return (
|
24 |
+
f"import gradio as gr"
|
25 |
+
f"\nname_list = {name_list} "
|
26 |
+
f"\ninterfaces = [gr.Interface.load(name) for name in name_list]"
|
27 |
+
f"\ngr.mix.Parallel(*interfaces).launch()"
|
28 |
+
)
|
29 |
+
|
30 |
+
|
31 |
+
def combine_several_space_inputs(names: str) -> str:
|
32 |
+
"""
|
33 |
+
Combines several space inputs and returns the file that is going to be created in the new space as string.
|
34 |
+
|
35 |
+
:param names: space inputs
|
36 |
+
:return: file as a string
|
37 |
+
"""
|
38 |
+
name_list = names.split("\n")
|
39 |
+
interfaces = [gr.Interface.load(name) for name in name_list]
|
40 |
+
if not control_input_and_output_types(interfaces):
|
41 |
+
print("Spaces have different input or output types, could not combine them!")
|
42 |
+
return ""
|
43 |
+
else:
|
44 |
+
return file_as_a_string(name_list)
|
45 |
+
|
46 |
+
|
47 |
+
def control_input_and_output_types(interface_list: List["gr.Interface"]) -> bool:
|
48 |
+
"""
|
49 |
+
Controls whether if input and output types of the given interfaces are the same.
|
50 |
+
|
51 |
+
:param interface_list: list of interfaces
|
52 |
+
:return: True if all input and output types are the same
|
53 |
+
"""
|
54 |
+
first_input_types = [type(input) for input in interface_list[0].input_components]
|
55 |
+
first_output_types = [
|
56 |
+
type(output) for output in interface_list[0].output_components
|
57 |
+
]
|
58 |
+
for interface in interface_list:
|
59 |
+
interface_input_types = [type(input) for input in interface.input_components]
|
60 |
+
if not np.all(
|
61 |
+
interface_input_types == first_input_types
|
62 |
+
): # Vectorize the comparison and don't use double for loop
|
63 |
+
return False
|
64 |
+
interface_output_types = [
|
65 |
+
type(output) for output in interface.output_components
|
66 |
+
]
|
67 |
+
if not np.all(interface_output_types == first_output_types):
|
68 |
+
return False
|
69 |
+
|
70 |
+
return True
|
71 |
+
|
72 |
+
|
73 |
+
def space_builder(spaces, hf_token, username, space_name, space_description):
|
74 |
+
"""
|
75 |
+
Creates a space with given inputs
|
76 |
+
:param spaces:
|
77 |
+
:param hf_token:
|
78 |
+
:param username:
|
79 |
+
:param space_name:
|
80 |
+
:param space_description:
|
81 |
+
:return:
|
82 |
+
"""
|
83 |
+
return "Thank you for using the draft, it will be completed later"
|
84 |
+
# get_full_repo_name(space_name)
|
85 |
+
|
86 |
+
|
87 |
+
iface = gr.Interface(
|
88 |
+
fn=space_builder,
|
89 |
+
inputs=[
|
90 |
+
gr.inputs.Textbox(
|
91 |
+
lines=4,
|
92 |
+
placeholder=(
|
93 |
+
f"Drop space links at each line, ie:"
|
94 |
+
f"\nspaces/Norod78/ComicsHeroHD"
|
95 |
+
f"\nspaces/Amrrs/gradio-sentiment-analyzer"
|
96 |
+
),
|
97 |
+
),
|
98 |
+
gr.inputs.Textbox(lines=1, placeholder="HF Write Token"),
|
99 |
+
gr.inputs.Textbox(lines=1, placeholder="Your username"),
|
100 |
+
gr.inputs.Textbox(lines=1, placeholder="Name for the space"),
|
101 |
+
gr.inputs.Textbox(lines=1, placeholder="Description for the space"),
|
102 |
+
],
|
103 |
+
outputs="text",
|
104 |
+
)
|
105 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
numpy
|
3 |
+
requests
|
4 |
+
typing
|