File size: 2,069 Bytes
2217335
 
1823861
b380c20
2217335
858a66e
2217335
fe586f2
1823861
2217335
858a66e
2217335
b466a33
858a66e
 
2217335
858a66e
2217335
858a66e
 
 
2217335
 
050bf4e
 
97d85a1
2217335
 
 
b380c20
2217335
858a66e
 
 
 
 
 
2217335
858a66e
 
 
 
 
 
 
 
c774338
 
 
2daa8fa
c774338
 
858a66e
 
 
 
 
2217335
 
1823861
 
858a66e
1823861
 
2217335
1823861
2217335
02212c9
b466a33
858a66e
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
import os
import gradio as gr
from gradio.components import Textbox, Button, Slider, Checkbox
from AinaTheme import theme

from vectorstore import VectorStore

MAX_NEW_TOKENS = 700


vectorStore = VectorStore(embeddings_model=os.getenv("EMBEDDINGS", "BAAI/bge-m3"))

def eadop_rag(prompt, num_chunks):
    prompt = prompt.strip()
    if prompt == "":
        gr.Warning(
            "Prompt can't be empty!"
        )
        raise ValueError("Prompt can't be empty!")
    return vectorStore.get_context(prompt, num_chunks)
    
def clear():
    return (
        None,
        None,
        gr.Slider(value=2.0),
    )

def gradio_app():
    with gr.Blocks(theme=theme) as demo:
        with gr.Row(equal_height=True):
            output = Textbox(
                lines=10, 
                label="Context", 
                interactive=False, 
                show_copy_button=True
            )

        with gr.Row(equal_height=True):
            input_ = Textbox(
                label="Input",
                placeholder="Quina és la finalitat del Servei Meteorològic de Catalunya?",
            )

        with gr.Row(equal_height=True):
            num_chunks = Slider(
                            minimum=1,
                            maximum=6,
                            step=1,
                            value=2,
                            label="Number of chunks"
                        )
        with gr.Row(equal_height=True):
            clear_btn = Button("Clear")
        with gr.Row(equal_height=True):
            submit_btn = Button("Submit", variant="primary")
                        

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

        demo.launch(show_api=True)


if __name__ == "__main__":
    gradio_app()