Spaces:
Sleeping
Sleeping
File size: 2,344 Bytes
51769a3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | """
POST /ai/generate
Generate H-Script code from a plain English/Hinglish description.
"""
from fastapi import APIRouter
from pydantic import BaseModel
from core.gemini_client import ask_gemini
router = APIRouter()
class GenerateRequest(BaseModel):
prompt: str # e.g. "write a function that adds two numbers"
context: str | None = None # optional — existing code for context
class GenerateResponse(BaseModel):
code: str
explanation: str
@router.post("/generate", response_model=GenerateResponse)
async def generate_code(body: GenerateRequest):
context_section = ""
if body.context:
context_section = f"""
Existing code context (write code that fits with this):
```
{body.context}
```
"""
prompt = f"""
The user wants you to write H-Script code based on this description:
"{body.prompt}"
{context_section}
Rules:
1. ONLY use valid H-Script syntax (let_him_cook, boliye, pov, agar, warna, etc.)
2. Write clean, readable code with comments
3. After the code block, give a SHORT 2-3 line explanation of what you wrote
Format your response EXACTLY like this:
```hscript
[your code here]
```
EXPLANATION: [short explanation here]
"""
raw = await ask_gemini(prompt)
# Guard: empty = rate limit hit
if not raw or not raw.strip():
return GenerateResponse(
code="// AI is busy, try again in a moment bhai 🙏",
explanation="Rate limit hit — wait a few seconds and try again."
)
# Parse code and explanation from response
code = ""
explanation = ""
if "```" in raw:
parts = raw.split("```")
for i, part in enumerate(parts):
if i % 2 == 1: # inside code block
lines = part.strip().splitlines()
if lines and not lines[0].strip().startswith("let_him_cook") \
and not lines[0].strip().startswith("//") \
and len(lines[0].strip()) < 20:
lines = lines[1:]
code = "\n".join(lines).strip()
break
if "EXPLANATION:" in raw:
explanation = raw.split("EXPLANATION:")[-1].strip()
if not code:
# Fallback — return whole response as code
code = raw.strip()
explanation = ""
return GenerateResponse(code=code, explanation=explanation)
|