G.Hemanth Sai commited on
Commit
addbc00
1 Parent(s): 53d5196

Initialized schema and fastapi app

Browse files
.gitignore ADDED
File without changes
backend/__init__.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import mysql.connector
2
+ from mysql.connector import errorcode
3
+
4
+ from fastapi import FastAPI, status
5
+ from fastapi.exceptions import HTTPException
6
+
7
+ from backend.utils import DBConnection
8
+
9
+ from langchain.llms import CTransformers
10
+ from langchain.chains import LLMChain
11
+ from langchain import PromptTemplate
12
+
13
+ app = FastAPI(title="DocGup-Tea",
14
+ version="V0.0.1",
15
+ description="API for automatic code documentation generation!"
16
+ )
17
+
18
+ from backend import router
19
+
20
+ try:
21
+ dbconnection = DBConnection()
22
+ test_conn = DBConnection.get_client().get_server_info()
23
+
24
+ # send prompt codellama-13b-instruct-GGUF model
25
+ with open("docguptea/utils/prompt.txt",'r') as f:
26
+ prompt = f.read()
27
+ print(prompt)
28
+
29
+ prompt = PromptTemplate(template=prompt,
30
+ input_variables=['query'])
31
+
32
+ llm = CTransformers(
33
+ model = "docguptea/static/models/codellama-13b-instruct.Q4_K_M.gguf",
34
+ model_type="llama",
35
+ max_new_tokens = 1096,
36
+ temperature = 0.25,
37
+ repetition_penalty = 1.13,
38
+ stream=True,
39
+ gpu_layers = 10,
40
+ )
41
+
42
+ llmchain = LLMChain(
43
+ prompt=prompt,
44
+ llm=llm
45
+ )
46
+
47
+ app.state.llmchain = llmchain
48
+
49
+ except mysql.connector.Error as err:
50
+ raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(err))
51
+
52
+
backend/models/__init__.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ from .auth import *
2
+ from .generic import *
3
+ from .inference import *
backend/models/auth.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel, Field, EmailStr
2
+ from typing import Union, List, Tuple
3
+
4
+ from .generic import Base
5
+
6
+
7
+ class TokenSchema(Base):
8
+ access_token: str
9
+ refresh_token: str
10
+
11
+
12
+ class UserAuth(Base):
13
+ username: str = Field(..., description="username")
14
+ password: str = Field(..., min_length=5, max_length=24, description="user password")
15
+ email: EmailStr
16
+
17
+
18
+ class User(Base):
19
+ username: str
20
+ email: EmailStr
21
+
22
+ class TokenPayload(Base):
23
+ sub: str = None
24
+ exp: int = None
25
+
26
+
27
+ class LoginCreds(Base):
28
+ username: str
29
+ password: str
30
+
31
+ class APIKey(Base):
32
+ api_key: str
backend/models/generic.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel
2
+ from typing import List
3
+
4
+
5
+ class Base(BaseModel):
6
+ @classmethod
7
+ def get_instance(cls, **kwargs):
8
+ return cls(**kwargs)
9
+
10
+
11
+ class GeneralResponse(Base):
12
+ status:str
13
+ message: List[str]
14
+ data:dict
backend/models/inference.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel
2
+ from typing import List
3
+ from .generic import Base
4
+
5
+ class Inference(Base):
6
+ docstr:str
backend/router.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import Request, Depends, UploadFile
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+
4
+ from docguptea import app
5
+ from docguptea.utils import DBConnection
6
+ from docguptea.models import *
7
+ from docguptea.services.auth import *
8
+
9
+
10
+ app.add_middleware(
11
+ CORSMiddleware,
12
+ allow_origins=["*"],
13
+ allow_credentials=True,
14
+ allow_methods=["*"],
15
+ allow_headers=["*"],
16
+ )
17
+
18
+ @app.get("/api/response_check", tags=["Resource Server"])
19
+ def api_response_check():
20
+ response_result = GeneralResponse.get_instance(data={},
21
+ status="not_allowed",
22
+ message=["Not authenticated"]
23
+ )
24
+
25
+ try:
26
+ db_msg = ""
27
+ if DBConnection.is_connected():
28
+ db_msg = "Connection Successful to db!"
29
+ else:
30
+ db_msg = "Connection failed to db"
31
+
32
+ response_result.message.append(db_msg)
33
+
34
+ except Exception as e:
35
+ print("Exception :", e)
36
+
37
+ return response_result
38
+
39
+ @app.post("/auth/signup", summary="Creates new user account", response_model=GeneralResponse, tags=["Auth Server"])
40
+ async def signup(response: UserAuth):
41
+ response_result = GeneralResponse.get_instance(data={},
42
+ status="not_allowed",
43
+ message=["Not authenticated"]
44
+ )
45
+ ops_signup(response_result, response)
46
+
47
+ return response_result
48
+
49
+ @app.post("/auth/login", summary="Logs in user", response_model=TokenSchema, tags=["Auth Server"])
50
+ async def login(response:LoginCreds):
51
+ return ops_login(response)
52
+
53
+ @app.put("/auth/regenerate_api_key",summary="Forget Password",response_model=APIKey,tags=["Auth Server"],dependencies=[Depends(JWTBearer())])
54
+ async def regenerate_api_key(access_token: str = Depends(JWTBearer())):
55
+ user_sub=Auth.get_user_credentials(access_token)
56
+
57
+ return ops_regenerate_api_key(user_sub)
58
+
59
+ @app.post("/api/inference", summary="Inference", response_model=Inference, tags=["Resource Server"], dependencies=[Depends(JWTBearer())])
60
+ async def inference(code_block:str, api_key: str,access_token:str=Depends(JWTBearer())):
61
+ user_sub=Auth.get_user_credentials(access_token)
62
+
63
+ print("after res")
64
+
65
+ return ops_inference(code_block,api_key,user_sub)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ streamlit
2
+ fastapi