broadfield-dev commited on
Commit
c7b1ee6
Β·
verified Β·
1 Parent(s): 83ef011

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoProcessor, Gemma3nForConditionalGeneration
2
+ from PIL import Image
3
+ import requests
4
+ import torch
5
+
6
+ model_id = "google/gemma-3n-e4b-it"
7
+
8
+ model = Gemma3nForConditionalGeneration.from_pretrained(model_id, device_map="auto", torch_dtype=torch.bfloat16,).eval()
9
+
10
+ processor = AutoProcessor.from_pretrained(model_id)
11
+
12
+ messages = [
13
+ {
14
+ "role": "system",
15
+ "content": [{"type": "text", "text": "You are a helpful assistant."}]
16
+ },
17
+ {
18
+ "role": "user",
19
+ "content": [
20
+ {"type": "image", "image": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg"},
21
+ {"type": "text", "text": "Describe this image in detail."}
22
+ ]
23
+ }
24
+ ]
25
+
26
+ inputs = processor.apply_chat_template(
27
+ messages,
28
+ add_generation_prompt=True,
29
+ tokenize=True,
30
+ return_dict=True,
31
+ return_tensors="pt",
32
+ ).to(model.device)
33
+
34
+ input_len = inputs["input_ids"].shape[-1]
35
+
36
+ with torch.inference_mode():
37
+ generation = model.generate(**inputs, max_new_tokens=100, do_sample=False)
38
+ generation = generation[0][input_len:]
39
+
40
+ decoded = processor.decode(generation, skip_special_tokens=True)
41
+ print(decoded)
42
+
43
+ # **Overall Impression:** The image is a close-up shot of a vibrant garden scene,
44
+ # focusing on a cluster of pink cosmos flowers and a busy bumblebee.
45
+ # It has a slightly soft, natural feel, likely captured in daylight.