mohan1869 commited on
Commit
ce3c914
·
1 Parent(s): 7821cce

Deploy SQLCoder with Streamlit

Browse files
Files changed (1) hide show
  1. streamlit_app.py +41 -0
streamlit_app.py CHANGED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Load the model and tokenizer
8
+ @st.cache_resource
9
+ def load_model():
10
+ model_name = "defog/sqlcoder-7b-2"
11
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
12
+ model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
13
+ return tokenizer, model
14
+
15
+ tokenizer, model = load_model()
16
+
17
+ # Define the function to generate code
18
+ def generate_code(prompt, max_length=150):
19
+ inputs = tokenizer(prompt, return_tensors="pt")
20
+ outputs = model.generate(**inputs, max_length=max_length, num_return_sequences=1)
21
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
22
+
23
+ # Streamlit app layout
24
+ st.title("SQLCoder: AI-Powered SQL Code Generator")
25
+ st.write("Generate SQL queries and code snippets using the SQLCoder model.")
26
+
27
+ # Input for user prompt
28
+ prompt = st.text_area("Enter your query or code prompt:", height=150)
29
+ max_length = st.slider("Maximum Output Length", min_value=50, max_value=300, value=150, step=10)
30
+
31
+ # Generate button
32
+ if st.button("Generate Code"):
33
+ if prompt.strip():
34
+ with st.spinner("Generating code..."):
35
+ try:
36
+ generated_code = generate_code(prompt, max_length=max_length)
37
+ st.text_area("Generated Code:", value=generated_code, height=200)
38
+ except Exception as e:
39
+ st.error(f"Error generating code: {e}")
40
+ else:
41
+ st.warning("Please enter a prompt before generating code.")