Commit
Β·
1e3f569
1
Parent(s):
7a8a034
Use my hosted API
Browse files- README.md +1 -1
- app-kobold.py +105 -0
README.md
CHANGED
@@ -5,7 +5,7 @@ colorFrom: gray
|
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.37.0
|
8 |
-
app_file: app.py
|
9 |
pinned: true
|
10 |
duplicated_from: richardr1126/natsql-wizardcoder-demo
|
11 |
license: bigcode-openrail-m
|
|
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.37.0
|
8 |
+
app_file: app-kobold.py
|
9 |
pinned: true
|
10 |
duplicated_from: richardr1126/natsql-wizardcoder-demo
|
11 |
license: bigcode-openrail-m
|
app-kobold.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import sqlparse
|
4 |
+
import requests
|
5 |
+
from time import sleep
|
6 |
+
|
7 |
+
def format(text):
|
8 |
+
# Split the text by "|", and get the last element in the list which should be the final query
|
9 |
+
try:
|
10 |
+
final_query = text.split("|")[1].strip()
|
11 |
+
except Exception:
|
12 |
+
final_query = text
|
13 |
+
|
14 |
+
try:
|
15 |
+
# Attempt to format SQL query using sqlparse
|
16 |
+
formatted_query = sqlparse.format(final_query, reindent=True, keyword_case='upper')
|
17 |
+
except Exception:
|
18 |
+
# If formatting fails, use the original, unformatted query
|
19 |
+
formatted_query = final_query
|
20 |
+
|
21 |
+
# Convert SQL to markdown (not required, but just to show how to use the markdown module)
|
22 |
+
final_query_markdown = f"{formatted_query}"
|
23 |
+
|
24 |
+
return final_query_markdown
|
25 |
+
|
26 |
+
|
27 |
+
def bot(input_message: str, db_info="", temperature=0.1, top_p=0.9, top_k=0, repetition_penalty=1.08):
|
28 |
+
# Format the user's input message
|
29 |
+
messages = f"Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n\n### Instruction:\n\nConvert text to sql: {input_message} {db_info}\n\n### Response:\n\n"
|
30 |
+
|
31 |
+
url = "https://https://e9f4be879d38-8269039109365193683.ngrok-free.app/api/v1/generate"
|
32 |
+
payload = {
|
33 |
+
"prompt": messages,
|
34 |
+
"temperature": temperature,
|
35 |
+
"top_p": top_p,
|
36 |
+
"top_k": top_k,
|
37 |
+
"top_a": 0,
|
38 |
+
"n": 1,
|
39 |
+
"max_context_length": 2048,
|
40 |
+
"max_length": 512,
|
41 |
+
"rep_pen": repetition_penalty,
|
42 |
+
"sampler_order": [6,0,1,3,4,2,5],
|
43 |
+
"stop_sequence": ["###", "Result"],
|
44 |
+
}
|
45 |
+
headers = {"Content-Type": "application/json"}
|
46 |
+
|
47 |
+
for _ in range(3):
|
48 |
+
try:
|
49 |
+
response = requests.post(url, json=payload, headers=headers)
|
50 |
+
response_text = response.json()["results"][0]["text"]
|
51 |
+
response_text = response_text.replace("\n", "").replace("\t", " ")
|
52 |
+
if response_text and response_text[-1] == ".":
|
53 |
+
response_text = response_text[:-1]
|
54 |
+
|
55 |
+
return format(response_text)
|
56 |
+
|
57 |
+
except Exception as e:
|
58 |
+
print(f'Error occurred: {str(e)}')
|
59 |
+
print('Waiting for 10 seconds before retrying...')
|
60 |
+
sleep(10)
|
61 |
+
|
62 |
+
with gr.Blocks(theme='gradio/soft') as demo:
|
63 |
+
header = gr.HTML("""
|
64 |
+
<h1 style="text-align: center">SQL Skeleton WizardCoder Demo</h1>
|
65 |
+
<h3 style="text-align: center">π§ββοΈ Generate SQL queries from Natural Language π§ββοΈ</h3>
|
66 |
+
""")
|
67 |
+
|
68 |
+
output_box = gr.Code(label="Generated SQL", lines=2, interactive=True)
|
69 |
+
input_text = gr.Textbox(lines=3, placeholder='Write your question here...', label='NL Input')
|
70 |
+
db_info = gr.Textbox(lines=4, placeholder='Example: | table_01 : column_01 , column_02 | table_02 : column_01 , column_02 | ...', label='Database Info')
|
71 |
+
|
72 |
+
with gr.Accordion("Hyperparameters", open=False):
|
73 |
+
temperature = gr.Slider(label="Temperature", minimum=0.0, maximum=1.0, value=0.5, step=0.1)
|
74 |
+
top_p = gr.Slider(label="Top-p (nucleus sampling)", minimum=0.0, maximum=1.0, value=0.9, step=0.01)
|
75 |
+
top_k = gr.Slider(label="Top-k", minimum=0, maximum=200, value=0, step=1)
|
76 |
+
repetition_penalty = gr.Slider(label="Repetition Penalty", minimum=1.0, maximum=2.0, value=1.08, step=0.01)
|
77 |
+
|
78 |
+
run_button = gr.Button("Generate SQL", variant="primary")
|
79 |
+
|
80 |
+
with gr.Accordion("Examples", open=True):
|
81 |
+
examples = gr.Examples([
|
82 |
+
["What is the average, minimum, and maximum age for all French singers?", "| stadium : stadium_id , location , name , capacity , highest , lowest , average | singer : singer_id , name , country , song_name , song_release_year , age , is_male | concert : concert_id , concert_name , theme , stadium_id , year | singer_in_concert : concert_id , singer_id | concert.stadium_id = stadium.stadium_id | singer_in_concert.singer_id = singer.singer_id | singer_in_concert.concert_id = concert.concert_id |"],
|
83 |
+
["Show location and name for all stadiums with a capacity between 5000 and 10000.", "| stadium : stadium_id , location , name , capacity , highest , lowest , average | singer : singer_id , name , country , song_name , song_release_year , age , is_male | concert : concert_id , concert_name , theme , stadium_id , year | singer_in_concert : concert_id , singer_id | concert.stadium_id = stadium.stadium_id | singer_in_concert.singer_id = singer.singer_id | singer_in_concert.concert_id = concert.concert_id |"],
|
84 |
+
["What are the number of concerts that occurred in the stadium with the largest capacity ?", "| stadium : stadium_id , location , name , capacity , highest , lowest , average | singer : singer_id , name , country , song_name , song_release_year , age , is_male | concert : concert_id , concert_name , theme , stadium_id , year | singer_in_concert : concert_id , singer_id | concert.stadium_id = stadium.stadium_id | singer_in_concert.singer_id = singer.singer_id | singer_in_concert.concert_id = concert.concert_id |"],
|
85 |
+
["How many male singers performed in concerts in the year 2023?", "| stadium : stadium_id , location , name , capacity , highest , lowest , average | singer : singer_id , name , country , song_name , song_release_year , age , is_male | concert : concert_id , concert_name , theme , stadium_id , year | singer_in_concert : concert_id , singer_id | concert.stadium_id = stadium.stadium_id | singer_in_concert.singer_id = singer.singer_id | singer_in_concert.concert_id = concert.concert_id |"],
|
86 |
+
["List the names of all singers who performed in a concert with the theme 'Rock'", "| stadium : stadium_id , location , name , capacity , highest , lowest , average | singer : singer_id , name , country , song_name , song_release_year , age , is_male | concert : concert_id , concert_name , theme , stadium_id , year | singer_in_concert : concert_id , singer_id | concert.stadium_id = stadium.stadium_id | singer_in_concert.singer_id = singer.singer_id | singer_in_concert.concert_id = concert.concert_id |"]
|
87 |
+
], inputs=[input_text, db_info, temperature, top_p, top_k, repetition_penalty], fn=bot)
|
88 |
+
|
89 |
+
quantized_model = "richardr1126/spider-skeleton-wizard-coder-ggml"
|
90 |
+
merged_model = "richardr1126/spider-skeleton-wizard-coder-merged"
|
91 |
+
initial_model = "WizardLM/WizardCoder-15B-V1.0"
|
92 |
+
lora_model = "richardr1126/spider-skeleton-wizard-coder-qlora"
|
93 |
+
dataset = "richardr1126/spider-skeleton-context-instruct"
|
94 |
+
|
95 |
+
footer = gr.HTML(f"""
|
96 |
+
<p>π οΈ If you want you can <strong>duplicate this Space</strong>, then change the HF_MODEL_REPO spaces env varaible to use any GGML model.</p>
|
97 |
+
<p>π Leveraging the <a href='https://huggingface.co/{quantized_model}'><strong>4-bit GGML version</strong></a> of <a href='https://huggingface.co/{merged_model}'><strong>{merged_model}</strong></a> model.</p>
|
98 |
+
<p>π How it's made: <a href='https://huggingface.co/{initial_model}'><strong>{initial_model}</strong></a> was finetuned to create <a href='https://huggingface.co/{lora_model}'><strong>{lora_model}</strong></a>, then merged together to create <a href='https://huggingface.co/{merged_model}'><strong>{merged_model}</strong></a>.</p>
|
99 |
+
<p>π Fine-tuning was performed using QLoRA techniques on the <a href='https://huggingface.co/datasets/{dataset}'><strong>{dataset}</strong></a> dataset. You can view training metrics on the <a href='https://huggingface.co/{lora_model}'><strong>QLoRa adapter HF Repo</strong></a>.</p>
|
100 |
+
""")
|
101 |
+
|
102 |
+
|
103 |
+
run_button.click(fn=bot, inputs=[input_text, db_info, temperature, top_p, top_k, repetition_penalty], outputs=output_box, api_name="txt2sql")
|
104 |
+
|
105 |
+
demo.queue(concurrency_count=1, max_size=10).launch(debug=True)
|