File size: 4,533 Bytes
0392324
 
 
 
 
 
 
 
 
 
 
 
58a91e1
0392324
 
58a91e1
 
 
0392324
 
 
 
 
 
58a91e1
 
69f0938
58a91e1
 
 
 
 
 
9c405cd
58a91e1
 
 
9c405cd
58a91e1
 
9c405cd
58a91e1
 
0392324
 
58a91e1
0d74617
0392324
58a91e1
 
 
 
 
 
8dec221
58a91e1
 
 
 
 
 
 
 
 
0392324
 
 
58a91e1
 
 
0392324
58a91e1
0392324
 
 
8dec221
0392324
 
 
 
 
 
 
58a91e1
 
0392324
58a91e1
0392324
58a91e1
0392324
 
58a91e1
0392324
 
808bf67
 
0392324
58a91e1
0392324
 
 
 
 
 
 
 
 
 
 
 
8dec221
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import json
import requests
from PIL import Image

import streamlit as st
from Login import auth_page

st.set_page_config(
    page_title="Welcome",
    page_icon="👋",
    layout="wide",
    initial_sidebar_state="expanded",
    menu_items={"About": "Built by @HemanthSai7 and @MayureshAgashe2107 with Streamlit"},
)

st.markdown("## :rainbow[Welcome to Techdocs: Where Code Meets Clarity!] 🚀")



def logout():
    del st.session_state["access_token"]
    del st.session_state["refresh_token"]
    del st.session_state["username"]

def instructions():
    with st.expander("📝Instructions",expanded=True):
        st.info("Please note that the Streamlit app is just a preview to give the user an idea of the project. To witness the full power of Techdocs, please use the CLI. The instructions to use the CLI are listed in Usage page")     
        st.markdown(
            """
            ##### 1. Generate an `API Key` from the sidebar to get started.

            ##### 2. Paste the  `API Key` in the field provided.

            ##### 3. Paste your `code function` in the input code box.

            ##### 4. Click on the `Generate Documentation` 🤖 button to generate the documentation.
            
            ##### 5. The `generated documentation` will be displayed in the section below.

            """
        )


with st.sidebar:
    if 'username' not in st.session_state:
        with st.expander("🧑Account Details",expanded=True):
            st.warning("Please Login or Signup to continue")
    else:
        
        with st.expander("🔑 TECHDOCS-API-KEY",expanded=True):
            st.warning("Generating a new API Key will invalidate the previous one from all your projects. Do you wish to continue?")
            if st.button("Generate API KEY"):
                with st.spinner("Generating API Key..."):
                    try:
                        base_url = "https://caffeinecrew-techdocs.hf.space"
                        headers={"accept":"application/json", "Authorization": f"Bearer {st.session_state.access_token}"}
                        response = requests.put(url=base_url + "/auth/regenerate_api_key", headers=headers, data=json.dumps({"username":st.session_state.username}))
                        if (response.status_code!=200):
                            raise Exception("API Key Generation Failed")
                        st.info("Please save the API KEY as it will be shown only once.")
                        st.code(response.json()["api_key"],"bash")
                        st.success("API Key Generated Successfully")
                    except Exception as e:
                        st.error(e)
        


        with st.expander("🧑Account Details",expanded=True):
            st.info(f"Welcome, {st.session_state.username}! 😄")
            if st.button("Logout 👋"):
                logout()
                st.rerun()


def code_page():
    base_url = 'https://caffeinecrew-techdocs.hf.space'

    def query_post(url, headers, data=None, params=None):
        response = requests.post(url, data=data, headers=headers, params=params)
        return response
    
    headers={"accept":"application/json"}

    instructions()
    st.warning("Hi there! Paste your TECHDOCS-API-KEY in the field below to get started!\n\n", icon="🚨")
    API_KEY = st.text_input(label="Enter your API key", label_visibility="hidden",placeholder="Enter your API key", type="password")
  

    code_input = st.text_area("Code Input", height=300, help="Paste your code here")
    comment_placeholder = st.empty()

    if st.button("🤖 Generate Documentation"):
        if code_input:
            headers['Authorization'] = f"Bearer {st.session_state.access_token}"
            response = query_post(base_url + '/api/inference', headers=headers, 
                                  data=json.dumps({'code_block':code_input, 'api_key':API_KEY}))
            docstr = response.json()["docstr"]
            comment_placeholder.subheader("Generated Documentation:")
            comment_placeholder.markdown(f"<pre><code>{docstr}</code></pre>", unsafe_allow_html=True)
            # Scroll to the comment section
            comment_placeholder.empty()
            comment_placeholder.markdown(f"<pre><code>{docstr}</code></pre>", unsafe_allow_html=True)
        else:
            st.warning("Please enter some code.")


if 'access_token' not in st.session_state:
    st.session_state.runpage = auth_page  
else:
    st.session_state.runpage = code_page
st.session_state.runpage()