G.Hemanth Sai commited on
Commit
0392324
β€’
1 Parent(s): 1e1ce36

Streamlit frontend

Browse files
frontend/Login.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import requests
3
+
4
+ import streamlit as st
5
+
6
+
7
+ def auth_page():
8
+
9
+ base_url = 'http://127.0.0.1:8000'
10
+
11
+
12
+ headers={"accept":"application/json"}
13
+
14
+ tab1, tab2 = st.tabs(["Signup", "Login"])
15
+
16
+ with tab1:
17
+ with st.form(key="myform1"):
18
+ username = st.text_input(label="Username", label_visibility="collapsed", placeholder="Username")
19
+ password = st.text_input(label="Password", label_visibility="collapsed", placeholder="Password", type="password")
20
+ email = st.text_input(label="Email", label_visibility="collapsed", placeholder="Email")
21
+ signup_button = st.form_submit_button(label="Signup")
22
+
23
+ with st.spinner("Signing up..."):
24
+ if signup_button:
25
+ try:
26
+ credentials = {"username":username, "password":password, "email":email}
27
+ response = requests.post(url=base_url + "/auth/signup", headers=headers, data=json.dumps(credentials))
28
+ if (response.status_code!=200):
29
+ raise Exception("Signup Failed")
30
+
31
+ st.success("Signed up successfully")
32
+ except:
33
+ st.error("Signup Failed")
34
+
35
+
36
+ with tab2:
37
+ with st.form(key="myform2"):
38
+ username = st.text_input(label="Username", label_visibility="collapsed", placeholder="Username")
39
+ password = st.text_input(label="Password", label_visibility="collapsed", placeholder="Password", type="password")
40
+ login_button = st.form_submit_button(label="Login")
41
+
42
+ with st.spinner("Logging in..."):
43
+ if login_button:
44
+ try:
45
+ credentials = {"username":username, "password":password}
46
+ response = requests.post(base_url + "/auth/login", headers=headers, data=json.dumps(credentials))
47
+ if (response.status_code!=200):
48
+ raise Exception("Login Failed")
49
+ # res_dict.update(response.json())
50
+ st.session_state["username"] = username
51
+ st.session_state["access_token"] = response.json()['access_token']
52
+ st.session_state["refresh_token"] = response.json()['refresh_token']
53
+ st.success("Logged in successfully")
54
+ st.experimental_rerun()
55
+
56
+ except Exception as e:
57
+ st.error(e)
58
+
59
+
frontend/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+
frontend/pages/Code.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import requests
3
+ from PIL import Image
4
+
5
+ import streamlit as st
6
+ from Login import auth_page
7
+ # frontend.Login import auth_pageom from streamlit_extras import
8
+
9
+
10
+ st.set_page_config(
11
+ page_title="Welcome",
12
+ page_icon="πŸ‘‹",
13
+ layout="wide",
14
+ initial_sidebar_state="expanded",
15
+ )
16
+
17
+ st.write("# DocGup-tea: AI based Documentation Generator πŸ’€")
18
+
19
+ def logout():
20
+ del st.session_state["access_token"]
21
+ del st.session_state["refresh_token"]
22
+ del st.session_state["username"]
23
+
24
+ with st.sidebar:
25
+ if 'username' not in st.session_state:
26
+ st.header("Login/Signup")
27
+ else:
28
+ st.header(f"Welcome, {st.session_state.username}!")
29
+ st.warning("Generating a new API Key will invalidate the previous one from all your projects. Do you wish to continue?")
30
+ if st.button("Generate API KEY"):
31
+ with st.spinner("Generating API Key..."):
32
+ try:
33
+ base_url = "http://localhost:8000"
34
+ headers={"accept":"application/json", "Authorization": f"Bearer {st.session_state.access_token}"}
35
+ response = requests.put(url=base_url + "/auth/regenerate_api_key", headers=headers, data=json.dumps({"username":st.session_state.username}))
36
+ if (response.status_code!=200):
37
+ raise Exception("API Key Generation Failed")
38
+ st.info("Please save the API KEY as it will be shown only once.")
39
+ st.code(response.json()["api_key"],"bash")
40
+ st.success("API Key Generated Successfully")
41
+ except Exception as e:
42
+ st.error(e)
43
+
44
+
45
+
46
+ with st.expander("More Options"):
47
+ if st.button("Logout"):
48
+ logout()
49
+ st.experimental_rerun()
50
+
51
+
52
+ def code_page():
53
+ base_url = 'http://localhost:8000'
54
+
55
+ def query_post(url, headers, data=None, params=None):
56
+ response = requests.post(url, data=data, headers=headers, params=params)
57
+ return response
58
+
59
+ headers={"accept":"application/json"}
60
+
61
+
62
+ API_KEY = st.text_input(label="Enter your API key", label_visibility="hidden",placeholder="Enter your API key", type="password")
63
+ st.title("Code Documentation Generator")
64
+ st.write("Enter your code and click 'Generate Comment' to get the corresponding comment.")
65
+
66
+ code_input = st.text_area("Code Input", height=200)
67
+ comment_placeholder = st.empty()
68
+
69
+ if st.button("Generate Comment"):
70
+ if code_input:
71
+ headers['Authorization'] = f"Bearer {st.session_state.access_token}"
72
+ response = query_post(base_url + '/api/inference', headers=headers, params={'code_block':"def add(a,b):\n\treturn a+b", 'api_key':API_KEY})
73
+ docstr = response.json()["docstr"]
74
+ print(docstr)
75
+ comment_placeholder.subheader("Generated Comment:")
76
+ comment_placeholder.markdown(f"<pre><code>{docstr}</code></pre>", unsafe_allow_html=True)
77
+ # Scroll to the comment section
78
+ comment_placeholder.empty()
79
+ comment_placeholder.markdown(f"<pre><code>{docstr}</code></pre>", unsafe_allow_html=True)
80
+ else:
81
+ st.warning("Please enter some code.")
82
+
83
+
84
+ if 'access_token' not in st.session_state:
85
+ st.session_state.runpage = auth_page
86
+ else:
87
+ st.session_state.runpage = code_page
88
+ st.session_state.runpage()
frontend/🏑 Home.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from Login import auth_page
3
+ from PIL import Image
4
+
5
+ import base64
6
+
7
+ # image2=Image.open('assets/logo2.png')
8
+ st.set_page_config(
9
+ page_title="DocGup-tea",
10
+ layout="wide",
11
+ page_icon="🏑",
12
+ initial_sidebar_state="expanded",
13
+ )
14
+
15
+ @st.cache_data
16
+ def get_base64_bin_file(bin_file):
17
+ with open(bin_file, 'rb') as f:
18
+ data = f.read()
19
+ return base64.b64encode(data).decode()
20
+
21
+
22
+ # image = Image.open('assets/poster.jpg')
23
+ # st.image(image, caption='ELIGILOAN')
24
+
25
+ st.markdown("# :DocGup-tea: AI based Documentation Generator πŸ“ƒ")
26
+
27
+ def logout():
28
+ del st.session_state["access_token"]
29
+ del st.session_state["refresh_token"]
30
+ del st.session_state["username"]
31
+
32
+ with st.sidebar:
33
+ if 'username' not in st.session_state:
34
+ st.header("Login/Signup")
35
+ else:
36
+ st.header(f"Welcome, {st.session_state.username}!")
37
+ if st.button("Logout"):
38
+ logout()
39
+ st.experimental_rerun()
40
+
41
+
42
+ def home_page():
43
+
44
+
45
+ def set_page_background(png_file):
46
+ bin_str = get_base64_bin_file(png_file)
47
+ page_bg_img = f'''
48
+ <style>
49
+ .stApp {{
50
+ background-image: url("data:image/jpg;base64,{bin_str}");
51
+ background-size: cover;
52
+ background-repeat: no-repeat;
53
+ background-attachment: scroll;
54
+ }}
55
+ </style>
56
+ '''
57
+ st.markdown(page_bg_img, unsafe_allow_html=True)
58
+
59
+ set_page_background("../assets/bg.jpg")
60
+
61
+ st.markdown(
62
+ """
63
+
64
+ ## A step-by-step guide
65
+
66
+ The process is quite straightforward. BOB offers loans to eligible applicants with strong financial profiles.
67
+ Individuals need to provide their basic personal, employment, income and property details to know if you are fit to apply for a loan.
68
+
69
+ ### 1 . Login
70
+
71
+ Step one is the login part, you just have to work your way through the following simple steps.
72
+ - Enter your mobile number
73
+ - Enter the OTP recieved
74
+ - accept consent in the Safe portal
75
+
76
+ ### 2. Loan page
77
+
78
+ Once you login, you will be redirected to the loan page.
79
+ - Offer all relevant details such as loan amount, loan history, income, etc.
80
+ - Click on the submit option once you have filled in all the details.
81
+ - Our algorithm will assess your eligibility based on the details provided by you and you will be awarded with a `yes` or a `no`.
82
+ """
83
+ )
84
+
85
+ if 'access_token' not in st.session_state:
86
+ st.session_state.runpage = auth_page
87
+ else:
88
+ st.session_state.runpage = home_page
89
+ st.session_state.runpage()
90
+