File size: 1,084 Bytes
04efaa2
47f333e
 
04efaa2
600be72
1be65a2
04efaa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# app.py
import subprocess
subprocess.run(["pip", "install", "-r", "requirements.txt"])
import streamlit as st
from transformers import AutoModelForSeq2SeqLM


def main():
    st.title("Hugging Face SQL Generator")

    # Get user input
    prompt = st.text_area("Enter your prompt:")

    if st.button("Generate SQL"):
        # Call a function to generate SQL using the Hugging Face model
        sql_result = generate_sql(prompt)

        # Display the SQL result
        st.write("Generated SQL:")
        st.code(sql_result, language="sql")

def generate_sql(prompt):
    # Load the "NumbersStation/nsql-350M" model
    model_name = "NumbersStation/nsql-350M"
    model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
    tokenizer = AutoTokenizer.from_pretrained(model_name)

    # Tokenize and generate SQL
    inputs = tokenizer(prompt, return_tensors="pt")
    outputs = model(**inputs)

    # Decode the generated SQL
    sql_query = tokenizer.batch_decode(outputs["output_ids"], skip_special_tokens=True)[0]

    return sql_query

if __name__ == "__main__":
    main()