Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -40,10 +40,13 @@ Question:
|
|
| 40 |
Answer (with explanation):
|
| 41 |
"""
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
try:
|
| 46 |
-
# ✅ Works for huggingface-hub >=0.33
|
| 47 |
response = client.text_generation(
|
| 48 |
prompt,
|
| 49 |
model="google/gemma-2b-it",
|
|
@@ -51,14 +54,12 @@ Answer (with explanation):
|
|
| 51 |
temperature=0.7,
|
| 52 |
)
|
| 53 |
except TypeError:
|
| 54 |
-
# ✅ Fallback for older versions that use 'inputs' & 'parameters'
|
| 55 |
response = client.text_generation(
|
| 56 |
model="google/gemma-2b-it",
|
| 57 |
inputs=prompt,
|
| 58 |
parameters={"max_new_tokens": 1024, "temperature": 0.7},
|
| 59 |
)
|
| 60 |
|
| 61 |
-
# Extract text safely
|
| 62 |
if isinstance(response, str):
|
| 63 |
answer = response
|
| 64 |
elif isinstance(response, dict) and "generated_text" in response:
|
|
@@ -68,4 +69,4 @@ Answer (with explanation):
|
|
| 68 |
else:
|
| 69 |
answer = str(response)
|
| 70 |
|
| 71 |
-
return answer
|
|
|
|
| 40 |
Answer (with explanation):
|
| 41 |
"""
|
| 42 |
|
| 43 |
+
# ✅ Explicitly specify provider (avoids StopIteration)
|
| 44 |
+
client = InferenceClient(
|
| 45 |
+
provider="hf-inference",
|
| 46 |
+
token=os.environ.get("HUGGINGFACE_API_KEY")
|
| 47 |
+
)
|
| 48 |
|
| 49 |
try:
|
|
|
|
| 50 |
response = client.text_generation(
|
| 51 |
prompt,
|
| 52 |
model="google/gemma-2b-it",
|
|
|
|
| 54 |
temperature=0.7,
|
| 55 |
)
|
| 56 |
except TypeError:
|
|
|
|
| 57 |
response = client.text_generation(
|
| 58 |
model="google/gemma-2b-it",
|
| 59 |
inputs=prompt,
|
| 60 |
parameters={"max_new_tokens": 1024, "temperature": 0.7},
|
| 61 |
)
|
| 62 |
|
|
|
|
| 63 |
if isinstance(response, str):
|
| 64 |
answer = response
|
| 65 |
elif isinstance(response, dict) and "generated_text" in response:
|
|
|
|
| 69 |
else:
|
| 70 |
answer = str(response)
|
| 71 |
|
| 72 |
+
return answer
|