gruhit-patel commited on
Commit
8bc8944
1 Parent(s): 826db2e

Added API KEY for api call verification

Browse files
Files changed (2) hide show
  1. QuoteGenerator.py +0 -1
  2. main.py +18 -13
QuoteGenerator.py CHANGED
@@ -26,7 +26,6 @@ class QuoteGenerator():
26
  top_p:float=0.9, top_k:int=5):
27
 
28
  tags = self.preprocess_tags(tags)
29
- print("Tags = ", tags)
30
  output = self.quote_generator(tags, min_length=min_length, max_length=max_length,
31
  temperature=1.0, top_k=5, top_p=top_p, early_stopping=True,
32
  num_beams=4)
 
26
  top_p:float=0.9, top_k:int=5):
27
 
28
  tags = self.preprocess_tags(tags)
 
29
  output = self.quote_generator(tags, min_length=min_length, max_length=max_length,
30
  temperature=1.0, top_k=5, top_p=top_p, early_stopping=True,
31
  num_beams=4)
main.py CHANGED
@@ -1,24 +1,29 @@
1
- from fastapi import FastAPI, Request
2
- from fastapi.middleware.cors import CORSMiddleware
3
- from fastapi.middleware.trustedhost import TrustedHostMiddleware
4
  from QuoteGenerator import QuoteGenerator
5
  from typing import Union
6
  from pydantic import BaseModel
7
  import time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  class QuoteRequest(BaseModel):
10
  tags: Union[None, str]
11
 
12
  app = FastAPI()
13
 
14
- origins = ['quote-generator.streamlit.app']
15
-
16
- # Middleware to valiate host
17
- app.add_middleware(
18
- TrustedHostMiddleware,
19
- allowed_hosts=origins
20
- )
21
-
22
  #Middleware to note time
23
  @app.middleware("http")
24
  async def note_response_time(request: Request, call_next):
@@ -31,11 +36,11 @@ async def note_response_time(request: Request, call_next):
31
  quote_generator = QuoteGenerator()
32
  quote_generator.load_generator()
33
 
34
- @app.get("/")
35
  def root(request: Request):
36
  return {"message": "This is the website for quote-generator"}
37
 
38
- @app.post("/generate_quote")
39
  def generate_quote(req: QuoteRequest):
40
  print("Tags: ", req.tags)
41
  generated_quote_oup = quote_generator.generate_quote(req.tags)
 
1
+ from fastapi import FastAPI, Request, Depends, HTTPException, status
2
+ from fastapi.security import OAuth2PasswordBearer
 
3
  from QuoteGenerator import QuoteGenerator
4
  from typing import Union
5
  from pydantic import BaseModel
6
  import time
7
+ import os
8
+
9
+ # API to key to validate the Referer
10
+ API_KEY = os.getenv('API_KEY')
11
+
12
+ oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
13
+
14
+ # Function to check of the incoming API call is from valid host or not
15
+ def api_key_auth(api_key:str = Depends(oauth2_scheme)):
16
+ if api_key != API_KEY:
17
+ raise HTTPException(
18
+ status_code = status.HTTP_401_UNAUTHORIZED,
19
+ detail="Forbidden Access"
20
+ )
21
 
22
  class QuoteRequest(BaseModel):
23
  tags: Union[None, str]
24
 
25
  app = FastAPI()
26
 
 
 
 
 
 
 
 
 
27
  #Middleware to note time
28
  @app.middleware("http")
29
  async def note_response_time(request: Request, call_next):
 
36
  quote_generator = QuoteGenerator()
37
  quote_generator.load_generator()
38
 
39
+ @app.post("/", dependencies=[Depends(api_key_auth)])
40
  def root(request: Request):
41
  return {"message": "This is the website for quote-generator"}
42
 
43
+ @app.post("/generate_quote", dependencies=[Depends(api_key_auth)])
44
  def generate_quote(req: QuoteRequest):
45
  print("Tags: ", req.tags)
46
  generated_quote_oup = quote_generator.generate_quote(req.tags)