Spaces:
Sleeping
Sleeping
faizr206
commited on
Commit
·
8b8fced
1
Parent(s):
ffc29c7
email data
Browse files- app/main.py +30 -1
app/main.py
CHANGED
|
@@ -5,7 +5,7 @@ from typing import Optional, Tuple
|
|
| 5 |
|
| 6 |
from fastapi import FastAPI, HTTPException, Response
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
-
from pydantic import BaseModel
|
| 9 |
|
| 10 |
# Optional .env for local testing
|
| 11 |
from dotenv import load_dotenv
|
|
@@ -34,6 +34,8 @@ app.add_middleware(
|
|
| 34 |
|
| 35 |
|
| 36 |
RUNS = Path("runs"); RUNS.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# ---------------- simple 10 RPM rate limiter ----------------
|
| 39 |
class RateLimiter:
|
|
@@ -383,6 +385,22 @@ def refine_loop(user_prompt: str, max_error_refines: int = 3, do_visual_refine:
|
|
| 383 |
class PromptIn(BaseModel):
|
| 384 |
prompt: str
|
| 385 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
@app.get("/")
|
| 387 |
def health():
|
| 388 |
return {"ok": True, "model": MODEL, "has_gemini": bool(API_KEY)}
|
|
@@ -408,3 +426,14 @@ def generate_and_render(inp: PromptIn):
|
|
| 408 |
media_type="video/mp4",
|
| 409 |
headers={"Content-Disposition": 'inline; filename="result.mp4"'}
|
| 410 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
from fastapi import FastAPI, HTTPException, Response
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
+
from pydantic import BaseModel, validator
|
| 9 |
|
| 10 |
# Optional .env for local testing
|
| 11 |
from dotenv import load_dotenv
|
|
|
|
| 34 |
|
| 35 |
|
| 36 |
RUNS = Path("runs"); RUNS.mkdir(parents=True, exist_ok=True)
|
| 37 |
+
DATA_DIR = Path("data"); DATA_DIR.mkdir(parents=True, exist_ok=True)
|
| 38 |
+
EMAILS_FILE = DATA_DIR / "emails.txt"
|
| 39 |
|
| 40 |
# ---------------- simple 10 RPM rate limiter ----------------
|
| 41 |
class RateLimiter:
|
|
|
|
| 385 |
class PromptIn(BaseModel):
|
| 386 |
prompt: str
|
| 387 |
|
| 388 |
+
class EmailIn(BaseModel):
|
| 389 |
+
email: str
|
| 390 |
+
|
| 391 |
+
@property
|
| 392 |
+
def sanitized(self) -> str:
|
| 393 |
+
return self.email
|
| 394 |
+
|
| 395 |
+
@validator("email")
|
| 396 |
+
def validate_email(cls, value: str) -> str:
|
| 397 |
+
cleaned = value.strip()
|
| 398 |
+
if not cleaned:
|
| 399 |
+
raise ValueError("Email cannot be empty")
|
| 400 |
+
if not re.match(r"^[^@\s]+@[^@\s]+\.[^@\s]+$", cleaned):
|
| 401 |
+
raise ValueError("Email is not valid")
|
| 402 |
+
return cleaned
|
| 403 |
+
|
| 404 |
@app.get("/")
|
| 405 |
def health():
|
| 406 |
return {"ok": True, "model": MODEL, "has_gemini": bool(API_KEY)}
|
|
|
|
| 426 |
media_type="video/mp4",
|
| 427 |
headers={"Content-Disposition": 'inline; filename="result.mp4"'}
|
| 428 |
)
|
| 429 |
+
|
| 430 |
+
@app.post("/store-email")
|
| 431 |
+
def store_email(email: EmailIn):
|
| 432 |
+
"""Append the provided email address to a server-side text file."""
|
| 433 |
+
sanitized_email = email.sanitized
|
| 434 |
+
try:
|
| 435 |
+
with EMAILS_FILE.open("a", encoding="utf-8") as fh:
|
| 436 |
+
fh.write(f"{sanitized_email}\n")
|
| 437 |
+
except Exception:
|
| 438 |
+
raise HTTPException(500, "Failed to save email address")
|
| 439 |
+
return {"stored": True}
|