Skip to content

Commit 94a7536

Browse files
committed
fix: call Gemini REST API directly — drop google-generativeai (no grpcio compile)
1 parent 76f50eb commit 94a7536

2 files changed

Lines changed: 24 additions & 26 deletions

File tree

apps/hdp-physical-hf/app.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,28 @@
4444
sign_edt,
4545
)
4646

47-
# ── Gemma 4 via Google AI Studio ─────────────────────────────────────────────
48-
# Gemma 4 (E4B) is a multimodal any-to-any model — not on HF serverless.
49-
# The only public inference path is Google AI Studio (free API key at
50-
# aistudio.google.com/apikey). Add key as GOOGLE_API_KEY Space secret.
47+
# ── Gemma 4 via Google AI Studio (pure REST — no extra deps) ─────────────────
48+
# Uses the Gemini REST API directly with `requests` (always available).
49+
# No google-generativeai package needed — avoids grpcio compile time.
5150
#
52-
# Fallback chain (checked in order):
53-
# 1. Google AI Studio — GOOGLE_API_KEY + GEMMA_MODEL (default: gemma-4-e4b-it)
54-
# 2. HF InferenceClient — HF_TOKEN + featherless-ai + gemma-3-12b-it
51+
# Fallback chain:
52+
# 1. Google AI Studio REST — GOOGLE_API_KEY
53+
# 2. HF featherless-ai — HF_TOKEN + gemma-3-12b-it
54+
import requests as _requests
55+
5556
_GEMMA_MODEL = os.environ.get("GEMMA_MODEL", "gemma-4-31b-it")
5657
_GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
5758
_HF_TOKEN = os.environ.get("HF_TOKEN")
5859
_GEMMA_AVAILABLE = False
5960
_GEMMA_BACKEND = None # "google" | "hf"
6061
_GEMMA_INIT_ERR = ""
61-
_gemma_google = None
6262
_gemma_hf = None
6363

64-
try:
65-
import google.generativeai as genai
66-
if _GOOGLE_API_KEY:
67-
genai.configure(api_key=_GOOGLE_API_KEY)
68-
_gemma_google = genai.GenerativeModel(_GEMMA_MODEL)
69-
_GEMMA_AVAILABLE = True
70-
_GEMMA_BACKEND = "google"
71-
else:
72-
_GEMMA_INIT_ERR = "GOOGLE_API_KEY not set"
73-
except Exception as _e:
74-
_GEMMA_INIT_ERR = f"google-generativeai error: {_e}"
75-
76-
if not _GEMMA_AVAILABLE:
64+
if _GOOGLE_API_KEY:
65+
_GEMMA_AVAILABLE = True
66+
_GEMMA_BACKEND = "google"
67+
else:
68+
_GEMMA_INIT_ERR = "GOOGLE_API_KEY not set"
7769
try:
7870
from huggingface_hub import InferenceClient
7971
if _HF_TOKEN:
@@ -205,11 +197,18 @@ def _call_gemma(prompt: str, single: bool = False) -> str:
205197
return ""
206198
try:
207199
if _GEMMA_BACKEND == "google":
208-
resp = _gemma_google.generate_content(
209-
prompt,
210-
generation_config={"max_output_tokens": 300 if not single else 120},
200+
url = (
201+
f"https://generativelanguage.googleapis.com/v1beta/models/"
202+
f"{_GEMMA_MODEL}:generateContent?key={_GOOGLE_API_KEY}"
211203
)
212-
return resp.text.strip()
204+
body = {
205+
"contents": [{"parts": [{"text": prompt}]}],
206+
"generationConfig": {"maxOutputTokens": 300 if not single else 120},
207+
}
208+
r = _requests.post(url, json=body, timeout=60)
209+
r.raise_for_status()
210+
data = r.json()
211+
return data["candidates"][0]["content"]["parts"][0]["text"].strip()
213212
else:
214213
result = _gemma_hf.chat.completions.create(
215214
model="google/gemma-3-12b-it",
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
hdp-physical>=0.1.0
22
cryptography>=42.0.0
33
huggingface_hub>=0.20.0
4-
google-generativeai>=0.8.0

0 commit comments

Comments
 (0)