Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
import requests
|
4 |
+
|
5 |
+
# Retrieve Hugging Face API token from environment variable
|
6 |
+
API_TOKEN = os.environ.get("HUGGING_FACE_API_TOKEN")
|
7 |
+
|
8 |
+
# Define the Hugging Face API URL
|
9 |
+
API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B"
|
10 |
+
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
11 |
+
|
12 |
+
# Function to query the Hugging Face API
|
13 |
+
def query(payload):
|
14 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
15 |
+
return response.json()
|
16 |
+
|
17 |
+
# Streamlit app
|
18 |
+
def main():
|
19 |
+
st.title("SQL Query Generator")
|
20 |
+
|
21 |
+
# User prompt input
|
22 |
+
prompt = st.text_input("Enter your prompt:", "Please generate a SQL query to fetch data from the database.")
|
23 |
+
|
24 |
+
# Button to generate SQL query
|
25 |
+
if st.button("Generate SQL Query"):
|
26 |
+
# Generate payload for Hugging Face API
|
27 |
+
payload = {"inputs": prompt}
|
28 |
+
|
29 |
+
# Query the Hugging Face API
|
30 |
+
with st.spinner('Generating SQL query...'):
|
31 |
+
output = query(payload)
|
32 |
+
|
33 |
+
# Display the SQL query response
|
34 |
+
if "generated_text" in output:
|
35 |
+
st.write("Generated SQL Query:")
|
36 |
+
st.code(output["generated_text"])
|
37 |
+
else:
|
38 |
+
st.error("Failed to generate SQL query. Please try again.")
|
39 |
+
|
40 |
+
if __name__ == "__main__":
|
41 |
+
main()
|