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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import os
import json
import numpy as np
import requests

import streamlit as st
import streamlit_nested_layout
from streamlit_ace import st_ace
from utils import *
# from io import StringIO 
# import base64
# from pathlib import Path

import warnings
warnings.simplefilter("ignore", UserWarning)

API = 'http://4.193.50.237:5000/api'

@st.experimental_memo
def load_session():
    return requests.Session()


def translate_panel(seed, length):
    with st.container():
        st.header("Code Translation")
            
        # Expand description panel 
        with st.expander('_:pencil: Quickly select demo example below_'):
            option = st.selectbox(
                '',
                ('Select Example', 'Example 1', 'Example 2', 'Example 3'),
                key='trans'
            )
            example_code = ""
            language = "python"
            index=0
            uploaded_file = st.file_uploader("or Upload your file: ", key='trans_file')
            if uploaded_file:
                # with open(uploaded_file, encoding='utf8') as f:
                example_code = uploaded_file.read().decode()

            if option == 'Example 1':
                example_code = TRANS_EXAMPLE1
                index=1

            elif option == "Example 2":
                example_code = TRANS_EXAMPLE2
                language = "java"
                index=0

            elif option == "Example 3":
                example_code = TRANS_EXAMPLE3
                language = "java"
                index=0

        if example_code  == '':
            content = st_ace('', placeholder="Input your code in HERE", language='python', auto_update=True, key="<trans-code>")
        else:
            content = st_ace(example_code, placeholder="Input your code in HERE", language=language, auto_update=True)
        
        target_lgs = st.selectbox(
            '*Select your target translate languages:*',
            ('Python', 'Java'),
            index=index
        )
        
        if st.button("**:repeat: Translate Code**"):
            with st.spinner('Translating...'):
                source_lgs = detect_lang(API, content)
                
                if target_lgs == source_lgs:
                    st.warning('Selected language can not be equal to source language', icon="⚠️")
                    trans_code = content
                else:
                    trans_code = request_api(
                    url=API, 
                    data={
                        'input': content,
                        'task': 'translate',
                        'source_lgs': source_lgs,
                        'target_lgs': target_lgs,
                        'seed': seed,
                        'length': length
                    },
                )
                    trans_code = json.loads(trans_code.text)
                    trans_code = trans_code['output']
                    
            st.write("Translated Code")
            st.code(trans_code, language=target_lgs.lower())
        

def complete_panel(seed, length):
    with st.container():
        st.header("Code Completion")
            
        # Expand description panel 
        with st.expander('_:pencil: Quickly select demo example below_'):
            option = st.selectbox(
                '',
                ('Select Example', 'Example 1', 'Example 2', 'Example 3'),
                key='complete'
            )
            example_code = ""

            if option == 'Example 1':
                example_code = COM_EXAMPLE1

            if option == "Example 2":
                example_code = COM_EXAMPLE2

            if option == "Example 3":
                example_code = COM_EXAMPLE3

        if example_code  == '':
            content = st_ace('', placeholder="Input your code in HERE", language='python', auto_update=True, key="<complete-code>")
        else:
            content = st_ace(example_code, placeholder="Input your code in HERE", language='python', auto_update=True)
        
        if st.button("**:writing_hand: Complete Code**"):
            with st.spinner('Completing ...'):
                source_lgs = detect_lang(API, content)
                trans_code = request_api(
                    url=API, 
                    data={
                        'input': content,
                        'task': 'completion',
                        'source_lgs': source_lgs,
                        'target_lgs': None,
                        'seed': seed,
                        'length': length
                    },
                )
                trans_code = json.loads(trans_code.text)
                    
            st.write("Completed Code")
            code = str(content).strip() + trans_code['output']
            st.code(code, language=source_lgs.lower())


def repair_panel(seed, length):
    with st.container():
        st.header("Code Repair")
            
        # Expand description panel 
        with st.expander('_:pencil: Quickly select demo example below_'):
            option = st.selectbox(
                '',
                ('Select Example', 'Example 1', 'Example 2', 'Example 3'),
                key='<example-repair>'
            )
            example_code = ""

            if option == 'Example 1':
                example_code = RE_EXAMPLE1

            if option == "Example 2":
                example_code = RE_EXAMPLE2

            if option == "Example 3":
                example_code = RE_EXAMPLE3

        if example_code  == '':
            content = st_ace('', placeholder="Input your code in HERE", language='python', auto_update=True, key="<repair-code>")
        else:
            content = st_ace(example_code, placeholder="Input your code in HERE", language='python', auto_update=True)
        
        if st.button("**:hammer_and_wrench: Repair Code**"):
            with st.spinner('Repairing . . .'):
                source_lgs = detect_lang(API, content)
                trans_code = request_api(
                    url=API, 
                    data={
                        'input': content,
                        'task': 'code_repair',
                        'source_lgs': source_lgs,
                        'target_lgs': None,
                        'seed': seed,
                        'length': length
                    },
                )
                
                trans_code = json.loads(trans_code.text)
                    
            st.write("Repaired Code")
            code = trans_code['output']
            st.code(code, language=source_lgs.lower())


