Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from llama_cpp import Llama
|
| 2 |
+
from typing import Optional, Dict, Union
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
model_path = hf_hub_download(
|
| 10 |
+
repo_id="omeryentur/phi-3-sql",
|
| 11 |
+
filename="phi-3-sql.Q4_K_M.gguf",
|
| 12 |
+
use_auth_token=True
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
llm = Llama(
|
| 16 |
+
model_path=model_path,
|
| 17 |
+
n_ctx=512,
|
| 18 |
+
n_threads=1,
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
def generate_sql_query(text_input_schema:str,text_input_question: str):
|
| 22 |
+
global pattern_counter
|
| 23 |
+
|
| 24 |
+
if not text:
|
| 25 |
+
return {"error": "not found text"}, None, None
|
| 26 |
+
try:
|
| 27 |
+
prompt = f"""<|system|>
|
| 28 |
+
{table_info}
|
| 29 |
+
|
| 30 |
+
<|user|>
|
| 31 |
+
{question}
|
| 32 |
+
|
| 33 |
+
<|sql|>"""
|
| 34 |
+
|
| 35 |
+
completion = llm(
|
| 36 |
+
prompt,
|
| 37 |
+
max_tokens=512,
|
| 38 |
+
temperature=0,
|
| 39 |
+
stop=["<end_of_turn>"]
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
generated_pattern = completion['choices'][0]['text'].strip()
|
| 43 |
+
result = parse_log_with_grok(text, generated_pattern)
|
| 44 |
+
time.sleep(0.5)
|
| 45 |
+
|
| 46 |
+
return result
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return {"error": e}
|
| 50 |
+
|
| 51 |
+
with gr.Blocks() as demo:
|
| 52 |
+
gr.Markdown("# Sql Query")
|
| 53 |
+
|
| 54 |
+
with gr.Row():
|
| 55 |
+
with gr.Column():
|
| 56 |
+
text_input_schema = gr.Textbox(label="Schema")
|
| 57 |
+
text_input_question = gr.Textbox(label="question")
|
| 58 |
+
generate_btn = gr.Button("Create Sql Query")
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
with gr.Column():
|
| 62 |
+
output = gr.JSON(label="Sql Query:")
|
| 63 |
+
|
| 64 |
+
generate_btn.click(
|
| 65 |
+
fn=generate_sql_query,
|
| 66 |
+
inputs=[text_input_schema,text_input_question],
|
| 67 |
+
outputs=[pattern_output]
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
demo.launch()
|