Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import gradio as gr
|
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
from httpx import Client
|
| 6 |
from huggingface_hub import HfApi
|
| 7 |
-
#from llama_cpp import Llama
|
| 8 |
import pandas as pd
|
| 9 |
from transformers import pipeline
|
| 10 |
import spaces
|
|
@@ -14,32 +13,14 @@ load_dotenv()
|
|
| 14 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 15 |
assert HF_TOKEN is not None, "You need to set HF_TOKEN in your environment variables"
|
| 16 |
|
| 17 |
-
|
| 18 |
BASE_DATASETS_SERVER_URL = "https://datasets-server.huggingface.co"
|
| 19 |
-
API_URL = "https://m82etjwvhoptr3t5.us-east-1.aws.endpoints.huggingface.cloud"
|
| 20 |
-
headers = {
|
| 21 |
-
"Accept" : "application/json",
|
| 22 |
-
"Authorization": f"Bearer {HF_TOKEN}",
|
| 23 |
-
"Content-Type": "application/json"
|
| 24 |
-
}
|
| 25 |
-
|
| 26 |
client = Client(headers=headers)
|
| 27 |
api = HfApi(token=HF_TOKEN)
|
| 28 |
-
|
| 29 |
-
# First approach: Use llama.cpp
|
| 30 |
-
#llama = Llama(model_path="DuckDB-NSQL-7B-v0.1-q8_0.gguf", n_ctx=2048)
|
| 31 |
-
#def query_local_model(text):
|
| 32 |
-
# pred = llama(text, temperature=0.1, max_tokens=500)
|
| 33 |
-
# return pred["choices"][0]["text"]
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
# Second approach: Use transformers -> Took too much time
|
| 37 |
pipe = pipeline("text-generation", model="motherduckdb/DuckDB-NSQL-7B-v0.1", device=1)
|
| 38 |
-
# pipe.to('cuda')
|
| 39 |
|
| 40 |
@spaces.GPU
|
| 41 |
-
def
|
| 42 |
-
pred = pipe(
|
| 43 |
return pred[0]["generated_text"]
|
| 44 |
|
| 45 |
|
|
@@ -48,16 +29,6 @@ def get_first_parquet(dataset: str):
|
|
| 48 |
return resp.json()["parquet_files"][0]
|
| 49 |
|
| 50 |
|
| 51 |
-
def query_remote_model(text):
|
| 52 |
-
payload = {
|
| 53 |
-
"inputs": text,
|
| 54 |
-
"parameters": {}
|
| 55 |
-
}
|
| 56 |
-
response = client.post(API_URL, headers=headers, json=payload)
|
| 57 |
-
pred = response.json()
|
| 58 |
-
return pred[0]["generated_text"]
|
| 59 |
-
|
| 60 |
-
|
| 61 |
def text2sql(dataset_name, query_input):
|
| 62 |
print(f"start text2sql for {dataset_name}")
|
| 63 |
try:
|
|
@@ -92,8 +63,7 @@ def text2sql(dataset_name, query_input):
|
|
| 92 |
### Response (use duckdb shorthand if possible):
|
| 93 |
"""
|
| 94 |
try:
|
| 95 |
-
|
| 96 |
-
sql_output = query_local_model_transformers(text)
|
| 97 |
except Exception as error:
|
| 98 |
return {
|
| 99 |
schema_output: ddl_create,
|
|
@@ -123,7 +93,7 @@ with gr.Blocks() as demo:
|
|
| 123 |
gr.Markdown("This space showcase how to generate a SQL query from a text and get the result.")
|
| 124 |
gr.Markdown("Tech stack: duckdb and DuckDB-NSQL-7B model")
|
| 125 |
dataset_name = gr.Textbox("jamescalam/world-cities-geo", label="Dataset Name")
|
| 126 |
-
query_input = gr.Textbox("Cities from Albania country
|
| 127 |
examples = [
|
| 128 |
["Cities from Albania country"],
|
| 129 |
["The continent with the most number of countries"],
|
|
|
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
from httpx import Client
|
| 6 |
from huggingface_hub import HfApi
|
|
|
|
| 7 |
import pandas as pd
|
| 8 |
from transformers import pipeline
|
| 9 |
import spaces
|
|
|
|
| 13 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 14 |
assert HF_TOKEN is not None, "You need to set HF_TOKEN in your environment variables"
|
| 15 |
|
|
|
|
| 16 |
BASE_DATASETS_SERVER_URL = "https://datasets-server.huggingface.co"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
client = Client(headers=headers)
|
| 18 |
api = HfApi(token=HF_TOKEN)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
pipe = pipeline("text-generation", model="motherduckdb/DuckDB-NSQL-7B-v0.1", device=1)
|
|
|
|
| 20 |
|
| 21 |
@spaces.GPU
|
| 22 |
+
def generate_sql(prompt):
|
| 23 |
+
pred = pipe(prompt, max_length=1000)
|
| 24 |
return pred[0]["generated_text"]
|
| 25 |
|
| 26 |
|
|
|
|
| 29 |
return resp.json()["parquet_files"][0]
|
| 30 |
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
def text2sql(dataset_name, query_input):
|
| 33 |
print(f"start text2sql for {dataset_name}")
|
| 34 |
try:
|
|
|
|
| 63 |
### Response (use duckdb shorthand if possible):
|
| 64 |
"""
|
| 65 |
try:
|
| 66 |
+
sql_output = generate_sql(text)
|
|
|
|
| 67 |
except Exception as error:
|
| 68 |
return {
|
| 69 |
schema_output: ddl_create,
|
|
|
|
| 93 |
gr.Markdown("This space showcase how to generate a SQL query from a text and get the result.")
|
| 94 |
gr.Markdown("Tech stack: duckdb and DuckDB-NSQL-7B model")
|
| 95 |
dataset_name = gr.Textbox("jamescalam/world-cities-geo", label="Dataset Name")
|
| 96 |
+
query_input = gr.Textbox("Cities from Albania country", label="Ask something about your data")
|
| 97 |
examples = [
|
| 98 |
["Cities from Albania country"],
|
| 99 |
["The continent with the most number of countries"],
|