File size: 3,802 Bytes
fabad77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7aa7d06
fabad77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9730a55
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
113
114
115
116
117
118
from __future__ import annotations
import gradio as gr
from gradio.themes.base import Base
from gradio.themes.utils import colors, fonts, sizes
import base64
import requests
import json
import os
class BaseTheme(Base):
    def __init__(
        self,
        *,
        primary_hue: colors.Color | str = colors.orange,
        secondary_hue: colors.Color | str = colors.blue,
        neutral_hue: colors.Color | str = colors.gray,
        spacing_size: sizes.Size | str = sizes.spacing_md,
        radius_size: sizes.Size | str = sizes.radius_md,
        text_size: sizes.Size | str = sizes.text_lg,
    ):
        super().__init__(
            primary_hue=primary_hue,
            secondary_hue=secondary_hue,
            neutral_hue=neutral_hue,
            spacing_size=spacing_size,
            radius_size=radius_size,
            text_size=text_size,
        )

basetheme = BaseTheme()

js_func = """
function refresh() {
    const url = new URL(window.location);

    if (url.searchParams.get('__theme') !== 'dark') {
        url.searchParams.set('__theme', 'dark');
        window.location.href = url.href;
    }
}
"""
def process(description):
    # Define the API endpoint and headers
    url = os.getenv("ENDPONT")
    headers = {
        "Content-Type": "application/json"
    }

    # Define the payload
    payload = {
        "text": description
    }

    # Make the POST request
    response = requests.post(url, headers=headers, data=json.dumps(payload))

    # Process the response
    if response.status_code == 200:
        data = response.json()
        category = data["results"].get("Category", "No Category Found")
        reasoning = data["results"].get("Reasoning", "No Reasoning Found")
        return category, reasoning
    else:
        return "Error", "An error occurred while processing the request"
        
    

# Convert the images to Base64
with open("logo_vistec.png", "rb") as logo_file:
    base64_logo = base64.b64encode(logo_file.read()).decode("utf-8")

# Gradio app
with gr.Blocks(title="Classification category of incident",theme=basetheme,js=js_func) as demo:
    # Add logo at the top using Base64 HTML
    with gr.Row():
        gr.HTML(
            f"""
                <div style="display: grid; grid-template-columns: 1fr 2fr 1fr; align-items: center;">
                    <div style="justify-self: start;">
                        <img src="data:image/png;base64,{base64_logo}" alt="Logo" style="width: 150px; height: auto;">
                    </div>
                    <div style="justify-self: center;">
                        <h2 style="margin: 0; text-align: center;">Classification category of incident</h2>
                    </div>
                    <div></div>
                </div>
            """
        )

    with gr.Row():
        with gr.Column():
            description_input = gr.Textbox(lines=15, label="รายละเอียดของการเกิดเหตุ")
            
            with gr.Row():
                clear_button = gr.Button("Clear")
                # Add two buttons: standard submit button and image-based button
                submit_button = gr.Button("Submit",variant="primary")
        with gr.Column():
            category_box = gr.Textbox(label="ประเภทของเรื่อง") 
            reasoning_box = gr.Textbox(label="เหตุผล", lines=5)


    # Link functionality to the standard submit button
    submit_button.click(
        fn=process,
        inputs=[description_input],
        outputs=[category_box, reasoning_box],
    )
    clear_button.click(
        fn=lambda: ("", "", ""),
        inputs=[],
        outputs=[description_input, category_box, reasoning_box]
    )


# Launch Gradio app
if __name__ == "__main__":
    demo.launch()