nhathuy07 commited on
Commit
c949efc
·
verified ·
1 Parent(s): 1b68a38

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +31 -3
main.py CHANGED
@@ -70,7 +70,7 @@ class APIGuardMiddleware(BaseHTTPMiddleware):
70
  # Get current client url and client IP address
71
  client_url = request.url.path
72
  client_ip_addr = request.client.host
73
-
74
  # IP-based rate limitation
75
  async with r.pipeline(transaction=True) as pipeline:
76
  try:
@@ -80,6 +80,11 @@ class APIGuardMiddleware(BaseHTTPMiddleware):
80
  res = None
81
 
82
  if res == None:
 
 
 
 
 
83
  ok = await pipeline.set(client_ip_addr, 25).execute()
84
  await r.expire(client_ip_addr, 60)
85
  elif res > 0:
@@ -600,6 +605,28 @@ async def convert2html(request):
600
 
601
  return JSONResponse(rets)
602
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
603
 
604
  async def get_flashcards(request):
605
  # [title, content, keywords]
@@ -612,7 +639,7 @@ async def get_flashcards(request):
612
 
613
  __tldr = await tldr(__content, __lang)
614
  print(__tldr)
615
- __definitions = await get_definitions_from_words(__keywords, __tldr)
616
  print(__definitions)
617
 
618
  return JSONResponse({"tldr": __tldr, "defs": __definitions, "imgs": await fetch_img_for_words(__keywords)})
@@ -685,7 +712,8 @@ app = Starlette(debug=True,routes=[
685
  Route('/convert2md', convert2md, methods=['POST']),
686
  Route('/mltest', __mltest, methods=['GET']),
687
  Route('/validateSimilarity', validate_similarity, methods=['POST']),
688
- Route('/', root, methods=['GET'])
 
689
  ],
690
  middleware=middleware)
691
 
 
70
  # Get current client url and client IP address
71
  client_url = request.url.path
72
  client_ip_addr = request.client.host
73
+
74
  # IP-based rate limitation
75
  async with r.pipeline(transaction=True) as pipeline:
76
  try:
 
80
  res = None
81
 
82
  if res == None:
83
+ # lim = 25 if client_url.endswith('text2quiz-three.vercel.app') else 5
84
+ # lim = 10 if client_url.endswith('localhost:8100') else 5
85
+
86
+ app_logger.info(client_url)
87
+
88
  ok = await pipeline.set(client_ip_addr, 25).execute()
89
  await r.expire(client_ip_addr, 60)
90
  elif res > 0:
 
605
 
606
  return JSONResponse(rets)
607
 
608
+ async def llm_generate_text(request):
609
+ async with httpx.AsyncClient() as client:
610
+ o = await request.json()
611
+ _r = await client.post(
612
+ url="https://api.awanllm.com/v1/chat/completions",
613
+ headers={'Content-Type': 'application/json', 'Authorization': f'Bearer {LLM_API_KEY}'},
614
+ data=json.dumps({
615
+ "model": "Meta-Llama-3-8B-Instruct",
616
+ "messages": [
617
+ {"role": "user", "content": f"Generate a study note about {o['prompt']}. Use {o['lang']}."}
618
+ ],
619
+ "max_tokens": 4096,
620
+ "presence_penalty":0.3,
621
+ "temperature":0.5
622
+ }),
623
+ timeout=None)
624
+
625
+ try:
626
+ repl = _r.json()['choices'][0]['message']['content']
627
+ return PlainTextResponse(repl)
628
+ else:
629
+ raise HTTPException(status_code=422)
630
 
631
  async def get_flashcards(request):
632
  # [title, content, keywords]
 
639
 
640
  __tldr = await tldr(__content, __lang)
641
  print(__tldr)
642
+ __definitions = await get_definitions_from_words(__keywords, __tldr, __lang)
643
  print(__definitions)
644
 
645
  return JSONResponse({"tldr": __tldr, "defs": __definitions, "imgs": await fetch_img_for_words(__keywords)})
 
712
  Route('/convert2md', convert2md, methods=['POST']),
713
  Route('/mltest', __mltest, methods=['GET']),
714
  Route('/validateSimilarity', validate_similarity, methods=['POST']),
715
+ Route('/llmGenerateText', llm_generate_text, methods=['POST']),
716
+ Route('/', root, methods=['GET']),
717
  ],
718
  middleware=middleware)
719