KN123 commited on
Commit
b2c5d32
1 Parent(s): c31d89b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -2
app.py CHANGED
@@ -1,4 +1,5 @@
1
- from fastapi import FastAPI
 
2
  from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
3
  from typing import List, Dict
4
  import time
@@ -33,6 +34,13 @@ def inference(question: str, tables: Dict[str, List[str]]) -> str:
33
  return result
34
 
35
  app = FastAPI()
 
 
 
 
 
 
 
36
 
37
  @app.get("/")
38
  def home():
@@ -43,7 +51,7 @@ def home():
43
  "description": "This api can be used to convert natural language to SQL given the human prompt, tables and the attributes."
44
  }
45
 
46
- @app.get("/generate")
47
  def generate(text:str):
48
  start = time.time()
49
  res = inference("how many people with name jui and age less than 25", {
@@ -65,6 +73,33 @@ def generate(text:str):
65
  }
66
  }
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
  if __name__ == "__main__":
70
  uvicorn.run(app, host="127.0.0.1", port=8000)
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from fastapi.middleware.cors import CORSMiddleware
3
  from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
4
  from typing import List, Dict
5
  import time
 
34
  return result
35
 
36
  app = FastAPI()
37
+ app.add_middleware(
38
+ CORSMiddleware,
39
+ allow_origins=["*"], # Allows all origins
40
+ allow_credentials=True,
41
+ allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"], # Allows all methods
42
+ allow_headers=["*"], # Allows all headers
43
+ )
44
 
45
  @app.get("/")
46
  def home():
 
51
  "description": "This api can be used to convert natural language to SQL given the human prompt, tables and the attributes."
52
  }
53
 
54
+ @app.get("/test-generate")
55
  def generate(text:str):
56
  start = time.time()
57
  res = inference("how many people with name jui and age less than 25", {
 
73
  }
74
  }
75
 
76
+ @app.post("/generate")
77
+ def generate(request_body:Dict):
78
+ if 'text' not in request_body or 'tables' not in request_body:
79
+ raise HTTPException(status_code=400, detail="Missing 'text' or 'tables' in request body")
80
+
81
+ prompt = request_body['text']
82
+ tables = request_body['tables']
83
+
84
+ start = time.time()
85
+ res = inference(prompt, tables)
86
+ end = time.time()
87
+ total_time_taken = end - start
88
+ current_utc_datetime = datetime.datetime.now(datetime.timezone.utc)
89
+ current_date = datetime.date.today()
90
+ timezone_name = time.tzname[time.daylight]
91
+ print(res)
92
+ return {
93
+ "api_response": f"{res}",
94
+ "time_taken(s)": f"{total_time_taken}",
95
+ "request_details": {
96
+ "utc_datetime": f"{current_utc_datetime}",
97
+ "current_date": f"{current_date}",
98
+ "timezone_name": f"{timezone_name}"
99
+ }
100
+ }
101
+
102
+
103
 
104
  if __name__ == "__main__":
105
  uvicorn.run(app, host="127.0.0.1", port=8000)