Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ from fastapi.templating import Jinja2Templates
|
|
| 10 |
app = FastAPI()
|
| 11 |
templates = Jinja2Templates(directory="templates")
|
| 12 |
|
| 13 |
-
#
|
| 14 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "AIzaSyDcYyq3w21iwipYn17wCAQo3AYWhUIGDSI")
|
| 15 |
GEMINI_API_URL = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={GEMINI_API_KEY}"
|
| 16 |
|
|
@@ -23,44 +23,46 @@ def call_gemini_api(prompt: str) -> str:
|
|
| 23 |
"parts": [{"text": prompt}]
|
| 24 |
}]
|
| 25 |
}
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
return
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
@app.get("/", response_class=HTMLResponse)
|
| 34 |
-
def
|
| 35 |
return templates.TemplateResponse("index.html", {"request": request})
|
| 36 |
|
| 37 |
@app.post("/fortune", response_class=HTMLResponse)
|
| 38 |
-
def
|
| 39 |
try:
|
| 40 |
datetime.datetime.strptime(birthday, "%Y-%m-%d").date()
|
| 41 |
except Exception:
|
| 42 |
return templates.TemplateResponse(
|
| 43 |
"index.html",
|
| 44 |
-
{"request": request, "error": "Invalid
|
| 45 |
)
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
grade = grade_match.group(0) if grade_match else "1"
|
| 49 |
-
|
| 50 |
-
# Construct a prompt that emphasizes coding and robotics
|
| 51 |
prompt = (
|
| 52 |
-
f"You are an inspirational fortune teller for young coders
|
| 53 |
-
f"
|
| 54 |
-
f"
|
| 55 |
-
f"emphasizing creativity, learning, and fun in technology."
|
| 56 |
)
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
except Exception as e:
|
| 61 |
-
fortune_message = "Coding and robotics hold a bright future for you! Keep dreaming and exploring."
|
| 62 |
-
|
| 63 |
return templates.TemplateResponse(
|
| 64 |
"index.html",
|
| 65 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
)
|
|
|
|
| 10 |
app = FastAPI()
|
| 11 |
templates = Jinja2Templates(directory="templates")
|
| 12 |
|
| 13 |
+
# Get your Gemini API key from environment variables
|
| 14 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "AIzaSyDcYyq3w21iwipYn17wCAQo3AYWhUIGDSI")
|
| 15 |
GEMINI_API_URL = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={GEMINI_API_KEY}"
|
| 16 |
|
|
|
|
| 23 |
"parts": [{"text": prompt}]
|
| 24 |
}]
|
| 25 |
}
|
| 26 |
+
try:
|
| 27 |
+
response = requests.post(GEMINI_API_URL, headers=headers, data=json.dumps(data))
|
| 28 |
+
response.raise_for_status()
|
| 29 |
+
response_data = response.json()
|
| 30 |
+
# Expecting the API to return candidates with an "output" field.
|
| 31 |
+
if "candidates" in response_data and len(response_data["candidates"]) > 0:
|
| 32 |
+
return response_data["candidates"][0].get("output", "Keep coding and stay inspired!")
|
| 33 |
+
return "Keep coding and stay inspired!"
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return "Keep coding and stay inspired!"
|
| 36 |
|
| 37 |
@app.get("/", response_class=HTMLResponse)
|
| 38 |
+
def index(request: Request):
|
| 39 |
return templates.TemplateResponse("index.html", {"request": request})
|
| 40 |
|
| 41 |
@app.post("/fortune", response_class=HTMLResponse)
|
| 42 |
+
def fortune(request: Request, birthday: str = Form(...), standard: str = Form(...)):
|
| 43 |
try:
|
| 44 |
datetime.datetime.strptime(birthday, "%Y-%m-%d").date()
|
| 45 |
except Exception:
|
| 46 |
return templates.TemplateResponse(
|
| 47 |
"index.html",
|
| 48 |
+
{"request": request, "error": "Invalid birthday format! Please use YYYY-MM-DD."}
|
| 49 |
)
|
| 50 |
+
|
| 51 |
+
# Build a prompt that uses the provided birthday and standard.
|
|
|
|
|
|
|
|
|
|
| 52 |
prompt = (
|
| 53 |
+
f"You are an inspirational fortune teller for young coders. "
|
| 54 |
+
f"Here's the birthday: {birthday}. For a student in standard {standard}, "
|
| 55 |
+
f"provide a random, modern, concise, and energetic quote that inspires them to code and explore robotics."
|
|
|
|
| 56 |
)
|
| 57 |
+
|
| 58 |
+
fortune_message = call_gemini_api(prompt)
|
| 59 |
+
|
|
|
|
|
|
|
|
|
|
| 60 |
return templates.TemplateResponse(
|
| 61 |
"index.html",
|
| 62 |
+
{
|
| 63 |
+
"request": request,
|
| 64 |
+
"fortune": fortune_message,
|
| 65 |
+
"birthday": birthday,
|
| 66 |
+
"standard": standard
|
| 67 |
+
}
|
| 68 |
)
|