asFrants commited on
Commit
cdd5b2f
1 Parent(s): 8311f5b

add base security to api

Browse files
Files changed (2) hide show
  1. app.py +2 -2
  2. main.py +36 -3
app.py CHANGED
@@ -123,9 +123,9 @@ class Summarizer:
123
 
124
  def summarize(self, req: TextRequest, lang: str = "en") -> Result:
125
  sum_pipe, sent_pipe = self.get_pipe(lang)
126
- response_summary = sum_pipe(req.text)
127
  logger.info(response_summary)
128
- response_sentiment = sent_pipe(req.text)
129
  logger.info(response_sentiment)
130
  result = Result(
131
  summary=response_summary[0]["summary_text"],
 
123
 
124
  def summarize(self, req: TextRequest, lang: str = "en") -> Result:
125
  sum_pipe, sent_pipe = self.get_pipe(lang)
126
+ response_summary = sum_pipe(req)
127
  logger.info(response_summary)
128
+ response_sentiment = sent_pipe(req)
129
  logger.info(response_sentiment)
130
  result = Result(
131
  summary=response_summary[0]["summary_text"],
main.py CHANGED
@@ -1,11 +1,14 @@
1
  import os
2
  import gradio as gr
3
  import random
4
- from fastapi import FastAPI, Request
 
 
5
  from fastapi.staticfiles import StaticFiles
6
  from fastapi.responses import HTMLResponse, RedirectResponse
7
  from fastapi.templating import Jinja2Templates
8
  from fastapi.exceptions import HTTPException
 
9
  from loguru import logger
10
  from dotenv import load_dotenv
11
 
@@ -22,9 +25,13 @@ from models.forms import VerificationForm
22
  load_dotenv()
23
 
24
  SITE_KEY = os.getenv("SITE_KEY")
 
 
 
25
 
26
  app = FastAPI()
27
  pipe = Summarizer()
 
28
 
29
  # mount FastAPI StaticFiles server
30
  app.mount("/static", StaticFiles(directory="static"), name="static")
@@ -56,15 +63,41 @@ async def verify(request: Request):
56
  return await verify_page(request)
57
 
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  @app.post("/summ_ru", response_model=Result)
60
- async def ru_summ_api(request: TextRequest):
 
 
61
  results = pipe.summarize(request.text, lang="ru")
62
  logger.info(results)
63
  return results
64
 
65
 
66
  @app.post("/summ_en", response_model=Result)
67
- async def en_summ_api(request: TextRequest):
 
 
68
  results = pipe.summarize(request.text, lang="en")
69
  logger.info(results)
70
  return results
 
1
  import os
2
  import gradio as gr
3
  import random
4
+ import secrets
5
+ from typing import Annotated
6
+ from fastapi import FastAPI, Request, status, Depends
7
  from fastapi.staticfiles import StaticFiles
8
  from fastapi.responses import HTMLResponse, RedirectResponse
9
  from fastapi.templating import Jinja2Templates
10
  from fastapi.exceptions import HTTPException
11
+ from fastapi.security import HTTPBasic, HTTPBasicCredentials
12
  from loguru import logger
13
  from dotenv import load_dotenv
14
 
 
25
  load_dotenv()
26
 
27
  SITE_KEY = os.getenv("SITE_KEY")
28
+ API_USER = os.getenv("API_USER")
29
+ API_PWD = os.getenv("API_PWD")
30
+ users = set()
31
 
32
  app = FastAPI()
33
  pipe = Summarizer()
34
+ security = HTTPBasic()
35
 
36
  # mount FastAPI StaticFiles server
37
  app.mount("/static", StaticFiles(directory="static"), name="static")
 
63
  return await verify_page(request)
64
 
65
 
66
+ def get_current_username(
67
+ credentials: Annotated[HTTPBasicCredentials, Depends(security)]
68
+ ):
69
+ current_username_bytes = credentials.username.encode("utf8")
70
+ correct_username_bytes = bytes(API_USER, "utf-8")
71
+ is_correct_username = secrets.compare_digest(
72
+ current_username_bytes, correct_username_bytes
73
+ )
74
+ current_password_bytes = credentials.password.encode("utf8")
75
+ correct_password_bytes = bytes(API_PWD, "utf-8")
76
+ is_correct_password = secrets.compare_digest(
77
+ current_password_bytes, correct_password_bytes
78
+ )
79
+ if not (is_correct_username and is_correct_password):
80
+ raise HTTPException(
81
+ status_code=status.HTTP_401_UNAUTHORIZED,
82
+ detail="Incorrect username or password",
83
+ headers={"WWW-Authenticate": "Basic"},
84
+ )
85
+ return credentials.username
86
+
87
+
88
  @app.post("/summ_ru", response_model=Result)
89
+ async def ru_summ_api(
90
+ request: TextRequest, username: Annotated[str, Depends(get_current_username)]
91
+ ):
92
  results = pipe.summarize(request.text, lang="ru")
93
  logger.info(results)
94
  return results
95
 
96
 
97
  @app.post("/summ_en", response_model=Result)
98
+ async def en_summ_api(
99
+ request: TextRequest, username: Annotated[str, Depends(get_current_username)]
100
+ ):
101
  results = pipe.summarize(request.text, lang="en")
102
  logger.info(results)
103
  return results