Spaces:
Runtime error
Runtime error
mohan1869
commited on
Commit
·
2c9b240
1
Parent(s):
b973723
Deploy SQLCoder with Streamlit
Browse files
app.py
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
|
4 |
-
# Set page configuration
|
5 |
-
st.set_page_config(page_title="SQLCoder", layout="wide")
|
6 |
|
7 |
-
#
|
8 |
@st.cache_resource
|
9 |
def load_model():
|
10 |
model_name = "defog/sqlcoder"
|
@@ -12,31 +12,38 @@ def load_model():
|
|
12 |
model = AutoModelForCausalLM.from_pretrained(
|
13 |
model_name,
|
14 |
device_map="auto",
|
15 |
-
|
16 |
low_cpu_mem_usage=True,
|
17 |
)
|
18 |
return tokenizer, model
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
tokenizer, model = load_model()
|
28 |
|
29 |
-
#
|
30 |
def generate_code(prompt, max_length=150):
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
# Streamlit app layout
|
36 |
st.title("SQLCoder: AI-Powered SQL Code Generator")
|
37 |
st.write("Generate SQL queries and code snippets using the SQLCoder model.")
|
38 |
|
39 |
-
# Input
|
40 |
prompt = st.text_area("Enter your query or code prompt:", height=150)
|
41 |
max_length = st.slider("Maximum Output Length", min_value=50, max_value=300, value=150, step=10)
|
42 |
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
|
4 |
+
# Set Streamlit page configuration
|
5 |
+
st.set_page_config(page_title="SQLCoder: AI-Powered SQL Code Generator", layout="wide")
|
6 |
|
7 |
+
# Function to load the model and tokenizer
|
8 |
@st.cache_resource
|
9 |
def load_model():
|
10 |
model_name = "defog/sqlcoder"
|
|
|
12 |
model = AutoModelForCausalLM.from_pretrained(
|
13 |
model_name,
|
14 |
device_map="auto",
|
15 |
+
offload_folder="./offload_weights", # Ensure weights are stored properly
|
16 |
low_cpu_mem_usage=True,
|
17 |
)
|
18 |
return tokenizer, model
|
19 |
|
20 |
+
# Load the model and tokenizer
|
21 |
+
try:
|
22 |
+
tokenizer, model = load_model()
|
23 |
+
except Exception as e:
|
24 |
+
st.error(f"Error loading model: {e}")
|
25 |
+
st.stop()
|
|
|
|
|
26 |
|
27 |
+
# Function to generate code using the model
|
28 |
def generate_code(prompt, max_length=150):
|
29 |
+
try:
|
30 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
31 |
+
outputs = model.generate(
|
32 |
+
**inputs,
|
33 |
+
max_length=max_length,
|
34 |
+
num_return_sequences=1,
|
35 |
+
temperature=0.7, # Adjust temperature for creativity
|
36 |
+
top_k=50, # Limit sampling to top k tokens
|
37 |
+
)
|
38 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
39 |
+
except Exception as e:
|
40 |
+
raise RuntimeError(f"Error during code generation: {e}")
|
41 |
|
42 |
# Streamlit app layout
|
43 |
st.title("SQLCoder: AI-Powered SQL Code Generator")
|
44 |
st.write("Generate SQL queries and code snippets using the SQLCoder model.")
|
45 |
|
46 |
+
# Input prompt
|
47 |
prompt = st.text_area("Enter your query or code prompt:", height=150)
|
48 |
max_length = st.slider("Maximum Output Length", min_value=50, max_value=300, value=150, step=10)
|
49 |
|