Spaces:
Build error
Build error
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667from dotenv import load_dotenvimport streamlit as stimport osimport sqlite3import google.generativeai as genaiimport warningswarnings.filterwarnings("ignore")# Load all the environment variablesload_dotenv()# Configure genAI keygenai.configure(api_key=os.getenv("GOOGLE_API_KEY"))# Create a function to load the Gemini model and get a responsedef get_gemini_response(query, prompt):model = genai.GenerativeModel("gemini-pro")response = model.generate_content([prompt[0], query])print(response.text)return response.text# Function to retrieve query from the databasedef read_sql_query(sql, db):try:connection = sqlite3.connect(db)cursor = connection.cursor()cursor.execute(sql)rows = cursor.fetchall()connection.commit()for row in rows:print(row)except sqlite3.Error as e:# st.error(f"An error occurred: {e}")rows = []finally:connection.close()return rows# Define the promptprompt = ["""You are an expert in converting English questions to SQL code!The SQL database is named STUDENT and has the following columns: Name, Class, Section.For example,Example 1: How many entries of records are present?The SQL command will be something like this: SELECT COUNT(*) FROM STUDENT;Ensure that the SQL code in the response does not have ''' at the beginning or end of the response and does not contain the word 'sql' in the output."""]# Streamlit app configurationst.set_page_config(page_title="Data Retrieval from SQL Database Using gemini-pro")st.header("Data Retrieval from SQL Database Using gemini-pro")question = st.text_input("Input your question:", key='input')submit = st.button("Submit")# If submit is clickedif submit:response = get_gemini_response(question, prompt)if response:results = read_sql_query(response, "Student.db")st.subheader("Output:")if results:for row in results:st.write(row)else:st.write("No results found or an error occurred.")12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667from dotenv import load_dotenvimport streamlit as stimport osimport sqlite3import google.generativeai as genaiimport warningswarnings.filterwarnings("ignore")# Load all the environment variablesload_dotenv()# Configure genAI keygenai.configure(api_key=os.getenv("GOOGLE_API_KEY"))# Create a function to load the Gemini model and get a responsedef get_gemini_response(query, prompt):model = genai.GenerativeModel("gemini-pro")response = model.generate_content([prompt[0], query])print(response.text)return response.text# Function to retrieve query from the databasedef read_sql_query(sql, db):try:connection = sqlite3.connect(db)cursor = connection.cursor()cursor.execute(sql)rows = cursor.fetchall()connection.commit()for row in rows:print(row)except sqlite3.Error as e:# st.error(f"An error occurred: {e}")rows = []finally:connection.close()return rows# Define the promptprompt = ["""You are an expert in converting English questions to SQL code!The SQL database is named STUDENT and has the following columns: Name, Class, Section.For example,Example 1: How many entries of records are present?The SQL command will be something like this: SELECT COUNT(*) FROM STUDENT;Ensure that the SQL code in the response does not have ''' at the beginning or end of the response and does not contain the word 'sql' in the output."""]# Streamlit app configurationst.set_page_config(page_title="RetrieveGPT")st.header("Retrieve_GPT")question = st.text_input("Input your question:", key='input')submit = st.button("Submit")# If submit is clickedif submit:response = get_gemini_response(question, prompt)if response:results = read_sql_query(response, "Student.db")st.subheader("Output:")if results:for row in results:st.write(row)else:st.write("No results found or an error occurred.")
app.py+67-67