Spaces:
Sleeping
Sleeping
Commit ·
6487c9c
1
Parent(s): cf19f26
app
Browse files- .streamlit/config.toml +5 -0
- app.py +153 -0
- meta-llama-3.webp +0 -0
- packages.txt +7 -0
- requirements.txt +4 -0
.streamlit/config.toml
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[theme]
|
| 2 |
+
base="light"
|
| 3 |
+
primaryColor="#4baaff"
|
| 4 |
+
secondaryBackgroundColor="#c2dbf3"
|
| 5 |
+
textColor="#000000"
|
app.py
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from llama_cpp import Llama
|
| 4 |
+
import llama_cpp
|
| 5 |
+
import time
|
| 6 |
+
st.set_page_config(layout = "wide")
|
| 7 |
+
import streamlit.components.v1 as components
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
col1, mid, col2 = st.columns([4,1,40])
|
| 11 |
+
with col1:
|
| 12 |
+
st.image("meta-llama-3.webp", width=150)
|
| 13 |
+
with col2:
|
| 14 |
+
st.markdown('''# Llama :blue[3.2] ''')
|
| 15 |
+
st.markdown('''##### :gray[finetuned for SQL questions]''')
|
| 16 |
+
llm = Llama.from_pretrained(repo_id="alessandroptsn/llama3_2sql_model",filename="*llama3_2sql_model.Q4_K_M.gguf",verbose=False)
|
| 17 |
+
|
| 18 |
+
st.session_state.context_text ="""
|
| 19 |
+
CREATE TABLE student (
|
| 20 |
+
student_id INT PRIMARY KEY,
|
| 21 |
+
name TEXT,
|
| 22 |
+
age INT
|
| 23 |
+
);
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
INSERT INTO student (student_id, name, age)
|
| 27 |
+
VALUES
|
| 28 |
+
(1, 'Carlos Silva', 20),
|
| 29 |
+
(2, 'Ana Oliveira', 22),
|
| 30 |
+
(3, 'João Pereira', 19);
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
CREATE TABLE subject (
|
| 34 |
+
subject_id INT PRIMARY KEY,
|
| 35 |
+
name TEXT,
|
| 36 |
+
hours INT
|
| 37 |
+
);
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
INSERT INTO subject (subject_id, name, hours)
|
| 41 |
+
VALUES
|
| 42 |
+
(1, 'Mathematics', 60),
|
| 43 |
+
(2, 'History', 40),
|
| 44 |
+
(3, 'Biology', 50);
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
CREATE TABLE grades (
|
| 48 |
+
student_id INT,
|
| 49 |
+
subject_id INT,
|
| 50 |
+
grade REAL,
|
| 51 |
+
PRIMARY KEY (student_id, subject_id),
|
| 52 |
+
FOREIGN KEY (student_id) REFERENCES student(student_id)
|
| 53 |
+
);
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
INSERT INTO grades (student_id, subject_id, grade)
|
| 57 |
+
VALUES
|
| 58 |
+
(1, 1, 8.5),
|
| 59 |
+
(1, 2, 7.0),
|
| 60 |
+
(2, 1, 9.0),
|
| 61 |
+
(2, 3, 6.5),
|
| 62 |
+
(3, 2, 7.5);
|
| 63 |
+
"""
|
| 64 |
+
|
| 65 |
+
def on_upper_clicked():
|
| 66 |
+
st.session_state.text = st.session_state.context_text
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
query = st.text_input(label="Insert your question here")
|
| 70 |
+
|
| 71 |
+
col1, col2 = st.columns([1, 3])
|
| 72 |
+
|
| 73 |
+
with col1:
|
| 74 |
+
context_check = st.checkbox("Add context")
|
| 75 |
+
if context_check:
|
| 76 |
+
concatenated_context = st.session_state.context_text
|
| 77 |
+
else:
|
| 78 |
+
concatenated_context = ''
|
| 79 |
+
|
| 80 |
+
if context_check:
|
| 81 |
+
st.text_area("Press Ctrl+Enter to apply the context",st.session_state.context_text, key="text",height=700)
|
| 82 |
+
st.button("Reset context", on_click=on_upper_clicked)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
with col2:
|
| 86 |
+
if st.button("Answer"):
|
| 87 |
+
start = time.time()
|
| 88 |
+
st.session_state.resp = llm.create_chat_completion(
|
| 89 |
+
messages=[
|
| 90 |
+
{
|
| 91 |
+
"role": "system",
|
| 92 |
+
"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."
|
| 93 |
+
},
|
| 94 |
+
{
|
| 95 |
+
"role" : "template",
|
| 96 |
+
"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.
|
| 97 |
+
### Instruction:
|
| 98 |
+
Company database: """+concatenated_context+"""
|
| 99 |
+
|
| 100 |
+
### Input:
|
| 101 |
+
SQL Prompt: """+query+"""
|
| 102 |
+
|
| 103 |
+
### Response:
|
| 104 |
+
SQL: {}
|
| 105 |
+
|
| 106 |
+
Explanation: {}
|
| 107 |
+
"""
|
| 108 |
+
}
|
| 109 |
+
]
|
| 110 |
+
)
|
| 111 |
+
end = time.time()
|
| 112 |
+
st.session_state.total_time = end - start
|
| 113 |
+
|
| 114 |
+
if 'resp' in st.session_state:
|
| 115 |
+
st.markdown(st.session_state.resp["choices"][0]["message"]['content'])
|
| 116 |
+
st.write(f"Execution time: {st.session_state.total_time:.2f} seconds")
|
| 117 |
+
if context_check:
|
| 118 |
+
st.write("Do you want to validate the query? run it [here](%s)" % 'https://onecompiler.com/postgresql/')
|
| 119 |
+
|
| 120 |
+
if context_check:
|
| 121 |
+
st.text("Diagram of the example context provided :")
|
| 122 |
+
components.iframe("https://dbdiagram.io/e/6772f5965406798ef7f30310/6772f59f5406798ef7f303e4", height=500)
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
footer="""<style>
|
| 127 |
+
a:link , a:visited{
|
| 128 |
+
color: blue;
|
| 129 |
+
background-color: transparent;
|
| 130 |
+
text-decoration: underline;
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
a:hover, a:active {
|
| 134 |
+
color: red;
|
| 135 |
+
background-color: transparent;
|
| 136 |
+
text-decoration: underline;
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
.footer {
|
| 140 |
+
position: fixed;
|
| 141 |
+
left: 0;
|
| 142 |
+
bottom: 0;
|
| 143 |
+
width: 100%;
|
| 144 |
+
background-color: white;
|
| 145 |
+
color: black;
|
| 146 |
+
text-align: center;
|
| 147 |
+
}
|
| 148 |
+
</style>
|
| 149 |
+
<div class="footer">
|
| 150 |
+
<p>Developed by - <a href="https://github.com/AlessandroPTSN">AlessandroPTSN</a></p>
|
| 151 |
+
</div>
|
| 152 |
+
"""
|
| 153 |
+
st.markdown(footer,unsafe_allow_html=True)
|
meta-llama-3.webp
ADDED
|
packages.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ninja-build
|
| 2 |
+
build-essential
|
| 3 |
+
pkg-config
|
| 4 |
+
libpthread-stubs0-dev
|
| 5 |
+
gcc
|
| 6 |
+
g++
|
| 7 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy==2.2.1
|
| 2 |
+
streamlit==1.41.1
|
| 3 |
+
llama_cpp_python
|
| 4 |
+
huggingface-hub
|