Spaces:
Running
Running
æLtorio
commited on
Commit
•
4784163
1
Parent(s):
7c2ecba
wip
Browse files
app.py
CHANGED
@@ -2,15 +2,16 @@ import gradio as gr
|
|
2 |
from transformers import AutoProcessor, Idefics3ForConditionalGeneration, image_utils
|
3 |
import torch
|
4 |
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
|
|
5 |
model_id="eltorio/IDEFICS3_ROCO"
|
6 |
# model = AutoModelForImageTextToText.from_pretrained(model_id).to(device)
|
7 |
base_model_path="HuggingFaceM4/Idefics3-8B-Llama3" #or change to local path
|
8 |
-
processor = AutoProcessor.from_pretrained(base_model_path)
|
9 |
model = Idefics3ForConditionalGeneration.from_pretrained(
|
10 |
base_model_path, torch_dtype=torch.bfloat16
|
11 |
).to(device)
|
12 |
|
13 |
-
model.load_adapter(model_id)
|
14 |
|
15 |
def infere(image):
|
16 |
messages = [
|
@@ -24,10 +25,11 @@ def infere(image):
|
|
24 |
]
|
25 |
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
26 |
inputs = processor(text=prompt, images=[image], return_tensors="pt")
|
|
|
27 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
28 |
generated_ids = model.generate(**inputs, max_new_tokens=8192)
|
29 |
generated_texts = processor.batch_decode(generated_ids, skip_special_tokens=True)
|
30 |
return generated_texts
|
31 |
|
32 |
-
|
33 |
-
|
|
|
2 |
from transformers import AutoProcessor, Idefics3ForConditionalGeneration, image_utils
|
3 |
import torch
|
4 |
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
5 |
+
print(f"Using device: {device}")
|
6 |
model_id="eltorio/IDEFICS3_ROCO"
|
7 |
# model = AutoModelForImageTextToText.from_pretrained(model_id).to(device)
|
8 |
base_model_path="HuggingFaceM4/Idefics3-8B-Llama3" #or change to local path
|
9 |
+
processor = AutoProcessor.from_pretrained(base_model_path, trust_remote_code=True)
|
10 |
model = Idefics3ForConditionalGeneration.from_pretrained(
|
11 |
base_model_path, torch_dtype=torch.bfloat16
|
12 |
).to(device)
|
13 |
|
14 |
+
model.load_adapter(model_id,device_map="auto")
|
15 |
|
16 |
def infere(image):
|
17 |
messages = [
|
|
|
25 |
]
|
26 |
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
27 |
inputs = processor(text=prompt, images=[image], return_tensors="pt")
|
28 |
+
print(f"inputs: {inputs}")
|
29 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
30 |
generated_ids = model.generate(**inputs, max_new_tokens=8192)
|
31 |
generated_texts = processor.batch_decode(generated_ids, skip_special_tokens=True)
|
32 |
return generated_texts
|
33 |
|
34 |
+
radiotest = gr.Interface(fn=infere, inputs="image", outputs="text")
|
35 |
+
radiotest.launch()
|