Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,10 @@
|
|
| 1 |
"""
|
| 2 |
-
Text-to-Game Generator -
|
| 3 |
-
- Code :
|
| 4 |
- Images: canvas-drawn graphics only (zero API, zero quota, zero cost)
|
| 5 |
|
| 6 |
No GPU needed. No compilation. No HF quota. No credit card.
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
Setup: go to ai.google.dev, sign in with Google, click Get API key.
|
| 10 |
-
Then add it as a Space secret named GOOGLE_API_KEY.
|
| 11 |
"""
|
| 12 |
|
| 13 |
import os
|
|
@@ -22,26 +19,26 @@ from openai import OpenAI
|
|
| 22 |
# Google AI Studio client (OpenAI-compatible endpoint)
|
| 23 |
# ---------------------------------------------------------------------------
|
| 24 |
|
| 25 |
-
CODE_MODEL
|
| 26 |
-
|
| 27 |
|
| 28 |
|
| 29 |
def _check_key():
|
| 30 |
-
if not
|
| 31 |
raise ValueError(
|
| 32 |
-
"
|
| 33 |
-
"1. Go to
|
| 34 |
-
"2.
|
| 35 |
-
"3.
|
| 36 |
-
"4. Add it as a Space secret: Settings > Variables and secrets > Name:
|
| 37 |
)
|
| 38 |
|
| 39 |
|
| 40 |
def get_client():
|
| 41 |
_check_key()
|
| 42 |
return OpenAI(
|
| 43 |
-
base_url="https://
|
| 44 |
-
api_key=
|
| 45 |
)
|
| 46 |
|
| 47 |
|
|
@@ -191,9 +188,9 @@ def generate_game_code(game_type: str, theme: str, temperature: float, max_new_t
|
|
| 191 |
traceback.print_exc()
|
| 192 |
err = str(exc)
|
| 193 |
if "401" in err or "api_key" in err.lower():
|
| 194 |
-
err = "Invalid
|
| 195 |
-
elif "429" in err or "rate" in err.lower()
|
| 196 |
-
err = "Rate limited - wait a few seconds and try again
|
| 197 |
else:
|
| 198 |
err = "Error: " + str(exc)
|
| 199 |
return "", err, _placeholder_html(err)
|
|
|
|
| 1 |
"""
|
| 2 |
+
Text-to-Game Generator - Groq Edition (Truly Free)
|
| 3 |
+
- Code : llama-3.1-8b-instant via Groq API (free, no credit card)
|
| 4 |
- Images: canvas-drawn graphics only (zero API, zero quota, zero cost)
|
| 5 |
|
| 6 |
No GPU needed. No compilation. No HF quota. No credit card.
|
| 7 |
+
Setup: add GROQ_API_KEY as a Space secret.
|
|
|
|
|
|
|
|
|
|
| 8 |
"""
|
| 9 |
|
| 10 |
import os
|
|
|
|
| 19 |
# Google AI Studio client (OpenAI-compatible endpoint)
|
| 20 |
# ---------------------------------------------------------------------------
|
| 21 |
|
| 22 |
+
CODE_MODEL = "llama-3.1-8b-instant"
|
| 23 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "")
|
| 24 |
|
| 25 |
|
| 26 |
def _check_key():
|
| 27 |
+
if not GROQ_API_KEY:
|
| 28 |
raise ValueError(
|
| 29 |
+
"GROQ_API_KEY is not set.\n"
|
| 30 |
+
"1. Go to console.groq.com and sign in\n"
|
| 31 |
+
"2. Click API Keys > Create API Key\n"
|
| 32 |
+
"3. Copy the key (starts with gsk_)\n"
|
| 33 |
+
"4. Add it as a Space secret: Settings > Variables and secrets > Name: GROQ_API_KEY"
|
| 34 |
)
|
| 35 |
|
| 36 |
|
| 37 |
def get_client():
|
| 38 |
_check_key()
|
| 39 |
return OpenAI(
|
| 40 |
+
base_url="https://api.groq.com/openai/v1",
|
| 41 |
+
api_key=GROQ_API_KEY,
|
| 42 |
)
|
| 43 |
|
| 44 |
|
|
|
|
| 188 |
traceback.print_exc()
|
| 189 |
err = str(exc)
|
| 190 |
if "401" in err or "api_key" in err.lower():
|
| 191 |
+
err = "Invalid GROQ_API_KEY. Check your key at console.groq.com."
|
| 192 |
+
elif "429" in err or "rate" in err.lower():
|
| 193 |
+
err = "Rate limited by Groq - wait a few seconds and try again."
|
| 194 |
else:
|
| 195 |
err = "Error: " + str(exc)
|
| 196 |
return "", err, _placeholder_html(err)
|