File size: 4,541 Bytes
6487c9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5620411
6487c9c
 
 
 
5620411
6487c9c
 
 
 
5620411
6487c9c
 
 
 
 
 
5620411
 
 
6487c9c
 
 
 
5620411
6487c9c
 
 
 
 
 
 
5620411
6487c9c
5620411
6487c9c
5620411
 
6487c9c
 
 
5620411
6487c9c
5620411
 
 
 
 
 
 
6487c9c
 
5620411
6487c9c
 
5620411
 
6487c9c
 
 
 
 
 
 
5620411
6487c9c
 
5620411
 
 
 
 
 
 
 
 
 
6487c9c
 
 
 
 
5620411
 
 
 
 
 
 
 
6487c9c
 
 
 
 
 
 
 
 
 
5620411
6487c9c
 
 
 
 
 
 
 
 
 
5620411
6487c9c
 
 
 
 
 
 
 
 
 
 
 
e5e9dce
6487c9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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

import streamlit as st
from llama_cpp import Llama
import llama_cpp
import time
st.set_page_config(layout = "wide")
import streamlit.components.v1 as components


col1, mid, col2 = st.columns([4,1,40])
with col1:
    st.image("meta-llama-3.webp", width=150)
with col2:
    st.markdown('''# Llama :blue[3.2] ''')
    st.markdown('''##### :gray[finetuned for SQL questions]''')
llm = Llama.from_pretrained(repo_id="alessandroptsn/llama3_2sql_model_long",filename="*llama3_2sql_model_long.Q4_K_M.gguf",verbose=False,n_ctx=100000)

st.session_state.context_text ="""
CREATE TABLE student (
    student_id INT PRIMARY KEY, 
    student_name TEXT, 
    age INT
);


INSERT INTO student (student_id, student_name, age)
VALUES 
(1, 'Carlos Silva', 20), 
(2, 'Ana Oliveira', 22), 
(3, 'João Pereira', 19);


CREATE TABLE topic (
    topic_id INT PRIMARY KEY, 
    topic_name TEXT, 
    hours INT
);


INSERT INTO topic (topic_id, topic_name, hours)
VALUES 
(1, 'Mathematics', 60), 
(2, 'History', 40), 
(3, 'Biology', 50);


CREATE TABLE grades (
    grades_id INT PRIMARY KEY, 
    student_id INT, 
    topic_id INT, 
    grade REAL, 
    FOREIGN KEY (student_id) REFERENCES student(student_id),
    FOREIGN KEY (topic_id) REFERENCES topic(topic_id)
);


INSERT INTO grades (grades_id,student_id, topic_id, grade)
VALUES 
(1,1, 1, 8.5), 
(2,1, 2, 7.0), 
(3,2, 1, 9.0), 
(4,2, 3, 6.5), 
(5,3, 2, 7.5),
(6,3, 3, 7.0);

"""

def context_text_clicked():
    st.session_state.text = st.session_state.context_text

def text_clicked2():
    st.session_state.text = st.session_state.text

query = st.text_input(label="Insert your question here")

col1, col2 = st.columns([1, 3])

with col1:
    context_check = st.checkbox("Add context")


    if context_check:
        test = st.session_state.context_text
        st.session_state.test2 = test
        st.text_area("For any change, click in 'Apply context'",test, key="text",height=700)
        b1, b2 = st.columns(2)
        with b1:
            if st.button("Reset context", on_click=context_text_clicked):
                st.session_state.concatenated_context = st.session_state.context_text
        with b2:
            if st.button("Apply context", on_click=text_clicked2):
                st.session_state.concatenated_context = st.session_state.text


with col2:
    if st.button("Answer"):
        start = time.time()
        try: 
            final_context = st.session_state.concatenated_context
        except:
            try:
                final_context = st.session_state.test2
            except:
                final_context = ''

        st.session_state.resp = llm.create_chat_completion(
        messages=[
            {
                "role": "system",
                "content": "You are an SQL generator that takes a users query and gives them helpful SQL to use. Answer in SQL querys for the user."
            },
            {
              "role"  : "template",
              "content":"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
              ### Instruction:
              Company database: """+final_context+"""
    
              ### Input:
              SQL Prompt: """+query+"""
    
              ### Response:
              SQL: {}
    
              Explanation: {}
              """
            }
        ]#,temperature=0.85
        )
        end = time.time()
        st.session_state.total_time = end - start

        if 'resp' in st.session_state:
            st.markdown(st.session_state.resp["choices"][0]["message"]['content'])
            st.write(f"Execution time: {st.session_state.total_time:.2f} seconds")
        if context_check:
            st.write("Do you want to validate the query? run it [here](%s)" % 'https://onecompiler.com/postgresql/')

if context_check:
    st.text("Diagram of the example context provided :")
    components.iframe("https://dbdiagram.io/e/6779dbd45406798ef74a4a61/6779dbf35406798ef74a4bbe", height=500)



footer="""<style>
a:link , a:visited{
color: blue;
background-color: transparent;
text-decoration: underline;
}

a:hover,  a:active {
color: red;
background-color: transparent;
text-decoration: underline;
}

.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: white;
color: black;
text-align: center;
}
</style>
<div class="footer">
  <p>Developed by - <a href="https://github.com/AlessandroPTSN">AlessandroPTSN</a></p>
</div>
"""
st.markdown(footer,unsafe_allow_html=True)