Spaces:
Sleeping
Sleeping
Commit
·
1dd7eb5
1
Parent(s):
bbeae41
Modify to use Hugging Face ZeRO GPU
Browse files- app.py +9 -10
- requirements.txt +6 -3
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import list_models
|
| 3 |
from typing import List
|
|
@@ -22,9 +23,7 @@ dataset = None
|
|
| 22 |
def load_merit_dataset():
|
| 23 |
global dataset
|
| 24 |
if dataset is None:
|
| 25 |
-
dataset = load_dataset(
|
| 26 |
-
"de-Rodrigo/merit", name="en-digital-seq", split="train", num_proc=8
|
| 27 |
-
)
|
| 28 |
return dataset
|
| 29 |
|
| 30 |
|
|
@@ -42,6 +41,7 @@ def get_collection_models(tag: str) -> List[str]:
|
|
| 42 |
return [model.modelId for model in models if tag in model.tags]
|
| 43 |
|
| 44 |
|
|
|
|
| 45 |
def get_donut():
|
| 46 |
global donut_model, donut_processor
|
| 47 |
if donut_model is None or donut_processor is None:
|
|
@@ -50,28 +50,26 @@ def get_donut():
|
|
| 50 |
"de-Rodrigo/donut-merit"
|
| 51 |
)
|
| 52 |
donut_processor = DonutProcessor.from_pretrained("de-Rodrigo/donut-merit")
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
logger.info("Donut model loaded successfully")
|
| 56 |
except Exception as e:
|
| 57 |
logger.error(f"Error loading Donut model: {str(e)}")
|
| 58 |
raise
|
| 59 |
return donut_model, donut_processor
|
| 60 |
|
| 61 |
|
|
|
|
| 62 |
def process_image_donut(model, processor, image):
|
| 63 |
try:
|
| 64 |
if not isinstance(image, Image.Image):
|
| 65 |
image = Image.fromarray(image)
|
| 66 |
|
| 67 |
-
pixel_values = processor(image, return_tensors="pt").pixel_values
|
| 68 |
-
if torch.cuda.is_available():
|
| 69 |
-
pixel_values = pixel_values.to("cuda")
|
| 70 |
|
| 71 |
task_prompt = "<s_cord-v2>"
|
| 72 |
decoder_input_ids = processor.tokenizer(
|
| 73 |
task_prompt, add_special_tokens=False, return_tensors="pt"
|
| 74 |
-
)["input_ids"]
|
| 75 |
|
| 76 |
outputs = model.generate(
|
| 77 |
pixel_values,
|
|
@@ -99,6 +97,7 @@ def process_image_donut(model, processor, image):
|
|
| 99 |
return f"Error: {str(e)}"
|
| 100 |
|
| 101 |
|
|
|
|
| 102 |
def process_image(model_name, image=None, dataset_image_index=None):
|
| 103 |
if dataset_image_index is not None:
|
| 104 |
image = get_image_from_dataset(dataset_image_index)
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import list_models
|
| 4 |
from typing import List
|
|
|
|
| 23 |
def load_merit_dataset():
|
| 24 |
global dataset
|
| 25 |
if dataset is None:
|
| 26 |
+
dataset = load_dataset("de-Rodrigo/merit", name="en-digital-seq", split="train")
|
|
|
|
|
|
|
| 27 |
return dataset
|
| 28 |
|
| 29 |
|
|
|
|
| 41 |
return [model.modelId for model in models if tag in model.tags]
|
| 42 |
|
| 43 |
|
| 44 |
+
@spaces.GPU
|
| 45 |
def get_donut():
|
| 46 |
global donut_model, donut_processor
|
| 47 |
if donut_model is None or donut_processor is None:
|
|
|
|
| 50 |
"de-Rodrigo/donut-merit"
|
| 51 |
)
|
| 52 |
donut_processor = DonutProcessor.from_pretrained("de-Rodrigo/donut-merit")
|
| 53 |
+
donut_model = donut_model.to("cuda")
|
| 54 |
+
logger.info("Donut model loaded successfully on GPU")
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
logger.error(f"Error loading Donut model: {str(e)}")
|
| 57 |
raise
|
| 58 |
return donut_model, donut_processor
|
| 59 |
|
| 60 |
|
| 61 |
+
@spaces.GPU
|
| 62 |
def process_image_donut(model, processor, image):
|
| 63 |
try:
|
| 64 |
if not isinstance(image, Image.Image):
|
| 65 |
image = Image.fromarray(image)
|
| 66 |
|
| 67 |
+
pixel_values = processor(image, return_tensors="pt").pixel_values.to("cuda")
|
|
|
|
|
|
|
| 68 |
|
| 69 |
task_prompt = "<s_cord-v2>"
|
| 70 |
decoder_input_ids = processor.tokenizer(
|
| 71 |
task_prompt, add_special_tokens=False, return_tensors="pt"
|
| 72 |
+
)["input_ids"].to("cuda")
|
| 73 |
|
| 74 |
outputs = model.generate(
|
| 75 |
pixel_values,
|
|
|
|
| 97 |
return f"Error: {str(e)}"
|
| 98 |
|
| 99 |
|
| 100 |
+
@spaces.GPU
|
| 101 |
def process_image(model_name, image=None, dataset_image_index=None):
|
| 102 |
if dataset_image_index is not None:
|
| 103 |
image = get_image_from_dataset(dataset_image_index)
|
requirements.txt
CHANGED
|
@@ -1,7 +1,10 @@
|
|
| 1 |
-
gradio
|
| 2 |
transformers
|
| 3 |
huggingface_hub
|
| 4 |
-
torch
|
| 5 |
numpy
|
| 6 |
Pillow
|
| 7 |
-
datasets
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
transformers
|
| 3 |
huggingface_hub
|
| 4 |
+
torch>=2.0.0,!=2.3.0,<2.5.0
|
| 5 |
numpy
|
| 6 |
Pillow
|
| 7 |
+
datasets
|
| 8 |
+
deepspeed
|
| 9 |
+
huggingface-hub[cli,torch]
|
| 10 |
+
huggingface_hub[spaces]
|