Spaces:
Runtime error
Runtime error
create
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
##Bloom
|
| 6 |
+
API_URL = "https://api-inference.huggingface.co/models/bigscience/bloom"
|
| 7 |
+
HF_TOKEN = os.environ["HF_TOKEN"]
|
| 8 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
prompt1 = """
|
| 12 |
+
word: risk
|
| 13 |
+
poem using word: And then the day came,
|
| 14 |
+
when the risk
|
| 15 |
+
to remain tight
|
| 16 |
+
in a bud
|
| 17 |
+
was more painful
|
| 18 |
+
than the risk
|
| 19 |
+
it took
|
| 20 |
+
to blossom.
|
| 21 |
+
word: """
|
| 22 |
+
|
| 23 |
+
prompt2 = """
|
| 24 |
+
Q: Joy has 5 balls. He buys 2 more cans of balls. Each can has 3 balls. How many balls he has now?
|
| 25 |
+
A: Joy had 5 balls. 2 cans of 3 balls each is 6 balls. 5 + 6 = 11. Answer is 11.
|
| 26 |
+
Q: Jane has 16 balls. Half balls are golf balls, and half golf balls are red. How many red golf balls are there?
|
| 27 |
+
A: """
|
| 28 |
+
|
| 29 |
+
prompt3 = """Q: A juggler can juggle 16 balls. Half of the balls are golf balls, and half of the golf balls are blue. How many blue golf balls are there?
|
| 30 |
+
A: Let’s think step by step.
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def sql_generate(prompt, input_prompt_sql ):
|
| 36 |
+
|
| 37 |
+
print(f"*****Inside SQL_generate - Prompt is :{prompt}")
|
| 38 |
+
if input_prompt_sql != '':
|
| 39 |
+
prompt = "Instruction: Given an input question, respond with syntactically correct PostgreSQL\nInput: " +input_prompt_sql + "\nPostgreSQL query: "
|
| 40 |
+
|
| 41 |
+
json_ = {"inputs": prompt,
|
| 42 |
+
"parameters":
|
| 43 |
+
{
|
| 44 |
+
"top_p": 0.9,
|
| 45 |
+
"temperature": 1.1,
|
| 46 |
+
"max_new_tokens": 250,
|
| 47 |
+
"return_full_text": False,
|
| 48 |
+
},
|
| 49 |
+
"options":
|
| 50 |
+
{"use_cache": True,
|
| 51 |
+
"wait_for_model": True,
|
| 52 |
+
},}
|
| 53 |
+
response = requests.post(API_URL, headers=headers, json=json_)
|
| 54 |
+
print(f"Response is : {response}")
|
| 55 |
+
output = response.json()
|
| 56 |
+
print(f"output is : {output}") #{output}")
|
| 57 |
+
output_tmp = output[0]['generated_text']
|
| 58 |
+
print(f"output_tmp is: {output_tmp}")
|
| 59 |
+
solution = output_tmp.split("\nQ:")[0] #output[0]['generated_text'].split("Q:")[0] # +"."
|
| 60 |
+
print(f"Final response after splits is: {solution}")
|
| 61 |
+
if '\nOutput:' in solution:
|
| 62 |
+
final_solution = solution.split("\nOutput:")[0]
|
| 63 |
+
print(f"Response after removing output is: {final_solution}")
|
| 64 |
+
elif '\n\n' in solution:
|
| 65 |
+
final_solution = solution.split("\n\n")[0]
|
| 66 |
+
print(f"Response after removing new line entries is: {final_solution}")
|
| 67 |
+
else:
|
| 68 |
+
final_solution = solution
|
| 69 |
+
return final_solution
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
demo = gr.Blocks()
|
| 73 |
+
|
| 74 |
+
with demo:
|
| 75 |
+
gr.Markdown("<h1><center>Bloom</center></h1>")
|
| 76 |
+
gr.Markdown(
|
| 77 |
+
"""Testing Bloom for SQL generation """
|
| 78 |
+
)
|
| 79 |
+
with gr.Row():
|
| 80 |
+
|
| 81 |
+
example_prompt = gr.Radio( [
|
| 82 |
+
"Instruction: Given an input question, respond with syntactically correct PostgreSQL\nInput: How many users signed up in the past month?\nPostgreSQL query: ",
|
| 83 |
+
"Instruction: Given an input question, respond with syntactically correct PostgreSQL\nInput: Create a query that displays empfname, emplname, deptid, deptname, location from employee table. Results should be in the ascending order based on the empfname and location.\nPostgreSQL query: ",
|
| 84 |
+
"Instruction: Given an input question, respond with syntactically correct PostgreSQL. Only use table called 'employees'.\nInput: What is the total salary paid to all the employees?\nPostgreSQL query: ",
|
| 85 |
+
"Instruction: Given an input question, respond with syntactically correct PostgreSQL. Only use table called 'employees'.\nInput: List names of all the employees whose name end with 'r'.\nPostgreSQL query: ",
|
| 86 |
+
"Instruction: Given an input question, respond with syntactically correct PostgreSQL. Only use table called 'employees'.\nInput: What are the number of employees in each department?\nPostgreSQL query: ",
|
| 87 |
+
"Instruction: Given an input question, respond with syntactically correct PostgreSQL. Only use table called 'employees'.\nInput: Select names of all theemployees who have third character in their name as 't'.\nPostgreSQL query: ",
|
| 88 |
+
"Instruction: Given an input question, respond with syntactically correct PostgreSQL. Only use table called 'employees'.\nInput: Select names of all the employees who are working under 'Peter'\nPostgreSQL query: ", ], label= "Choose a sample Prompt")
|
| 89 |
+
|
| 90 |
+
input_prompt_sql = gr.Textbox(label="Or Write text following the above pattern to get SQL commands...", value="Instruction: Given an input question, respond with syntactically correct PostgreSQL. Only use table called 'department'.\nInput: Select names of all the departments in descending alphabetical order of department names.\nPostgreSQL query: ")
|
| 91 |
+
|
| 92 |
+
with gr.Row():
|
| 93 |
+
generated_txt = gr.Textbox(lines=7)
|
| 94 |
+
|
| 95 |
+
b1 = gr.Button("Generate SQL")
|
| 96 |
+
|
| 97 |
+
b1.click(sql_generate,inputs=[example_prompt, input_prompt_sql], outputs=generated_txt)
|
| 98 |
+
|
| 99 |
+
demo.launch(enable_queue=True, debug=True)
|