Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -44,10 +44,11 @@ def init_db():
|
|
| 44 |
import traceback
|
| 45 |
traceback.print_exc()
|
| 46 |
|
| 47 |
-
def check_and_update_usage(username: str, use_pro_model: bool) -> bool:
|
| 48 |
"""
|
| 49 |
Check if user has reached daily limit and update usage.
|
| 50 |
Returns True if user can generate, False if limit reached.
|
|
|
|
| 51 |
"""
|
| 52 |
# Exempted users bypass all checks
|
| 53 |
if username in EXEMPTED_USERS:
|
|
@@ -72,12 +73,12 @@ def check_and_update_usage(username: str, use_pro_model: bool) -> bool:
|
|
| 72 |
# New user - create record
|
| 73 |
if use_pro_model:
|
| 74 |
cursor.execute("INSERT INTO usage (username, date, count_standard, count_pro) VALUES (?, ?, ?, ?)",
|
| 75 |
-
(username, today, 0,
|
| 76 |
else:
|
| 77 |
cursor.execute("INSERT INTO usage (username, date, count_standard, count_pro) VALUES (?, ?, ?, ?)",
|
| 78 |
-
(username, today,
|
| 79 |
conn.commit()
|
| 80 |
-
print(f"New user {username}:
|
| 81 |
return True
|
| 82 |
|
| 83 |
user_date, user_count_standard, user_count_pro = result
|
|
@@ -87,25 +88,25 @@ def check_and_update_usage(username: str, use_pro_model: bool) -> bool:
|
|
| 87 |
if user_date != today:
|
| 88 |
if use_pro_model:
|
| 89 |
cursor.execute("UPDATE usage SET date = ?, count_standard = ?, count_pro = ? WHERE username = ?",
|
| 90 |
-
(today, 0,
|
| 91 |
else:
|
| 92 |
cursor.execute("UPDATE usage SET date = ?, count_standard = ?, count_pro = ? WHERE username = ?",
|
| 93 |
-
(today,
|
| 94 |
conn.commit()
|
| 95 |
-
print(f"User {username} reset for new day:
|
| 96 |
return True
|
| 97 |
|
| 98 |
-
# Check
|
| 99 |
-
if user_count
|
| 100 |
-
print(f"User {username}
|
| 101 |
return False
|
| 102 |
|
| 103 |
-
# Increment count
|
| 104 |
-
new_count = user_count +
|
| 105 |
cursor.execute(f"UPDATE usage SET {count_column} = ? WHERE username = ?",
|
| 106 |
(new_count, username))
|
| 107 |
conn.commit()
|
| 108 |
-
print(f"User {username} usage: {new_count}/{limit} ({model_name})")
|
| 109 |
return True
|
| 110 |
|
| 111 |
except Exception as e:
|
|
@@ -201,6 +202,24 @@ def get_username(token: Optional[Union[gr.OAuthToken, str]]) -> Optional[str]:
|
|
| 201 |
print(f"Could not get username: {e}")
|
| 202 |
return None
|
| 203 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
def _extract_image_data_from_response(response) -> Optional[bytes]:
|
| 205 |
"""Helper to extract image data from the model's response."""
|
| 206 |
if hasattr(response, 'candidates') and response.candidates:
|
|
@@ -413,6 +432,15 @@ with gr.Blocks(theme=gr.themes.Citrus(), css=css) as demo:
|
|
| 413 |
info="PRO: Higher quality, slower generation (50/day) | Standard: Faster generation (75/day)"
|
| 414 |
)
|
| 415 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
aspect_ratio_dropdown = gr.Dropdown(
|
| 417 |
label="Aspect Ratio",
|
| 418 |
choices=["Auto", "1:1", "9:16", "16:9", "3:4", "4:3", "3:2", "2:3", "5:4", "4:5", "21:9"],
|
|
@@ -485,4 +513,4 @@ with gr.Blocks(theme=gr.themes.Citrus(), css=css) as demo:
|
|
| 485 |
demo.load(control_access, inputs=None, outputs=[main_interface, pro_message])
|
| 486 |
|
| 487 |
if __name__ == "__main__":
|
| 488 |
-
demo.queue(max_size=None, default_concurrency_limit=None).launch(show_error=True)
|
|
|
|
| 44 |
import traceback
|
| 45 |
traceback.print_exc()
|
| 46 |
|
| 47 |
+
def check_and_update_usage(username: str, use_pro_model: bool, credits_to_use: int = 1) -> bool:
|
| 48 |
"""
|
| 49 |
Check if user has reached daily limit and update usage.
|
| 50 |
Returns True if user can generate, False if limit reached.
|
| 51 |
+
credits_to_use: Number of credits to consume (1 for standard/1K, 2 for 2K, 4 for 4K)
|
| 52 |
"""
|
| 53 |
# Exempted users bypass all checks
|
| 54 |
if username in EXEMPTED_USERS:
|
|
|
|
| 73 |
# New user - create record
|
| 74 |
if use_pro_model:
|
| 75 |
cursor.execute("INSERT INTO usage (username, date, count_standard, count_pro) VALUES (?, ?, ?, ?)",
|
| 76 |
+
(username, today, 0, credits_to_use))
|
| 77 |
else:
|
| 78 |
cursor.execute("INSERT INTO usage (username, date, count_standard, count_pro) VALUES (?, ?, ?, ?)",
|
| 79 |
+
(username, today, credits_to_use, 0))
|
| 80 |
conn.commit()
|
| 81 |
+
print(f"New user {username}: {credits_to_use}/{limit} ({model_name})")
|
| 82 |
return True
|
| 83 |
|
| 84 |
user_date, user_count_standard, user_count_pro = result
|
|
|
|
| 88 |
if user_date != today:
|
| 89 |
if use_pro_model:
|
| 90 |
cursor.execute("UPDATE usage SET date = ?, count_standard = ?, count_pro = ? WHERE username = ?",
|
| 91 |
+
(today, 0, credits_to_use, username))
|
| 92 |
else:
|
| 93 |
cursor.execute("UPDATE usage SET date = ?, count_standard = ?, count_pro = ? WHERE username = ?",
|
| 94 |
+
(today, credits_to_use, 0, username))
|
| 95 |
conn.commit()
|
| 96 |
+
print(f"User {username} reset for new day: {credits_to_use}/{limit} ({model_name})")
|
| 97 |
return True
|
| 98 |
|
| 99 |
+
# Check if user has enough credits remaining
|
| 100 |
+
if user_count + credits_to_use > limit:
|
| 101 |
+
print(f"User {username} insufficient credits: needs {credits_to_use}, has {limit - user_count}/{limit} remaining ({model_name})")
|
| 102 |
return False
|
| 103 |
|
| 104 |
+
# Increment count by credits used
|
| 105 |
+
new_count = user_count + credits_to_use
|
| 106 |
cursor.execute(f"UPDATE usage SET {count_column} = ? WHERE username = ?",
|
| 107 |
(new_count, username))
|
| 108 |
conn.commit()
|
| 109 |
+
print(f"User {username} usage: {new_count}/{limit} (used {credits_to_use} credits) ({model_name})")
|
| 110 |
return True
|
| 111 |
|
| 112 |
except Exception as e:
|
|
|
|
| 202 |
print(f"Could not get username: {e}")
|
| 203 |
return None
|
| 204 |
|
| 205 |
+
def get_credit_cost(resolution: str) -> int:
|
| 206 |
+
"""Get the credit cost for a given resolution."""
|
| 207 |
+
if "4K" in resolution:
|
| 208 |
+
return 4
|
| 209 |
+
elif "2K" in resolution:
|
| 210 |
+
return 2
|
| 211 |
+
else: # 1K
|
| 212 |
+
return 1
|
| 213 |
+
|
| 214 |
+
def get_resolution_value(resolution: str) -> str:
|
| 215 |
+
"""Extract the resolution value from the dropdown selection."""
|
| 216 |
+
if "4K" in resolution:
|
| 217 |
+
return "4K"
|
| 218 |
+
elif "2K" in resolution:
|
| 219 |
+
return "2K"
|
| 220 |
+
else:
|
| 221 |
+
return "1K"
|
| 222 |
+
|
| 223 |
def _extract_image_data_from_response(response) -> Optional[bytes]:
|
| 224 |
"""Helper to extract image data from the model's response."""
|
| 225 |
if hasattr(response, 'candidates') and response.candidates:
|
|
|
|
| 432 |
info="PRO: Higher quality, slower generation (50/day) | Standard: Faster generation (75/day)"
|
| 433 |
)
|
| 434 |
|
| 435 |
+
# Resolution selection for PRO model
|
| 436 |
+
resolution_dropdown = gr.Dropdown(
|
| 437 |
+
label="Resolution (Nano Banana PRO only)",
|
| 438 |
+
choices=["1K (1 credit)", "2K (2 credits)", "4K (4 credits)"],
|
| 439 |
+
value="1K (1 credit)",
|
| 440 |
+
interactive=True,
|
| 441 |
+
visible=False
|
| 442 |
+
)
|
| 443 |
+
|
| 444 |
aspect_ratio_dropdown = gr.Dropdown(
|
| 445 |
label="Aspect Ratio",
|
| 446 |
choices=["Auto", "1:1", "9:16", "16:9", "3:4", "4:3", "3:2", "2:3", "5:4", "4:5", "21:9"],
|
|
|
|
| 513 |
demo.load(control_access, inputs=None, outputs=[main_interface, pro_message])
|
| 514 |
|
| 515 |
if __name__ == "__main__":
|
| 516 |
+
demo.queue(max_size=None, default_concurrency_limit=None).launch(show_error=True)
|