sirunchained commited on
Commit
3d0eabd
·
verified ·
1 Parent(s): 75e9362

Update app.py

Browse files

Some Optimization.

Files changed (1) hide show
  1. app.py +23 -16
app.py CHANGED
@@ -3,18 +3,23 @@ import gradio as gr
3
  from transformers import pipeline
4
  import torch
5
 
 
 
 
 
 
 
 
 
6
 
7
  @spaces.GPU
 
8
  def predict_sql(text_input, schema_input=""):
9
- # Get model
10
- DEVICE = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
11
- generator = pipeline("text-generation", model="sirunchained/text-to-sql-model-v2", device=DEVICE)
12
-
13
  # Construct the user content including schema if provided
14
- if schema_input and schema_input.strip() != "":
15
- user_content = f"# Schema \n{schema_input} \n\n# Text \n{text_input}"
16
  else:
17
- user_content = f"# Text \n{text_input}"
18
 
19
  chat_template_input = [
20
  {"role": "user", "content": user_content}
@@ -26,21 +31,23 @@ def predict_sql(text_input, schema_input=""):
26
  tokenize=False,
27
  add_generation_prompt=True,
28
  )
 
29
  # Generate output using the fine-tuned model
30
- output = generator(formatted_input, max_new_tokens=256, disable_compile=True)
 
 
 
 
31
 
32
  # Extract the generated SQL query
33
  generated_text = output[0]["generated_text"]
34
- # The model output includes the prompt, so we need to extract only the assistant's part
35
- # The assistant's response starts after '<start_of_turn>model'
36
- start_token = '<start_of_turn>model\n'
37
- if start_token in generated_text:
38
- sql_query = generated_text.split(start_token)[-1].strip()
39
- else:
40
- sql_query = generated_text.strip() # Fallback if token not found
41
 
42
  # Further clean up any unwanted tokens like '<end_of_turn>'
43
- sql_query = sql_query.replace('<end_of_turn>', '').strip()
 
44
  return sql_query
45
 
46
  # Sample queries for the Gradio interface including SELECT, INSERT, UPDATE, and DELETE operations
 
3
  from transformers import pipeline
4
  import torch
5
 
6
+ DEVICE = 0 if torch.cuda.is_available() else -1
7
+
8
+ generator = pipeline(
9
+ "text-generation",
10
+ model="sirunchained/text-to-sql-model-v2",
11
+ device=DEVICE,
12
+ torch_dtype=torch.float16 if DEVICE == 0 else torch.float32,
13
+ )
14
 
15
  @spaces.GPU
16
+ @torch.inference_mode()
17
  def predict_sql(text_input, schema_input=""):
 
 
 
 
18
  # Construct the user content including schema if provided
19
+ if schema_input and schema_input.strip():
20
+ user_content = f"# Schema\n{schema_input}\n\n# Text\n{text_input}"
21
  else:
22
+ user_content = f"# Text\n{text_input}"
23
 
24
  chat_template_input = [
25
  {"role": "user", "content": user_content}
 
31
  tokenize=False,
32
  add_generation_prompt=True,
33
  )
34
+
35
  # Generate output using the fine-tuned model
36
+ output = generator(
37
+ formatted_input,
38
+ max_new_tokens=256,
39
+ do_sample=False,
40
+ )
41
 
42
  # Extract the generated SQL query
43
  generated_text = output[0]["generated_text"]
44
+
45
+ # Remove the prompt from the generated output
46
+ sql_query = generated_text[len(formatted_input):].strip()
 
 
 
 
47
 
48
  # Further clean up any unwanted tokens like '<end_of_turn>'
49
+ sql_query = sql_query.replace("<end_of_turn>", "").strip()
50
+
51
  return sql_query
52
 
53
  # Sample queries for the Gradio interface including SELECT, INSERT, UPDATE, and DELETE operations