markrodrigo commited on
Commit
f938394
·
1 Parent(s): a140092
Files changed (1) hide show
  1. app.py +37 -93
app.py CHANGED
@@ -2,13 +2,12 @@ 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:
@@ -18,23 +17,22 @@ ALPACA_TEMPLATE = """<|begin_of_text|><|start_header_id|>system<|end_header_id|>
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
 
@@ -55,7 +53,7 @@ def respond(user_message, chat_history):
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
 
@@ -80,20 +78,11 @@ with gr.Blocks(title="Text to SQL via Llama 3.2") as demo:
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])
@@ -102,82 +91,36 @@ with gr.Blocks(title="Text to SQL via Llama 3.2") as demo:
102
  if __name__ == "__main__":
103
  print("Gradio version:", gr.__version__)
104
  demo.launch()
105
- '''
106
-
107
- # gradio < 5
108
- '''@spaces.GPU
109
- def respond(user_message, chat_history):
110
- chat_history = chat_history or []
111
-
112
- if not user_message or not user_message.strip():
113
- return chat_history, ""
114
-
115
- # Modern messages format (required in Gradio 5/6)
116
- chat_history.append({"role": "user", "content": user_message})
117
- chat_history.append({"role": "assistant", "content": None})
118
-
119
- prompt = ALPACA_TEMPLATE.format(instruction=user_message, input="")
120
-
121
- sequences = pipe(
122
- prompt,
123
- max_new_tokens=256,
124
- return_full_text=False,
125
- temperature=0.4,
126
- top_k=100,
127
- do_sample=True,
128
- )
129
-
130
- bot_response = sequences[0]["generated_text"].strip()
131
- chat_history[-1]["content"] = bot_response
132
-
133
- return chat_history, ""
134
-
135
-
136
- # ====================== UI ======================
137
- with gr.Blocks(title="Text to SQL via Llama 3.2") as demo:
138
- gr.Markdown("# Text to SQL via Llama 3.2\n### Convert natural language to PostgreSQL queries")
139
 
140
- chatbot = gr.Chatbot(
141
- label="Chat",
142
- height=500,
143
- type="messages",
144
- )
145
 
146
- with gr.Row():
147
- with gr.Column(scale=5):
148
- msg = gr.Textbox(
149
- placeholder="Describe what SQL query you need...",
150
- lines=2,
151
- container=False
152
- )
153
- with gr.Column(scale=1, min_width=100):
154
- submit_btn = gr.Button("Submit", variant="primary")
155
 
156
- gr.Markdown("### Quick Examples (click to fill, then Submit)")
157
- gr.Examples(
158
- examples=PRESET_EXAMPLES,
159
- inputs=msg,
160
- )
161
 
162
- submit_btn.click(
163
- fn=respond,
164
- inputs=[msg, chatbot],
165
- outputs=[chatbot, msg]
166
- )
167
 
168
- msg.submit(
169
- fn=respond,
170
- inputs=[msg, chatbot],
171
- outputs=[chatbot, msg]
172
- )
173
 
174
- clear_btn = gr.Button("Clear Chat")
175
- clear_btn.click(lambda: ([], ""), outputs=[chatbot, msg])
 
176
 
 
 
 
 
 
 
 
 
177
 
178
- if __name__ == "__main__":
179
- print("Gradio version:", gr.__version__)
180
- demo.launch()'''
181
 
182
  @spaces.GPU
183
  def chat_predict(message, history):
@@ -210,3 +153,4 @@ demo = gr.ChatInterface(
210
  if __name__ == "__main__":
211
  print("gradio " + gr.__version__)
212
  demo.launch()
 
 
2
  import gradio as gr
3
  import spaces
4
 
5
+
6
  pipe = pipeline("text-generation", model="markrodrigo/Llama-3.2-3B-Instruct-Spatial-SQL-1.1", device_map="auto")
7
 
8
+ ALPACA_TEMPLATE = """<|begin_of_text|><|start_header_id|>system<|end_header_id|> You are a helpful assistant. You are an expert at Postgresql and SQL and psql. <|eot_id|><|start_header_id|>user<|end_header_id|>
 
9
 
10
+ ### Instruction: Write a SQL statement for the following.
11
  {instruction}
12
 
13
  ### Input:
 
17
  <|eot_id|><|start_header_id|>assistant<|end_header_id|>
18
  """
19
 
20
+
21
  PRESET_EXAMPLES = [
22
+ "Example one here",
23
+ "Example two here",
24
+ "Example three here",
 
 
25
  ]
26
 
27
+
28
+ @spaces.GPU
29
  def respond(user_message, chat_history):
30
  chat_history = chat_history or []
31
 
32
  if not user_message or not user_message.strip():
33
  return chat_history, ""
34
 
35
+ # Modern format for Gradio 5/6
36
  chat_history.append({"role": "user", "content": user_message})
37
  chat_history.append({"role": "assistant", "content": None})
38
 
 
53
  return chat_history, ""
54
 
55
 
56
+
57
  with gr.Blocks(title="Text to SQL via Llama 3.2") as demo:
58
  gr.Markdown("# Text to SQL via Llama 3.2\n### Convert natural language to PostgreSQL queries")
59
 
 
78
  gr.Examples(
79
  examples=PRESET_EXAMPLES,
80
  inputs=msg,
81
+ label="Click an example → then click Submit"
82
  )
83
 
84
+ submit_btn.click(fn=respond, inputs=[msg, chatbot], outputs=[chatbot, msg])
85
+ msg.submit(fn=respond, inputs=[msg, chatbot], outputs=[chatbot, msg])
 
 
 
 
 
 
 
 
 
86
 
87
  clear_btn = gr.Button("Clear Chat")
88
  clear_btn.click(lambda: ([], ""), outputs=[chatbot, msg])
 
91
  if __name__ == "__main__":
92
  print("Gradio version:", gr.__version__)
93
  demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
95
+ '''from transformers import pipeline
96
+ import gradio as gr
97
+ import spaces
 
 
98
 
99
+ # Initialize the pipeline with an Alpaca-tuned model
100
+ pipe = pipeline("text-generation", model="markrodrigo/Llama-3.2-3B-Instruct-Spatial-SQL-1.1", device_map="auto")
 
 
 
 
 
 
 
101
 
102
+ # The Alpaca instruction prompt format
103
+ 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|>
 
 
 
104
 
105
+ ### Instruction: Write a PostGIS SQL statement for the following.
106
+ {instruction}
 
 
 
107
 
108
+ ### Input:
109
+ {input}
 
 
 
110
 
111
+ ### Response:
112
+ <|eot_id|><|start_header_id|>assistant<|end_header_id|>
113
+ """
114
 
115
+ # Define your list of pre-set example prompts
116
+ PRESET_EXAMPLES = [
117
+ "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))'",
118
+ "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))'",
119
+ "What is the thousand meter buffer for the following point? : 'Point(-8.7522658 41.3862664)'",
120
+ "How long is the line? : 'LINESTRING (-3.6976693 40.4263178, -3.6986082 40.4258729)'",
121
+ "How far apart is the point and line? : 'Point(-109.87549823 38.60574249)' 'LineString(-109.24324628 38.76349931, -109.4821773 38.6875815)'"
122
+ ]
123
 
 
 
 
124
 
125
  @spaces.GPU
126
  def chat_predict(message, history):
 
153
  if __name__ == "__main__":
154
  print("gradio " + gr.__version__)
155
  demo.launch()
156
+ '''