JeffersonCorreiax commited on
Commit
fc5b211
·
verified ·
1 Parent(s): bb01197

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -20
app.py CHANGED
@@ -1,24 +1,35 @@
1
- import os
2
- import gradio as gr
3
- from huggingface_hub import InferenceClient
4
 
5
- # Inicialize o InferenceClient com o novo modelo
6
- client = InferenceClient(model="cssupport/t5-small-awesome-text-to-sql")
7
 
8
- # Função para gerar SQL a partir de linguagem natural
9
- def gerar_sql(consulta_natural):
10
- # Envia a consulta para a API
11
- resposta = client.text_generation(prompt=consulta_natural, max_new_tokens=100)
12
- return resposta
13
 
14
- # Interface Gradio
15
- interface = gr.Interface(
16
- fn=gerar_sql,
17
- inputs="text",
18
- outputs="text",
19
- title="Gerador de SQL",
20
- description="Digite uma consulta em linguagem natural e gere a consulta SQL correspondente."
21
- )
 
 
 
 
22
 
23
- # Iniciar a interface
24
- interface.launch()
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
 
3
 
4
+ # Initialize the tokenizer from Hugging Face Transformers library
5
+ tokenizer = T5Tokenizer.from_pretrained('t5-small')
6
 
7
+ # Load the model
8
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
+ model = T5ForConditionalGeneration.from_pretrained('cssupport/t5-small-awesome-text-to-sql')
10
+ model = model.to(device)
11
+ model.eval()
12
 
13
+ def generate_sql(input_prompt):
14
+ # Tokenize the input prompt
15
+ inputs = tokenizer(input_prompt, padding=True, truncation=True, return_tensors="pt").to(device)
16
+
17
+ # Forward pass
18
+ with torch.no_grad():
19
+ outputs = model.generate(**inputs, max_length=512)
20
+
21
+ # Decode the output IDs to a string (SQL query in this case)
22
+ generated_sql = tokenizer.decode(outputs[0], skip_special_tokens=True)
23
+
24
+ return generated_sql
25
 
26
+ # Test the function
27
+ #input_prompt = "tables:\n" + "CREATE TABLE Catalogs (date_of_latest_revision VARCHAR)" + "\n" +"query for: Find the dates on which more than one revisions were made."
28
+ #input_prompt = "tables:\n" + "CREATE TABLE table_22767 ( \"Year\" real, \"World\" real, \"Asia\" text, \"Africa\" text, \"Europe\" text, \"Latin America/Caribbean\" text, \"Northern America\" text, \"Oceania\" text )" + "\n" +"query for:what will the population of Asia be when Latin America/Caribbean is 783 (7.5%)?."
29
+ #input_prompt = "tables:\n" + "CREATE TABLE procedures ( subject_id text, hadm_id text, icd9_code text, short_title text, long_title text ) CREATE TABLE diagnoses ( subject_id text, hadm_id text, icd9_code text, short_title text, long_title text ) CREATE TABLE lab ( subject_id text, hadm_id text, itemid text, charttime text, flag text, value_unit text, label text, fluid text ) CREATE TABLE demographic ( subject_id text, hadm_id text, name text, marital_status text, age text, dob text, gender text, language text, religion text, admission_type text, days_stay text, insurance text, ethnicity text, expire_flag text, admission_location text, discharge_location text, diagnosis text, dod text, dob_year text, dod_year text, admittime text, dischtime text, admityear text ) CREATE TABLE prescriptions ( subject_id text, hadm_id text, icustay_id text, drug_type text, drug text, formulary_drug_cd text, route text, drug_dose text )" + "\n" +"query for:" + "what is the total number of patients who were diagnosed with icd9 code 2254?"
30
+ input_prompt = "tables:\n" + "CREATE TABLE student_course_attendance (student_id VARCHAR); CREATE TABLE students (student_id VARCHAR)" + "\n" + "query for:" + "List the id of students who never attends courses?"
31
+
32
+ generated_sql = generate_sql(input_prompt)
33
+
34
+ print(f"The generated SQL query is: {generated_sql}")
35
+ #OUTPUT: The generated SQL query is: SELECT student_id FROM students WHERE NOT student_id IN (SELECT student_id FROM student_course_attendance)