def codegen_panel(seed, length):
    with st.container():
        st.header("Code Generation")
            
        # Expand description panel 
        with st.expander('_:pencil: Quickly select demo example below_'):
            option = st.selectbox(
                '',
                ('Select Example', 'Example 1', 'Example 2', 'Example 3'),
                key='<example-code-gen>'
            )
            example_code = ""
            uploaded_file = st.file_uploader("or Upload your file: ", key='gen_file')
            if uploaded_file:
                example_code = uploaded_file.read().decode()

            if option == 'Example 1':
                example_code = CGEN_EXAMPLE1

            if option == "Example 2":
                example_code = CGEN_EXAMPLE2

            if option == "Example 3":
                example_code = CGEN_EXAMPLE3

        if example_code  == '':
            content = st_ace('', placeholder="Input your code in HERE", auto_update=True, key="<gen-code-code>")
        else:
            content = st_ace(example_code, placeholder="Input your code in HERE", auto_update=True)
        
        target_lgs = st.selectbox(
            '*Select your target translate languages:*',
            ('Python', 'Java'),
            index=0,
            key="gen_select"
        )

        if st.button("**:hammer: Generate Code**"):
            with st.spinner('Generating . . .'):
                trans_code = request_api(
                    url=API, 
                    data={
                        'input': content,
                        'task': 'text2code',
                        'source_lgs': target_lgs.lower(),
                        'target_lgs': None,
                        'seed': seed,
                        'length': length
                    },
                )
                
                trans_code = json.loads(trans_code.text)
                    
            st.write("Generated Code")
            code = trans_code['output']
            st.code(code, language=target_lgs.lower())
    

def codesum_panel(seed, length):
    with st.container():
        st.header("Code Summarization")
            
        # Expand description panel 
        with st.expander('_:pencil: Quickly select demo example below_'):
            option = st.selectbox(
                '',
                ('Select Example', 'Example 1', 'Example 2', 'Example 3'),
                key='<example-sum>'
            )
            example_code = ""
            uploaded_file = st.file_uploader("or Upload your file: ", key='sum_file')
            if uploaded_file:
                # with open(uploaded_file, encoding='utf8') as f:
                example_code = uploaded_file.read().decode()

            if option == 'Example 1':
                example_code = SUM_EXAMPLE1

            if option == "Example 2":
                example_code = SUM_EXAMPLE2

            if option == "Example 3":
                example_code = SUM_EXAMPLE3

        if example_code  == '':
            content = st_ace('', placeholder="Input your code in HERE", language='python', auto_update=True, key="<sum-code>")
        else:
            content = st_ace(example_code, placeholder="Input your code in HERE", language='python', auto_update=True)
        
        task = 'code2text'
        with st.expander('Advanced Option'):
            param = st.checkbox('Summarize with param')
            if param:
                task += '_param'
        if st.button("**:hammer_and_wrench: Summarize Code**"):

            with st.spinner('Summarizing . . .'):
                source_lgs = detect_lang(API, content)
                trans_code = request_api(
                    url=API, 
                    data={
                        'input': content,
                        'task': task,
                        'source_lgs': source_lgs,
                        'target_lgs': None,
                        'seed': seed,
                        'length': length
                    },
                )
                trans_code = json.loads(trans_code.text)
                    
            st.write("Summarized Code")
            code = trans_code['output']
            st.code(code, language=source_lgs.lower())


