GPTfree api
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,35 @@
|
|
|
|
1 |
from vllm import LLM
|
2 |
from vllm.sampling_params import SamplingParams
|
3 |
-
from huggingface_hub import hf_hub_download
|
4 |
from datetime import datetime, timedelta
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
model_name = "mistralai/Pixtral-Large-Instruct-2411"
|
7 |
|
8 |
def load_system_prompt(repo_id: str, filename: str) -> str:
|
|
|
9 |
file_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
10 |
with open(file_path, 'r') as file:
|
11 |
system_prompt = file.read()
|
|
|
12 |
today = datetime.today().strftime('%Y-%m-%d')
|
13 |
yesterday = (datetime.today() - timedelta(days=1)).strftime('%Y-%m-%d')
|
14 |
model_name = repo_id.split("/")[-1]
|
15 |
return system_prompt.format(name=model_name, today=today, yesterday=yesterday)
|
16 |
|
|
|
17 |
SYSTEM_PROMPT = load_system_prompt(model_name, "SYSTEM_PROMPT.txt")
|
18 |
|
|
|
19 |
image_url = "https://huggingface.co/datasets/patrickvonplaten/random_img/resolve/main/europe.png"
|
20 |
|
|
|
21 |
messages = [
|
22 |
{"role": "system", "content": SYSTEM_PROMPT},
|
23 |
{
|
@@ -32,11 +44,21 @@ messages = [
|
|
32 |
},
|
33 |
]
|
34 |
|
|
|
35 |
sampling_params = SamplingParams(max_tokens=512)
|
36 |
|
37 |
-
#
|
38 |
-
llm = LLM(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
|
|
40 |
outputs = llm.chat(messages, sampling_params=sampling_params)
|
41 |
|
|
|
42 |
print(outputs[0].outputs[0].text)
|
|
|
1 |
+
import os
|
2 |
from vllm import LLM
|
3 |
from vllm.sampling_params import SamplingParams
|
4 |
+
from huggingface_hub import hf_hub_download, login
|
5 |
from datetime import datetime, timedelta
|
6 |
|
7 |
+
# Hugging Face トークンでログイン
|
8 |
+
hf_token = os.getenv("HF_TOKEN")
|
9 |
+
if not hf_token:
|
10 |
+
raise ValueError("Hugging Face token is not set in environment variables.")
|
11 |
+
login(hf_token)
|
12 |
+
|
13 |
model_name = "mistralai/Pixtral-Large-Instruct-2411"
|
14 |
|
15 |
def load_system_prompt(repo_id: str, filename: str) -> str:
|
16 |
+
"""指定されたリポジトリからSYSTEM_PROMPT.txtをダウンロードし、フォーマット済みプロンプトを返す"""
|
17 |
file_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
18 |
with open(file_path, 'r') as file:
|
19 |
system_prompt = file.read()
|
20 |
+
# 日付とモデル名でフォーマット
|
21 |
today = datetime.today().strftime('%Y-%m-%d')
|
22 |
yesterday = (datetime.today() - timedelta(days=1)).strftime('%Y-%m-%d')
|
23 |
model_name = repo_id.split("/")[-1]
|
24 |
return system_prompt.format(name=model_name, today=today, yesterday=yesterday)
|
25 |
|
26 |
+
# SYSTEM_PROMPT をロード
|
27 |
SYSTEM_PROMPT = load_system_prompt(model_name, "SYSTEM_PROMPT.txt")
|
28 |
|
29 |
+
# 画像URL
|
30 |
image_url = "https://huggingface.co/datasets/patrickvonplaten/random_img/resolve/main/europe.png"
|
31 |
|
32 |
+
# メッセージリスト
|
33 |
messages = [
|
34 |
{"role": "system", "content": SYSTEM_PROMPT},
|
35 |
{
|
|
|
44 |
},
|
45 |
]
|
46 |
|
47 |
+
# サンプリング設定
|
48 |
sampling_params = SamplingParams(max_tokens=512)
|
49 |
|
50 |
+
# LLMの初期化 (GPUを利用)
|
51 |
+
llm = LLM(
|
52 |
+
model=model_name,
|
53 |
+
config_format="mistral",
|
54 |
+
load_format="mistral",
|
55 |
+
tokenizer_mode="mistral",
|
56 |
+
tensor_parallel_size=8, # GPUを8個使用
|
57 |
+
limit_mm_per_prompt={"image": 4} # マルチモーダル入力制限
|
58 |
+
)
|
59 |
|
60 |
+
# メッセージを送信し、応答を取得
|
61 |
outputs = llm.chat(messages, sampling_params=sampling_params)
|
62 |
|
63 |
+
# 結果を表示
|
64 |
print(outputs[0].outputs[0].text)
|