markrodrigo commited on
Commit
7ca3449
·
1 Parent(s): 3725354
Files changed (2) hide show
  1. app.py +210 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import gradio as gr
3
+ import spaces
4
+
5
+ # Initialize the pipeline with an Alpaca-tuned model
6
+ pipe = pipeline("text-generation", model="markrodrigo/Llama-3.2-3B-Instruct-Spatial-SQL-1.1", device_map="auto")
7
+
8
+ # The Alpaca instruction prompt format
9
+ 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|>
10
+
11
+ ### Instruction: Write a PostGIS SQL statement for the following.
12
+ {instruction}
13
+
14
+ ### Input:
15
+ {input}
16
+
17
+ ### Response:
18
+ <|eot_id|><|start_header_id|>assistant<|end_header_id|>
19
+ """
20
+
21
+ # Define your list of pre-set example prompts
22
+ PRESET_EXAMPLES = [
23
+ "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))'",
24
+ "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))'",
25
+ "What is the thousand meter buffer for the following point? : 'Point(-8.7522658 41.3862664)'",
26
+ "How long is the line? : 'LINESTRING (-3.6976693 40.4263178, -3.6986082 40.4258729)'",
27
+ "How far apart is the point and line? : 'Point(-109.87549823 38.60574249)' 'LineString(-109.24324628 38.76349931, -109.4821773 38.6875815)'"
28
+ ]
29
+
30
+ @spaces.GPU
31
+ def respond(user_message, chat_history):
32
+ chat_history = chat_history or []
33
+
34
+ if not user_message or not user_message.strip():
35
+ return chat_history, ""
36
+
37
+ # ✅ Modern messages format (required in Gradio 5/6)
38
+ chat_history.append({"role": "user", "content": user_message})
39
+ chat_history.append({"role": "assistant", "content": None})
40
+
41
+ prompt = ALPACA_TEMPLATE.format(instruction=user_message, input="")
42
+
43
+ sequences = pipe(
44
+ prompt,
45
+ max_new_tokens=256,
46
+ return_full_text=False,
47
+ temperature=0.4,
48
+ top_k=100,
49
+ do_sample=True,
50
+ )
51
+
52
+ bot_response = sequences[0]["generated_text"].strip()
53
+ chat_history[-1]["content"] = bot_response
54
+
55
+ return chat_history, ""
56
+
57
+
58
+ # ====================== UI ======================
59
+ with gr.Blocks(title="Text to SQL via Llama 3.2") as demo:
60
+ gr.Markdown("# Text to SQL via Llama 3.2\n### Convert natural language to PostgreSQL queries")
61
+
62
+ chatbot = gr.Chatbot(
63
+ label="Chat",
64
+ height=500,
65
+ type="messages",
66
+ show_copy_button=True,
67
+ )
68
+
69
+ with gr.Row():
70
+ with gr.Column(scale=5):
71
+ msg = gr.Textbox(
72
+ placeholder="Describe what SQL query you need...",
73
+ lines=2,
74
+ container=False
75
+ )
76
+ with gr.Column(scale=1, min_width=100):
77
+ submit_btn = gr.Button("Submit", variant="primary")
78
+
79
+ gr.Markdown("### Quick Examples")
80
+ gr.Examples(
81
+ examples=PRESET_EXAMPLES,
82
+ inputs=msg,
83
+ label="Click an example → then Submit"
84
+ )
85
+
86
+ submit_btn.click(
87
+ fn=respond,
88
+ inputs=[msg, chatbot],
89
+ outputs=[chatbot, msg]
90
+ )
91
+
92
+ msg.submit(
93
+ fn=respond,
94
+ inputs=[msg, chatbot],
95
+ outputs=[chatbot, msg]
96
+ )
97
+
98
+ clear_btn = gr.Button("Clear Chat")
99
+ clear_btn.click(lambda: ([], ""), outputs=[chatbot, msg])
100
+
101
+
102
+ if __name__ == "__main__":
103
+ print("Gradio version:", gr.__version__)
104
+ demo.launch()
105
+
106
+ # gradio < 5
107
+ '''@spaces.GPU
108
+ def respond(user_message, chat_history):
109
+ chat_history = chat_history or []
110
+
111
+ if not user_message or not user_message.strip():
112
+ return chat_history, ""
113
+
114
+ # Modern messages format (required in Gradio 5/6)
115
+ chat_history.append({"role": "user", "content": user_message})
116
+ chat_history.append({"role": "assistant", "content": None})
117
+
118
+ prompt = ALPACA_TEMPLATE.format(instruction=user_message, input="")
119
+
120
+ sequences = pipe(
121
+ prompt,
122
+ max_new_tokens=256,
123
+ return_full_text=False,
124
+ temperature=0.4,
125
+ top_k=100,
126
+ do_sample=True,
127
+ )
128
+
129
+ bot_response = sequences[0]["generated_text"].strip()
130
+ chat_history[-1]["content"] = bot_response
131
+
132
+ return chat_history, ""
133
+
134
+
135
+ # ====================== UI ======================
136
+ with gr.Blocks(title="Text to SQL via Llama 3.2") as demo:
137
+ gr.Markdown("# Text to SQL via Llama 3.2\n### Convert natural language to PostgreSQL queries")
138
+
139
+ chatbot = gr.Chatbot(
140
+ label="Chat",
141
+ height=500,
142
+ type="messages",
143
+ )
144
+
145
+ with gr.Row():
146
+ with gr.Column(scale=5):
147
+ msg = gr.Textbox(
148
+ placeholder="Describe what SQL query you need...",
149
+ lines=2,
150
+ container=False
151
+ )
152
+ with gr.Column(scale=1, min_width=100):
153
+ submit_btn = gr.Button("Submit", variant="primary")
154
+
155
+ gr.Markdown("### Quick Examples (click to fill, then Submit)")
156
+ gr.Examples(
157
+ examples=PRESET_EXAMPLES,
158
+ inputs=msg,
159
+ )
160
+
161
+ submit_btn.click(
162
+ fn=respond,
163
+ inputs=[msg, chatbot],
164
+ outputs=[chatbot, msg]
165
+ )
166
+
167
+ msg.submit(
168
+ fn=respond,
169
+ inputs=[msg, chatbot],
170
+ outputs=[chatbot, msg]
171
+ )
172
+
173
+ clear_btn = gr.Button("Clear Chat")
174
+ clear_btn.click(lambda: ([], ""), outputs=[chatbot, msg])
175
+
176
+
177
+ if __name__ == "__main__":
178
+ print("Gradio version:", gr.__version__)
179
+ demo.launch()'''
180
+
181
+ '''@spaces.GPU
182
+ def chat_predict(message, history):
183
+ # For a chat interface, we treat the latest user message as the instruction
184
+ # and leave the input field empty for this example.
185
+ prompt = ALPACA_TEMPLATE.format(instruction=message, input="")
186
+
187
+ # Generate text (adjust max_new_tokens as needed)
188
+ sequences = pipe(
189
+ prompt,
190
+ max_new_tokens=128,
191
+ return_full_text=False,
192
+ temperature=0.4,
193
+ top_k=100,
194
+ # top_p=0.9,
195
+ )
196
+
197
+ # Extract and return the generated text
198
+ response = sequences[0]['generated_text'].strip()
199
+ return response
200
+
201
+ # Create the Gradio ChatInterface
202
+ demo = gr.ChatInterface(
203
+ fn=chat_predict,
204
+ title="Text to PostGIS SQL via Llama 3.2 - Primary Functions",
205
+ description="LLama 3.2 Spatial - Text to PostGIS SQL",
206
+ )
207
+
208
+ if __name__ == "__main__":
209
+ print("gradio " + gr.__version__)
210
+ demo.launch()'''
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ torch
2
+ transformers
3
+ gradio>=6.0
4
+ accelerate
5
+ spaces