def testgen_panel(seed, length):
    with st.container():
        st.header("Test Generation")
            
        # Expand description panel 
        with st.expander('_:pencil: Quickly select demo example below_'):
            option = st.selectbox(
                '',
                ('Select Example', 'Example 1', 'Example 2', 'Example 3'),
                key='<example-test>'
            )
            example_code = ""

            if option == 'Example 1':
                example_code = TEST_EXAMPLE1

            if option == "Example 2":
                example_code = TEST_EXAMPLE2

            if option == "Example 3":
                example_code = TEST_EXAMPLE3
        
        st.warning('Feature only support Java', icon="⚠️")
        
        if example_code  == '':
            content = st_ace('', placeholder="Input your code in HERE", language='python', auto_update=True, key="<test-code>")
        else:
            content = st_ace(example_code, placeholder="Input your code in HERE", language='python', auto_update=True)
        
        if st.button("**:hammer_and_wrench: Generate Test**"):
            with st.spinner('Generating . . .'):
                source_lgs = detect_lang(API, content)
                trans_code = request_api(
                    url=API, 
                    data={
                        'input': content,
                        'task': 'testgen',
                        'source_lgs': source_lgs,
                        'target_lgs': None,
                        'seed': seed,
                        'length': length
                    },
                )
                print(trans_code.text)
                trans_code = json.loads(trans_code.text)
                    
            st.write("Generated Test")
            code = trans_code['output']
            st.code(code, language=source_lgs.lower())


def member_infor(image_path, INFOR):
    col_1, col_2, col_3, col_4 = st.columns([1,1,2,6])
    avatar_image = Image.open(image_path)
    with col_2:
            st.image(avatar_image, width=100)
    with col_4:
        st.markdown(INFOR, unsafe_allow_html=True)

def logo(image_path):
    logo = load_image_from_local(image_path)
    st.image(logo, width=100)

def main_app():
    st.set_page_config(
        page_title="Docify-Lab",
        page_icon="🧊",
        layout="wide",
        # initial_sidebar_state="expanded"
    )
    
    col_title1, col_title2 = st.columns([1.5, 20])
    avatar_image = Image.open('./assets/ava_app.png')
    # st.markdown("<p style='text-align: center; color: grey;'>"+img_to_html('./assets/ava_app.png')+"</p>", unsafe_allow_html=True)

    with col_title1:
        st.image(avatar_image, width=70)
        # st.markdown("<p style='text-align: center; color: grey; width: 64px;'>"+img_to_html('./assets/ava_app.png')+"</p>", unsafe_allow_html=True)

    with col_title2:
        st.title("                 Welcome to Docify-Lab")
    
    # layout
    col1, col2 = st.columns([6, 4])
    # sess = load_session()
    
    # Advanced option
    st.sidebar.write("Advanced Custom")
    length = st.sidebar.slider('Generate length', 16, 1024, 512, 32)
    seed = st.sidebar.number_input('Model seed:', value=42, min_value=1, step=1)
    
    with col1:

        tab1, tab2, tab3, tab4, tab5, tab6 = st.tabs(
            ["Code Translation", "Test Generation", "Code Repair", 
            "Code Summarization", "Code Generation", "Code Completion"]
        )
        
        with tab1:
            translate_panel(seed, length)
        with tab2:
            testgen_panel(seed, length)
        with tab3:
            repair_panel(seed, length)
        with tab4:
            codesum_panel(seed, length)
        with tab5:
            codegen_panel(seed, length)
        with tab6:
            complete_panel(seed, length)
    
    with col2:
        c1, c2, c3, c4, c5, c6, c7 = st.columns([1,1,1,1,1,1,1])

        with c2:
            logo("./assets/fpt_logo.png")
        with c4:
            logo("./assets/AI_center_logo.png")
        with c6:
            logo("./assets/Docify-Logo.png")
        # st.markdown(SIDEBAR_INFO, unsafe_allow_html=True)


        with st.expander("What is Docify-Lab?", expanded=True):
            st.markdown(STORY, unsafe_allow_html=True)
        
        with st.expander("Docify Lab - FPT Software AI Center", expanded=True):
            tab_t1, tab_1, tab_t2, tab_t3, tab_t4, tab_t5, tab_t6 = st.tabs(
                ["About Us", "Leader","Member 1", "Member 2", 
                "Member 3", "Member 4", "Member 5"],
            )

            with tab_t1:
                st.markdown(GROUP_INFO, unsafe_allow_html=True)
            with tab_1:
                member_infor('./assets/Anh_Nghi_ava.png', LEADER_INFOR)
            with tab_t2:
                member_infor('./assets/Nam_ava.png', NAM_INFOR)
            with tab_t3:
                member_infor('./assets/Dung_ava.jpg', DUNG_INFOR)
            with tab_t4:
                member_infor('./assets/Minh_ava.JPG', MINH_INFOR)
            with tab_t5:
                member_infor('./assets/VA_ava.jpg', VA_INFOR)
            with tab_t6:
                member_infor('./assets/KA_ava.jpg', KA_INFOR)
    
if __name__ == '__main__':
    main_app()