triflix commited on
Commit
ff19c7d
·
verified ·
1 Parent(s): 15e5ca3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -26
app.py CHANGED
@@ -10,7 +10,7 @@ from fastapi.templating import Jinja2Templates
10
  app = FastAPI()
11
  templates = Jinja2Templates(directory="templates")
12
 
13
- # Retrieve 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,44 +23,46 @@ def call_gemini_api(prompt: str) -> str:
23
  "parts": [{"text": prompt}]
24
  }]
25
  }
26
- response = requests.post(GEMINI_API_URL, headers=headers, data=json.dumps(data))
27
- response_data = response.json()
28
- # Assuming the generated text is in the first candidate's "output" field:
29
- if "candidates" in response_data and len(response_data["candidates"]) > 0:
30
- return response_data["candidates"][0].get("output", "Keep coding and exploring robotics!")
31
- return "Keep coding and exploring robotics!"
 
 
 
 
32
 
33
  @app.get("/", response_class=HTMLResponse)
34
- def read_index(request: Request):
35
  return templates.TemplateResponse("index.html", {"request": request})
36
 
37
  @app.post("/fortune", response_class=HTMLResponse)
38
- def get_fortune(request: Request, birthday: str = Form(...), standard: str = Form(...)):
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 date format! Please use YYYY-MM-DD."}
45
  )
46
-
47
- grade_match = re.search(r'\d+', standard)
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 and robotics enthusiasts. "
53
- f"A student with birthday {birthday} and in grade {standard} is curious about technology. "
54
- f"Provide an uplifting fortune message that encourages them to explore coding and robotics, "
55
- f"emphasizing creativity, learning, and fun in technology."
56
  )
57
-
58
- try:
59
- fortune_message = call_gemini_api(prompt)
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
- {"request": request, "fortune": fortune_message, "birthday": birthday, "standard": standard}
 
 
 
 
 
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
  )