File size: 4,014 Bytes
2217335
 
1823861
b380c20
2217335
 
 
 
 
fe586f2
1823861
 
2217335
 
 
b466a33
2217335
 
b466a33
 
2217335
b466a33
2217335
 
 
 
 
b466a33
2217335
 
 
 
b466a33
2217335
 
 
 
1823861
2217335
050bf4e
 
97d85a1
2217335
 
 
 
b380c20
2217335
 
 
 
 
f5848c0
f005840
f941a22
 
 
 
 
2217335
1823861
 
c774338
 
 
 
2daa8fa
c774338
 
1823861
2217335
 
3b1628f
b466a33
050bf4e
 
 
3b1628f
 
7b5efbb
 
3b1628f
 
 
7b5efbb
 
3b1628f
 
 
 
1823861
 
2217335
 
 
 
 
 
 
 
 
 
 
 
f005840
 
2217335
 
 
 
1823861
 
b466a33
1823861
 
2217335
1823861
2217335
1823861
b466a33
3b1628f
b466a33
2217335
 
 
 
 
 
 
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
119
120
121
122
123
124
125
126
127
128
129
import os
import gradio as gr
from gradio.components import Textbox, Button, Slider, Checkbox
from AinaTheme import theme
from urllib.error import HTTPError

from rag import RAG
from utils import setup

MAX_NEW_TOKENS = 700
SHOW_MODEL_PARAMETERS_IN_UI = os.environ.get("SHOW_MODEL_PARAMETERS_IN_UI", default="True") == "True"

setup()


rag = RAG(embeddings_model=os.getenv("EMBEDDINGS"))


def eadop_rag(prompt, num_chunks):
    model_parameters = {"NUM_CHUNKS": num_chunks}
    try:
        retrun rag.get_context(prompt, model_parameters)
    except HTTPError as err:
        if err.code == 400:
            gr.Warning(
                "The inference endpoint is only available Monday through Friday, from 08:00 to 20:00 CET."
            )
        return None, None, None
    except:
        gr.Warning(
            "Inference endpoint is not available right now. Please try again later."
        )
        return None, None, None


def clear():
    return (
        None, 
        None,
        None,
        None,
        gr.Slider(value=2.0),
    )


def gradio_app():
    with gr.Blocks(theme=theme) as demo:
        with gr.Row(equal_height=True):
            with gr.Column(variant="panel"):
                input_ = Textbox(
                    lines=11,
                    label="Input",
                    placeholder="Quina és la finalitat del Servei Meteorològic de Catalunya?",
                )
                with gr.Row(variant="panel"):
                    clear_btn = Button(
                        "Clear",
                    )
                    submit_btn = Button("Submit", variant="primary", interactive=False)

                with gr.Row(variant="panel"):
                    with gr.Accordion("Model parameters", open=False, visible=SHOW_MODEL_PARAMETERS_IN_UI):
                        num_chunks = Slider(
                            minimum=1,
                            maximum=6,
                            step=1,
                            value=2,
                            label="Number of chunks"
                        )

            with gr.Column(variant="panel"):
                output = Textbox(
                    lines=10, 
                    label="Context", 
                    interactive=False, 
                    show_copy_button=True
                )
                with gr.Accordion("Sources and context:", open=False):
                    source_context = gr.Markdown(
                        label="Sources",
                        show_label=False,
                    )
                    with gr.Accordion("See full context evaluation:", open=False):
                        context_evaluation = gr.Markdown(
                            label="Full context",
                            show_label=False,
                            # interactive=False, 
                            # autoscroll=False,
                            # show_copy_button=True
                        )
                

        input_.change(
            fn=change_interactive,
            inputs=[input_],
            outputs=[clear_btn, submit_btn],
            api_name=False,
        )

        input_.change(
            fn=None,
            inputs=[input_],
            api_name=False,
            js="""(i, m) => {
            document.getElementById('inputlength').textContent = i.length + '  '
            document.getElementById('inputlength').style.color =  (i.length > m) ? "#ef4444" : "";
        }""",
        )

        clear_btn.click(
            fn=clear, 
            inputs=[], 
            outputs=[input_, output, source_context, context_evaluation, num_chunks],
              queue=False, 
              api_name=False
        )
        
        submit_btn.click(
            fn=submit_input, 
            inputs=[input_, num_chunks], 
            outputs=[output, source_context, context_evaluation],
            api_name="get-eadop-rag"
        )

        demo.launch(show_api=True)


if __name__ == "__main__":
    gradio_app()