text2sql / app.py
omeryentur's picture
Update app.py
8094a28 verified
raw
history blame
1.59 kB
from llama_cpp import Llama
from typing import Optional, Dict, Union
from huggingface_hub import hf_hub_download
import gradio as gr
import time
model_path = hf_hub_download(
repo_id="omeryentur/phi-3-sql",
filename="phi-3-sql.Q4_K_M.gguf",
use_auth_token=True
)
llm = Llama(
model_path=model_path,
n_ctx=512,
n_threads=1,
)
def generate_sql_query(text_input_schema:str,text_input_question: str):
global pattern_counter
try:
prompt = f"""<|system|>
{table_info}
<|user|>
{question}
<|sql|>"""
completion = llm(
prompt,
max_tokens=512,
temperature=0,
stop=["<end_of_turn>"]
)
generated_pattern = completion['choices'][0]['text'].strip()
result = parse_log_with_grok(text, generated_pattern)
time.sleep(0.5)
return result
except Exception as e:
return {"error": e}
with gr.Blocks() as demo:
gr.Markdown("# Sql Query")
with gr.Row():
with gr.Column():
text_input_schema = gr.TextArea(label="Schema")
text_input_question = gr.Textbox(label="question")
generate_btn = gr.Button("Create Sql Query")
with gr.Row():
with gr.Column():
output = gr.JSON(label="Sql Query:")
generate_btn.click(
fn=generate_sql_query,
inputs=[text_input_schema,text_input_question],
outputs=[output]
)
if __name__ == "__main__":
demo.launch()