Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,84 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
|
| 4 |
-
# Load the models
|
| 5 |
-
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
-
# Function to
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
table_str = ", ".join([f"{table}: ({', '.join(cols)})" for table, cols in tables.items()])
|
| 15 |
-
prompt = f"Generate a question based on the following table schema: {table_str}"
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
# Function to
|
| 24 |
-
def
|
| 25 |
-
|
| 26 |
-
prompt = f"Convert the question and table schema into an SQL query. Tables: {table_str}. Question: {question}"
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
return input_ids
|
| 30 |
|
| 31 |
-
# Inference function for
|
| 32 |
-
def
|
| 33 |
-
input_data =
|
| 34 |
-
input_data = input_data.to(
|
| 35 |
-
outputs =
|
| 36 |
-
|
| 37 |
-
return sql_query
|
| 38 |
|
| 39 |
# Streamlit UI
|
| 40 |
def main():
|
| 41 |
st.title("Multi-Model: Text to SQL and Question Generation")
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
'{"people_name": ["id", "name"], "people_age": ["people_id", "age"]}')
|
| 46 |
-
try:
|
| 47 |
-
tables = eval(tables_input) # Convert string to dict safely
|
| 48 |
-
except:
|
| 49 |
-
tables = {}
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
if
|
| 53 |
-
|
| 54 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
|
| 4 |
+
# Load the models
|
| 5 |
+
tokenizer_sql = AutoTokenizer.from_pretrained("juierror/flan-t5-text2sql-with-schema-v2")
|
| 6 |
+
model_sql = AutoModelForSeq2SeqLM.from_pretrained("juierror/flan-t5-text2sql-with-schema-v2")
|
| 7 |
|
| 8 |
+
tokenizer_question = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-question-generation-ap")
|
| 9 |
+
model_question = AutoModelForSeq2SeqLM.from_pretrained("mrm8488/t5-base-finetuned-question-generation-ap")
|
| 10 |
|
| 11 |
+
# Function to create the prompt for SQL model
|
| 12 |
+
def get_prompt_sql(tables, question):
|
| 13 |
+
return f"""convert question and table into SQL query. tables: {tables}. question: {question}"""
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Function to prepare input data for the SQL model
|
| 16 |
+
def prepare_input_sql(question: str, tables: dict):
|
| 17 |
+
tables = [f"""{table_name}({','.join(tables[table_name])})""" for table_name in tables]
|
| 18 |
+
tables = ", ".join(tables)
|
| 19 |
+
prompt = get_prompt_sql(tables, question)
|
| 20 |
+
input_ids = tokenizer_sql(prompt, max_length=512, return_tensors="pt").input_ids
|
| 21 |
+
return input_ids
|
| 22 |
+
|
| 23 |
+
# Inference function for the SQL model
|
| 24 |
+
def inference_sql(question: str, tables: dict) -> str:
|
| 25 |
+
input_data = prepare_input_sql(question=question, tables=tables)
|
| 26 |
+
input_data = input_data.to(model_sql.device)
|
| 27 |
+
outputs = model_sql.generate(inputs=input_data, num_beams=10, top_k=10, max_length=512)
|
| 28 |
+
return tokenizer_sql.decode(outputs[0], skip_special_tokens=True)
|
| 29 |
|
| 30 |
+
# Function to create the prompt for Question Generation model
|
| 31 |
+
def get_prompt_question(context):
|
| 32 |
+
return f"generate a question from the following context: {context}"
|
|
|
|
| 33 |
|
| 34 |
+
# Function to prepare input data for the Question Generation model
|
| 35 |
+
def prepare_input_question(context: str):
|
| 36 |
+
prompt = get_prompt_question(context)
|
| 37 |
+
input_ids = tokenizer_question(prompt, max_length=512, return_tensors="pt").input_ids
|
| 38 |
return input_ids
|
| 39 |
|
| 40 |
+
# Inference function for the Question Generation model
|
| 41 |
+
def inference_question(context: str) -> str:
|
| 42 |
+
input_data = prepare_input_question(context)
|
| 43 |
+
input_data = input_data.to(model_question.device)
|
| 44 |
+
outputs = model_question.generate(inputs=input_data, num_beams=10, top_k=10, max_length=512)
|
| 45 |
+
return tokenizer_question.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 46 |
|
| 47 |
# Streamlit UI
|
| 48 |
def main():
|
| 49 |
st.title("Multi-Model: Text to SQL and Question Generation")
|
| 50 |
|
| 51 |
+
# Model selection
|
| 52 |
+
model_choice = st.selectbox("Select a model", ["Text to SQL", "Question Generation"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# Input question and table schema for SQL model
|
| 55 |
+
if model_choice == "Text to SQL":
|
| 56 |
+
st.subheader("Text to SQL Model")
|
| 57 |
+
question = st.text_area("Enter your question:")
|
| 58 |
+
tables_input = st.text_area("Enter table schemas (in JSON format):", '{"people_name": ["id", "name"], "people_age": ["people_id", "age"]}')
|
| 59 |
+
try:
|
| 60 |
+
tables = eval(tables_input) # Convert string to dict safely
|
| 61 |
+
except:
|
| 62 |
+
tables = {}
|
| 63 |
|
| 64 |
+
if st.button("Generate SQL Query"):
|
| 65 |
+
if question and tables:
|
| 66 |
+
sql_query = inference_sql(question, tables)
|
| 67 |
+
st.write(f"Generated SQL Query: {sql_query}")
|
| 68 |
+
else:
|
| 69 |
+
st.write("Please enter both a question and table schemas.")
|
| 70 |
|
| 71 |
+
# Input context for Question Generation model
|
| 72 |
+
elif model_choice == "Question Generation":
|
| 73 |
+
st.subheader("Question Generation Model")
|
| 74 |
+
context = st.text_area("Enter context:")
|
| 75 |
+
|
| 76 |
+
if st.button("Generate Question"):
|
| 77 |
+
if context:
|
| 78 |
+
generated_question = inference_question(context)
|
| 79 |
+
st.write(f"Generated Question: {generated_question}")
|
| 80 |
+
else:
|
| 81 |
+
st.write("Please enter context for question generation.")
|
| 82 |
|
| 83 |
if __name__ == "__main__":
|
| 84 |
main()
|