markrodrigo
startup
09893ec
Raw
History Blame
4.86 kB
from transformers import pipeline
import gradio as gr
import spaces
# Hidden state for the counter
counter_state = gr.State(0)
pipe = pipeline("text-generation", model="markrodrigo/Llama-3.2-3B-Instruct-Spatial-SQL-1.1", device_map="auto")
# The Alpaca instruction prompt format
ALPACA_TEMPLATE = """<|begin_of_text|><|start_header_id|>system<|end_header_id|> You are a helpful assistant. You are an expert at PostGIS and Postgresql and SQL and psql. <|eot_id|><|start_header_id|>user<|end_header_id|>
### Instruction: Write a PostGIS SQL statement for the following.
{instruction}
### Input:
{input}
### Response:
<|eot_id|><|start_header_id|>assistant<|end_header_id|>
"""
# Define your list of pre-set example prompts
PRESET_EXAMPLES = [
"What is the area for the polygon? : 'Polygon ((-3.7515154 40.3855551, -3.7514972 40.3856581, -3.7507005 40.3855767, -3.7507167 40.3854722, -3.7515154 40.3855551))'",
"What is the centroid for the polygon? : 'Polygon ((-3.6934636 40.4808785, -3.6933352 40.4811486, -3.6930125 40.4810598, -3.693141 40.4807897, -3.6934636 40.4808785))'",
"What is the thousand meter buffer for the following point? : 'Point(-8.7522658 41.3862664)'",
"How long is the line? : 'LINESTRING (-3.6976693 40.4263178, -3.6986082 40.4258729)'",
"How far apart is the point and line? : 'Point(-109.87549823 38.60574249)' 'LineString(-109.24324628 38.76349931, -109.4821773 38.6875815)'"
]
# Custom CSS targeting the button by its ID
custom_css = """
#special-button {
background-color: #0052FF !important; /* Your desired color */
color: white !important;
border: none !important;
font-weight: 600 !important;
padding: 12px 24px !important;
}
#special-button:hover {
background-color: #CAD3DE !important;
transform: scale(1.03);
transition: all 0.2s ease;
}
#clear-chat-btn {
width: 200px !important;
min-width: 200px !important;
max-width: 200px !important;
height: 42px !important;
background-color: #FF5733 !important; /* Main hex color */
color: #FFFFFF !important; /* White text */
border: none !important;
font-size: 14px !important;
font-weight: 600 !important;
padding: 8px 16px !important;
border-radius: 8px !important; /* Optional rounded look */
}
#clear-chat-btn:hover {
background-color: #C70039 !important; /* Hover hex color (darker) */
transform: translateY(-1px);
box-shadow: 0 4px 12px rgba(199, 0, 57, 0.3) !important;
}
"""
@spaces.GPU
def respond(user_message, chat_history, counter):
# 1. Increment counter
counter += 1
chat_history = chat_history or []
if not user_message or not user_message.strip():
return chat_history, ""
# Modern format for Gradio 5/6
chat_history.append({"role": "user", "content": user_message})
chat_history.append({"role": "assistant", "content": None})
prompt = ALPACA_TEMPLATE.format(instruction=user_message, input="")
sequences = pipe(
prompt,
max_new_tokens=256,
return_full_text=False,
temperature=0.4,
top_k=100,
do_sample=True,
)
bot_response = sequences[0]["generated_text"].strip()
chat_history[-1]["content"] = bot_response
return chat_history, "", counter
with gr.Blocks(title="Text to PostGIS Postgresql via Llama 3.2", css=custom_css) as demo:
gr.Markdown("# Natural Language to Spatial SQL.\n### Convert natural language and spatial primitives to PostGIS with Llama 3.2")
chatbot = gr.Chatbot(
label="Chat",
height=400,
# type="messages",
# show_copy_button=True,
)
with gr.Row():
with gr.Column(scale=5):
msg = gr.Textbox(
placeholder="Natural Language : WKT format",
lines=2,
container=False
)
with gr.Column(scale=1, min_width=100):
submit_btn = gr.Button("Submit", variant="primary", elem_id="special-button" ) # gr.themes.Ocean()
gr.Markdown("### Quick Examples")
gr.Examples(
examples=PRESET_EXAMPLES,
inputs=msg,
label="Click an example → then click Submit"
)
counter_display = gr.Textbox(label="Request Counter", value=0, interactive=False)
submit_btn.click(fn=respond, inputs=[msg, chatbot, counter_state], outputs=[chatbot, msg, counter_state, counter_display])
msg.submit(fn=respond, inputs=[msg, chatbot, counter_state], outputs=[chatbot, msg, counter_state, counter_display])
clear_btn = gr.Button("Clear Chat", elem_id="clear-chat-btn")
clear_btn.click(lambda: ([], ""), outputs=[chatbot, msg])
gr.Markdown("## 🚀 Space Request Tracker")
# Display counter (optional)
if __name__ == "__main__":
print("Gradio version:", gr.__version__)
demo.launch(css=custom_css)