spoon-1 commited on
Commit
e2c283d
·
verified ·
1 Parent(s): 8f64074

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -15
app.py CHANGED
@@ -6,8 +6,9 @@ def fetch_gemini_models(api_key):
6
  """
7
  Fetches available Gemini models and returns them as a pandas DataFrame.
8
  """
 
9
  if not api_key:
10
- return None, "Error: GEMINI_API_KEY is required."
11
 
12
  try:
13
  client = genai.Client(api_key=api_key)
@@ -31,19 +32,40 @@ def fetch_gemini_models(api_key):
31
  methods = ", ".join(translated_methods)
32
 
33
  data.append({
34
- "Model Name": m.name,
35
- "Display Name": m.display_name,
36
- "Supported Methods (JP)": methods
37
  })
38
 
39
  if not data:
40
- return None, "No models found that support 'generateContent'."
41
 
42
  df = pd.DataFrame(data)
43
- return df, "Success: Models fetched successfully."
44
 
 
45
  except Exception as e:
46
- return None, f"An error occurred: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  # Define Gradio Theme (Rich Aesthetics)
49
  theme = gr.themes.Soft(
@@ -55,26 +77,26 @@ theme = gr.themes.Soft(
55
  button_primary_background_fill_hover='*primary_700',
56
  )
57
 
58
- with gr.Blocks(theme=theme, title="Gemini Model Checker") as demo:
59
  gr.Markdown(
60
  """
61
- # 💎 Gemini Model Checker
62
  Hugging Face Spaces上のGradioで、利用可能なGeminiモデルを簡単に確認できます。
63
  """
64
  )
65
 
66
  with gr.Row():
67
  api_key_input = gr.Textbox(
68
- label="Gemini API Key",
69
  placeholder="AIzaSy...",
70
  type="password",
71
  interactive=True,
72
  scale=4
73
  )
74
- fetch_btn = gr.Button("Fetch Models", variant="primary", scale=1)
75
 
76
- output_status = gr.Textbox(label="Status", interactive=False)
77
- output_table = gr.DataFrame(label="Available Models")
78
 
79
  fetch_btn.click(
80
  fn=fetch_gemini_models,
@@ -82,13 +104,31 @@ with gr.Blocks(theme=theme, title="Gemini Model Checker") as demo:
82
  outputs=[output_table, output_status]
83
  )
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  gr.Markdown(
86
  """
87
  ---
88
  ### 使い方
89
  1. Google AI Studioから取得した **APIキー** を入力します。
90
- 2. **Fetch Models** ボタンをクリックします。
91
- 3. 利用可能なモデルの一覧が表示されます。
92
  """
93
  )
94
 
 
6
  """
7
  Fetches available Gemini models and returns them as a pandas DataFrame.
8
  """
9
+
10
  if not api_key:
11
+ return None, "エラー: GEMINI_API_KEY が必要です。"
12
 
13
  try:
14
  client = genai.Client(api_key=api_key)
 
32
  methods = ", ".join(translated_methods)
33
 
34
  data.append({
35
+ "モデル名": m.name,
36
+ "表示名": m.display_name,
37
+ "対応メソッド": methods
38
  })
39
 
40
  if not data:
41
+ return None, "'generateContent' をサポートするモデルが見つかりませんでした。"
42
 
43
  df = pd.DataFrame(data)
44
+ return df, "成功: モデルの取得に成功しました。"
45
 
46
+
47
  except Exception as e:
48
+ return None, f"エラーが発生しました: {str(e)}"
49
+
50
+ def check_model_access(api_key, model_name):
51
+ """
52
+ Checks if the API key has access to the specified model by attempting a simple generation.
53
+ """
54
+ if not api_key:
55
+ return "エラー: GEMINI_API_KEY が必要です。"
56
+ if not model_name:
57
+ return "エラー: モデル名が必要です。"
58
+
59
+ try:
60
+ client = genai.Client(api_key=api_key)
61
+ # Try a minimal generation request
62
+ response = client.models.generate_content(
63
+ model=model_name,
64
+ contents="test",
65
+ )
66
+ return f"成功: {model_name} へのアクセスが確認されました。応答を受信しました。"
67
+ except Exception as e:
68
+ return f"アクセス確認失敗: {str(e)}"
69
 
70
  # Define Gradio Theme (Rich Aesthetics)
71
  theme = gr.themes.Soft(
 
77
  button_primary_background_fill_hover='*primary_700',
78
  )
79
 
80
+ with gr.Blocks(theme=theme, title="Gemini モデルチェッカー") as demo:
81
  gr.Markdown(
82
  """
83
+ # 💎 Gemini モデルチェッカー
84
  Hugging Face Spaces上のGradioで、利用可能なGeminiモデルを簡単に確認できます。
85
  """
86
  )
87
 
88
  with gr.Row():
89
  api_key_input = gr.Textbox(
90
+ label="Gemini APIキー",
91
  placeholder="AIzaSy...",
92
  type="password",
93
  interactive=True,
94
  scale=4
95
  )
96
+ fetch_btn = gr.Button("モデル一覧取得", variant="primary", scale=1)
97
 
98
+ output_status = gr.Textbox(label="ステータス", interactive=False)
99
+ output_table = gr.DataFrame(label="利用可能なモデル")
100
 
101
  fetch_btn.click(
102
  fn=fetch_gemini_models,
 
104
  outputs=[output_table, output_status]
105
  )
106
 
107
+ gr.Markdown("## モデルアクセス確認")
108
+ with gr.Row():
109
+ model_name_input = gr.Textbox(
110
+ label="モデル名",
111
+ placeholder="gemini-1.5-pro-001",
112
+ interactive=True,
113
+ scale=4
114
+ )
115
+ check_btn = gr.Button("アクセス確認", variant="secondary", scale=1)
116
+
117
+ access_status = gr.Textbox(label="アクセス確認結果", interactive=False)
118
+
119
+ check_btn.click(
120
+ fn=check_model_access,
121
+ inputs=[api_key_input, model_name_input],
122
+ outputs=access_status
123
+ )
124
+
125
  gr.Markdown(
126
  """
127
  ---
128
  ### 使い方
129
  1. Google AI Studioから取得した **APIキー** を入力します。
130
+ 2. **モデル一覧取得** ボタンをクリックします。利用可能なモデルの一覧が表示されます。
131
+ 3. 特定のモデルアクセスを確認するには、**モデルアクセス確認** セクションで **モデル名** を入力し、**アクセス確認** ボタンをクリックします。
132
  """
133
  )
134