Views
No views yet
1import os
2import torch
3from datetime import datetime, timedelta
4from transformers import Mistral3ForConditionalGeneration
5from mistral_common.protocol.instruct.request import ChatCompletionRequest
6from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
7
8
9def load_system_prompt(repo_id_or_path: str, filename: str) -> str:
10 from huggingface_hub import hf_hub_download
11
12 if os.path.isdir(repo_id_or_path):
13 file_path = os.path.join(repo_id_or_path, filename)
14 else:
15 file_path = hf_hub_download(repo_id=repo_id_or_path, filename=filename)
16 with open(file_path, "r") as file:
17 system_prompt = file.read()
18 today = datetime.today().strftime("%Y-%m-%d")
19 yesterday = (datetime.today() - timedelta(days=1)).strftime("%Y-%m-%d")
20 model_name = repo_id_or_path.split("/")[-1]
21 return system_prompt.format(name=model_name, today=today, yesterday=yesterday)
22
23model_id = "Intel/Magistral-Small-2509-int4-AutoRound"
24model = Mistral3ForConditionalGeneration.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto")
25if os.path.isdir(model_id):
26 tokenizer = MistralTokenizer.from_file(os.path.join(model_id, "tekken.json"))
27else:
28 tokenizer = MistralTokenizer.from_hf_hub(model_id)
29
30SYSTEM_PROMPT = load_system_prompt(model.name_or_path, "SYSTEM_PROMPT.txt")
31
32content = "Which of the depicted countries has the best food? Which the second and third and fourth? Name the country, its color on the map and one its city that is visible on the map, but is not the capital. Make absolutely sure to only name a city that can be seen on the map."
33image_url = "https://huggingface.co/datasets/patrickvonplaten/random_img/resolve/main/europe.png"
34conversation = [{"role": "system", "content": SYSTEM_PROMPT}]
35conversation.append({
36 "role": "user",
37 "content": [{
38 "type": "text",
39 "text": content
40 }, {
41 "type": "image_url",
42 "image_url": {
43 "url": image_url
44 }
45 }],
46},)
47tokenized = tokenizer.encode_chat_completion(ChatCompletionRequest(messages=conversation, continue_final_message=False))
48breakpoint()
49input_ids = torch.tensor([tokenized.tokens])
50attention_mask = torch.ones_like(input_ids)
51pixel_values = torch.tensor(tokenized.images[0], dtype=torch.bfloat16).unsqueeze(0)
52image_sizes = torch.tensor([pixel_values.shape[-2:]])
53ret = {
54 "input_ids": input_ids,
55 "attention_mask": attention_mask,
56 "pixel_values": pixel_values,
57 "image_sizes": image_sizes,
58}
59for key in ret:
60 if isinstance(ret[key], torch.Tensor):
61 ret[key] = ret[key].to(model.device)
62with torch.inference_mode():
63 generation = model.generate(**ret, max_new_tokens=512, do_sample=True)
64
65decoded = tokenizer.decode(generation)
66print(decoded)
67
68
69"""
70Alright, the question is about identifying the best, second best, third best, and fourth best countries in terms of food from the depicted map. The user also wants the country's color on the map and a non-capital city visible on the map for each.
71
72First, let's list out the countries visible on the map and their colors:
73
741. Iceland - light purple
752. Ireland - dark green
763. United Kingdom - yellow
774. France - brown
785. Spain - yellow
796. Portugal - yellow
807. Italy - orange
818. Greece - light blue
829. Germany - orange
8310. Switzerland - light green
8411. Austria - light green
8512. Netherlands - orange
8613. Belgium - light blue
8714. Luxembourg - light blue
8815. Denmark - light blue
8916. Poland - light green
9017. Czech Republic - purple
9118. Slovakia - purple
9219. Hungary - purple
9320. Romania - light green
9421. Bulgaria - dark brown
9522. Russia - red
9623. Finland - light green
9724. Sweden - dark purple
9825. Norway - dark green
9926. Denmark - light blue
10027. Estonia - light blue
10128. Latvia - light blue
10229. Lithuania - light blue
10330. Belarus - dark purple
10431. Ukraine - yellow
10532. Moldova - yellow
10633. Turkey - light blue (partially visible)
107
108Now, let's consider the countries known for their food. This is subjective, but generally, Italy, France, Spain, and Greece are often praised for their cuisine.
109
1101. Italy (orange) - Known for pasta, pizza, risotto, etc.
1112. France (brown) - Known for wine, cheese, pastries, etc.
1123. Spain (yellow) - Known for paella, tapas, jamón, etc.
1134. Greece (light blue) - Known for gyros, souvlaki, feta, etc.
114
115Now, we need to pick one non-capital city for each that is visible on the map.
116
1171. Italy: Milan (not the capital, Rome is the capital)
1182. France: Lyon (not the capital, Paris is the capital)
1193. Spain: Barcelona (not the capital, Madrid is the capital)
1204. Greece: Thessaloniki (not the capital, Athens is the capital)
121
122Now, let's format the response:
123
1241. First: Italy, color: orange, city: Milan
1252. Second: France, color: brown, city: Lyon
1263. Third: Spain, color: yellow, city: Barcelona
1274. Fourth: Greece, color: light blue, city: Thessaloniki
128
129But wait, the user asked for the best, second best, third best, and fourth best. We need to rank them. Let's assume the following ranking based on general popularity and global influence of cuisine:
130
1311. Italy - often considered the best
1322. France - often considered the second best
1333. Spain - often considered the third best
1344. Greece - often considered the fourth best
135
136Now, let's prepare the response.1. **Italy** - Color: orange, City: Milan
1372. **France** - Color: brown, City: Lyon
1383. **Spain** - Color: yellow, City: Barcelona
1394. **Greece** - Color: light blue, City: Thessaloniki
140
141"""auto_round --model /models/Magistral-Small-2509/ --iters 200 --seqlen 1024 --output_dir tmp_autoround