Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from dotenv import load_dotenv # Import load_dotenv to load environment variables
|
4 |
+
from langchain import HuggingFaceHub
|
5 |
+
|
6 |
+
# Load environment variables from the .env file
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
# Set your Hugging Face API token from the environment variable
|
10 |
+
HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
11 |
+
|
12 |
+
# Function to return the SQL query from natural language input
|
13 |
+
def load_sql_query(question):
|
14 |
+
try:
|
15 |
+
# Initialize the Hugging Face model using LangChain's HuggingFaceHub class
|
16 |
+
llm = HuggingFaceHub(
|
17 |
+
repo_id="Salesforce/grappa_large_jnt", # Hugging Face model repo for text-to-SQL
|
18 |
+
huggingfacehub_api_token=HUGGINGFACE_API_TOKEN, # Pass your API token
|
19 |
+
model_kwargs={"temperature": 0.3} # Optional: Adjust response randomness
|
20 |
+
)
|
21 |
+
|
22 |
+
# Call the model with the user's question and get the SQL query
|
23 |
+
sql_query = llm.predict(question)
|
24 |
+
return sql_query
|
25 |
+
except Exception as e:
|
26 |
+
# Capture and return any exceptions or errors
|
27 |
+
return f"Error: {str(e)}"
|
28 |
+
|
29 |
+
# Streamlit App UI starts here
|
30 |
+
st.set_page_config(page_title="Text-to-SQL Demo", page_icon=":robot:")
|
31 |
+
st.header("Text-to-SQL Demo")
|
32 |
+
|
33 |
+
# Function to get user input
|
34 |
+
def get_text():
|
35 |
+
input_text = st.text_input("Ask a question (related to a database):", key="input")
|
36 |
+
return input_text
|
37 |
+
|
38 |
+
# Get user input
|
39 |
+
user_input = get_text()
|
40 |
+
|
41 |
+
# Create a button for generating the SQL query
|
42 |
+
submit = st.button('Generate SQL')
|
43 |
+
|
44 |
+
# If the generate button is clicked and user input is not empty
|
45 |
+
if submit and user_input:
|
46 |
+
response = load_sql_query(user_input)
|
47 |
+
st.subheader("Generated SQL Query:")
|
48 |
+
st.write(response)
|
49 |
+
elif submit:
|
50 |
+
st.warning("Please enter a question.") # Warning for empty input
|