johann22 commited on
Commit
b34cc6a
1 Parent(s): 9ee0176

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -39
app.py CHANGED
@@ -1,39 +1,26 @@
1
- import torch
2
- from transformers import IdeficsForVisionText2Text, AutoProcessor
3
-
4
- device = "cuda" if torch.cuda.is_available() else "cpu"
5
-
6
- checkpoint = "HuggingFaceM4/idefics-9b-instruct"
7
- model = IdeficsForVisionText2Text.from_pretrained(checkpoint, torch_dtype=torch.bfloat16).to(device)
8
- processor = AutoProcessor.from_pretrained(checkpoint)
9
-
10
- # We feed to the model an arbitrary sequence of text strings and images. Images can be either URLs or PIL Images.
11
- prompts = [
12
- [
13
- "User: What is in this image?",
14
- "https://upload.wikimedia.org/wikipedia/commons/8/86/Id%C3%A9fix.JPG",
15
- "<end_of_utterance>",
16
-
17
- "\nAssistant: This picture depicts Idefix, the dog of Obelix in Asterix and Obelix. Idefix is running on the ground.<end_of_utterance>",
18
-
19
- "\nUser:",
20
- "https://static.wikia.nocookie.net/asterix/images/2/25/R22b.gif/revision/latest?cb=20110815073052",
21
- "And who is that?<end_of_utterance>",
22
-
23
- "\nAssistant:",
24
- ],
25
- ]
26
-
27
- # --batched mode
28
- inputs = processor(prompts, add_end_of_utterance_token=False, return_tensors="pt").to(device)
29
- # --single sample mode
30
- # inputs = processor(prompts[0], return_tensors="pt").to(device)
31
-
32
- # Generation args
33
- exit_condition = processor.tokenizer("<end_of_utterance>", add_special_tokens=False).input_ids
34
- bad_words_ids = processor.tokenizer(["<image>", "<fake_token_around_image>"], add_special_tokens=False).input_ids
35
-
36
- generated_ids = model.generate(**inputs, eos_token_id=exit_condition, bad_words_ids=bad_words_ids, max_length=100)
37
- generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
38
- for i, t in enumerate(generated_text):
39
- print(f"{i}:\n{t}\n")
 
1
+ from text_generation import Client
2
+
3
+ API_TOKEN = ""
4
+ API_URL = "https://api-inference.huggingface.co/models/HuggingFaceM4/idefics-80b-instruct"
5
+ DECODING_STRATEGY = "Greedy"
6
+ QUERY = "User: What is in this image?![](https://upload.wikimedia.org/wikipedia/commons/8/86/Id%C3%A9fix.JPG)<end_of_utterance>\nAssistant:"
7
+
8
+ client = Client(
9
+ base_url=API_URL,
10
+ headers={"x-use-cache": "0", "Authorization": f"Bearer {API_TOKEN}"},
11
+ )
12
+ generation_args = {
13
+ "max_new_tokens": 256,
14
+ "repetition_penalty": 1.0,
15
+ "stop_sequences": ["<end_of_utterance>", "\nUser:"],
16
+ }
17
+
18
+ if DECODING_STRATEGY == "Greedy":
19
+ generation_args["do_sample"] = False
20
+ elif DECODING_STRATEGY == "Top P Sampling":
21
+ generation_args["temperature"] = 1.
22
+ generation_args["do_sample"] = True
23
+ generation_args["top_p"] = 0.95
24
+
25
+ generated_text = client.generate(prompt=QUERY, **generation_args)
26
+ print(generated_text)