File size: 2,867 Bytes
14e11d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bb422f
14e11d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import xxhash
from gradio.components import _Keywords

from ai import AI
from config import Config
from contents import *
from storage import Storage


def webui(cfg: Config):
    """Run the web UI."""
    Webui(cfg).run()


class Webui:
    def __init__(self, cfg: Config):
        self.cfg = cfg
        self.ai = AI(cfg)
        self.storage = Storage.create_storage(self.cfg)  # Initialize storage here

    def _save_to_storage(self, contents, hash_id):
        print(f"Saving to storage {hash_id}")
        print(f"Contents: \n{contents}")
        self.storage = Storage.create_storage(self.cfg)
        if self.storage.been_indexed(hash_id):
            return 0
        else:
            embeddings, tokens = self.ai.create_embeddings(contents)
            self.storage.add_all(embeddings, hash_id)
            return tokens

    def _get_hash_id(self, contents):
        return xxhash.xxh3_128_hexdigest('\n'.join(contents))

    def run(self):
        with gr.Blocks(theme=gr.themes.Monochrome(), css="footer {visibility: hidden}") as demo:

            hash_id_state = gr.State('dd771cb6c4718ace4c4c596f4792cfdd')  # Initialize hash_id_state to 'dd771cb6c4718ace4c4c596f4792cfdd'
            chat_page = gr.Column(visible=True)  # Set chat_page to visible by default

            with chat_page:
                with gr.Row():
                    with gr.Column():
                        chatbot = gr.Chatbot(label="Kanunla Konuş")
                        msg = gr.Textbox(label="4857 Sayılı İş Kanunu ile ilgili tüm sorularınızı bekliyoruz.")
                        submit_box = gr.Button("Kanuna Sor", variant="primary")

                def respond(message, chat_history, hash_id):
                    kw = self.ai.get_keywords(message)
                    if len(kw) == 0 or hash_id is None:
                        return "", chat_history
                    _, kw_ebd = self.ai.create_embedding(kw)
                    ctx = self.storage.get_texts(kw_ebd, hash_id)
                    print(f"Context: \n{ctx}")
                    bot_message = self.ai.completion(message, ctx)
                    chat_history.append((message, bot_message))
                    return "", chat_history, \

                def reset():
                    return {
                        chat_page: gr.update(visible=True),
                        chatbot: gr.update(value=[]),
                        msg: gr.update(value=""),
                        hash_id_state: 'dd771cb6c4718ace4c4c596f4792cfdd',
                    }

                msg.submit(respond, [msg, chatbot, hash_id_state], [msg, chatbot])
                submit_box.click(respond, [msg, chatbot, hash_id_state], [msg, chatbot])
        demo.title = "Kanuna Sor"
        demo.launch(server_port=self.cfg.webui_port, server_name=self.cfg.webui_host, show_api=False)