DouDou commited on
Commit
3562304
·
verified ·
1 Parent(s): be0bdd8

Upload data3/gemini.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. data3/gemini.py +39 -0
data3/gemini.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import vertexai
2
+ from vertexai.generative_models import GenerativeModel
3
+
4
+ # export GOOGLE_APPLICATION_CREDENTIALS="/home/weifengsun/tangou1/step2/gemini.json"
5
+
6
+ # You can find your project ID by running `gcloud config get-value project` in the terminal
7
+ vertexai.init(project="tangou")
8
+ model = GenerativeModel("gemini-2.5-flash")
9
+ response = model.generate_content("hello")
10
+ print(response.text)
11
+
12
+ # 打印Token使用情况
13
+ print("\n" + "="*50)
14
+ print("Token Usage Information:")
15
+ print("="*50)
16
+
17
+ # 获取usage metadata
18
+ usage_metadata = response.usage_metadata
19
+ print(f"输入Token数 (Prompt Tokens): {usage_metadata.prompt_token_count}")
20
+ print(f"输出Token数 (Completion Tokens): {usage_metadata.candidates_token_count}")
21
+ print(f"总Token数 (Total Tokens): {usage_metadata.total_token_count}")
22
+
23
+ # 计算API价格 (Gemini 2.5 Flash定价)
24
+ # 输入: $0.075 per 1M tokens
25
+ # 输出: $0.30 per 1M tokens
26
+ input_price_per_million = 0.075
27
+ output_price_per_million = 0.30
28
+
29
+ input_cost = (usage_metadata.prompt_token_count / 1_000_000) * input_price_per_million
30
+ output_cost = (usage_metadata.candidates_token_count / 1_000_000) * output_price_per_million
31
+ total_cost = input_cost + output_cost
32
+
33
+ print("\n" + "="*50)
34
+ print("API Price Calculation:")
35
+ print("="*50)
36
+ print(f"输入Token价格: ${input_cost:.8f}")
37
+ print(f"输出Token价格: ${output_cost:.8f}")
38
+ print(f"总价格: ${total_cost:.8f}")
39
+ print("="*50)