Delete src/model.py
Browse files- src/model.py +0 -60
src/model.py
DELETED
@@ -1,60 +0,0 @@
|
|
1 |
-
# Importing necessary libraries
|
2 |
-
import gradio as gr
|
3 |
-
import subprocess
|
4 |
-
import spaces
|
5 |
-
from transformers import AutoProcessor, AutoModelForCausalLM
|
6 |
-
|
7 |
-
|
8 |
-
# Install the required dependencies
|
9 |
-
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
10 |
-
|
11 |
-
# Load model and processor from Hugging Face
|
12 |
-
model_id = "microsoft/Florence-2-large-ft"
|
13 |
-
model = (
|
14 |
-
AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True).to("cuda").eval()
|
15 |
-
)
|
16 |
-
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
17 |
-
|
18 |
-
|
19 |
-
@spaces.GPU(duration=120)
|
20 |
-
def run_example(task_prompt, image, text_input=None):
|
21 |
-
"""
|
22 |
-
Runs an example using the given task prompt and image.
|
23 |
-
|
24 |
-
Args:
|
25 |
-
- task_prompt (str): The task prompt for the example.
|
26 |
-
- image (PIL.Image.Image): The image to be processed.
|
27 |
-
- text_input (str, optional): Additional text input to be appended to the task prompt. Defaults to None.
|
28 |
-
|
29 |
-
Returns:
|
30 |
-
str: The parsed answer generated by the model.
|
31 |
-
"""
|
32 |
-
# Check if the image is provided
|
33 |
-
if not image:
|
34 |
-
raise gr.Error("No image provided")
|
35 |
-
|
36 |
-
# If there is no text input, use the task prompt as the prompt
|
37 |
-
if text_input is None:
|
38 |
-
prompt = task_prompt
|
39 |
-
else:
|
40 |
-
prompt = task_prompt + text_input
|
41 |
-
|
42 |
-
# Process the image and text input
|
43 |
-
inputs = processor(text=prompt, images=image, return_tensors="pt").to("cuda")
|
44 |
-
|
45 |
-
# Generate the answer using the model
|
46 |
-
generated_ids = model.generate(
|
47 |
-
input_ids=inputs["input_ids"],
|
48 |
-
pixel_values=inputs["pixel_values"],
|
49 |
-
max_new_tokens=1024,
|
50 |
-
early_stopping=False,
|
51 |
-
do_sample=False,
|
52 |
-
num_beams=3,
|
53 |
-
)
|
54 |
-
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
55 |
-
parsed_answer = processor.post_process_generation(
|
56 |
-
generated_text, task=task_prompt, image_size=(image.width, image.height)
|
57 |
-
)
|
58 |
-
|
59 |
-
# Return the parsed answer
|
60 |
-
return parsed_answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|