GPTfree api commited on
Commit
e717066
·
verified ·
1 Parent(s): ea1604a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -39
app.py CHANGED
@@ -1,44 +1,21 @@
 
1
  import os
2
- import tempfile
3
- from vllm import LLM
4
- from vllm.sampling_params import SamplingParams
5
- from datetime import datetime, timedelta
6
 
7
- # モデル名
8
- model_name = "mistralai/Mistral-7B-Instruct-v0.2"
 
9
 
10
- # SYSTEM_PROMPTのロード関数 (ローカルファイルを読み込む)
11
- def load_system_prompt(file_path: str) -> str:
12
- with open(file_path, 'r') as file:
13
- system_prompt = file.read()
14
- today = datetime.today().strftime('%Y-%m-%d')
15
- yesterday = (datetime.today() - timedelta(days=1)).strftime('%Y-%m-%d')
16
- return system_prompt.format(name=model_name, today=today, yesterday=yesterday)
17
 
18
- # SYSTEM_PROMPT.txtは現在のディレクトリ内にあることを想定
19
- system_prompt_path = "./SYSTEM_PROMPT.txt" # 現在のディレクトリ内のファイルパス
20
- SYSTEM_PROMPT = load_system_prompt(system_prompt_path)
21
 
22
- # 一時ディレクトリをキャッシュ用に設定
23
- with tempfile.TemporaryDirectory() as tmpdirname:
24
- os.environ["TRANSFORMERS_CACHE"] = tmpdirname
25
- os.environ["HF_HOME"] = tmpdirname
26
- os.environ["HF_TOKEN"] = os.getenv("HF_TOKEN") # 環境変数からトークンを取得
27
-
28
- # メッセージリストの作成
29
- messages = [
30
- {"role": "system", "content": SYSTEM_PROMPT},
31
- {
32
- "role": "user",
33
- "content": "Which of the depicted countries has the best food?",
34
- },
35
- ]
36
-
37
- sampling_params = SamplingParams(max_tokens=512)
38
-
39
- # モデルロードと実行
40
- llm = LLM(model=model_name, trust_remote_code=True, tensor_parallel_size=1, device="cpu")
41
- outputs = llm.chat(messages, sampling_params=sampling_params)
42
-
43
- # 出力を表示
44
- print(outputs[0].outputs[0].text)
 
1
+ import requests
2
  import os
 
 
 
 
3
 
4
+ # Hugging Face APIエンドポイントと認証トークン
5
+ api_url = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
6
+ headers = {"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
7
 
8
+ # クエリのメッセージ
9
+ payload = {
10
+ "inputs": "Which of the depicted countries has the best food?",
11
+ "parameters": {"max_new_tokens": 512}
12
+ }
 
 
13
 
14
+ # APIリクエスト
15
+ response = requests.post(api_url, headers=headers, json=payload)
 
16
 
17
+ # 結果表示
18
+ if response.status_code == 200:
19
+ print("Response:", response.json())
20
+ else:
21
+ print(f"Error {response.status_code}: {response.text}")