Spaces:
Runtime error
Runtime error
File size: 5,038 Bytes
2ee6ec3 40a67fd 2ee6ec3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
from dotenv import load_dotenv
load_dotenv() ## load all the environment variables
import streamlit as st
import os
import sqlite3
import google.generativeai as genai
## Configure Genai Key
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
## Function To Load Google Gemini Model and provide queries as response
def get_gemini_response(question,prompt):
model=genai.GenerativeModel('gemini-pro')
response=model.generate_content([prompt[0],question])
return response.text
## Function To retrieve query from the database
def read_sql_query(sql,db):
conn=sqlite3.connect(db)
cur=conn.cursor()
cur.execute(sql)
rows=cur.fetchall()
conn.commit()
conn.close()
return rows
## Define Your Prompt
## Define Your Prompt
prompt=[
"""
You are an expert in converting English questions to SQL query!
Read The Prompt Carefully and try to understand What The User Needs read the input from user and spend good time Understanding that,
The SQL database has the name STUDENT and has the following columns - NAME, CLASS,marks,section,
SECTION \n\nFor example,\nExample 1 - How many entries of records are present?,
the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;
\nExample 2 - Tell me all the students studying in Data Science class?,
the SQL command will be something like this SELECT * FROM STUDENT
where CLASS="Data Science";
also the sql code should not have ``` in beginning or end and sql word in output
"""
]
## Streamlit App
st.set_page_config(page_title="Gemini SQL Assistant", page_icon=":gemini:")
st.title("๐ฎ Gemini SQL Assistant ๐")
st.sidebar.title("๐ Ask a Question ๐")
question = st.sidebar.text_input("Enter your question here:")
submit = st.sidebar.button("๐ Get SQL Query")
# Main content area
st.markdown("---")
if submit:
response = get_gemini_response(question, prompt)
st.subheader("๐ Generated SQL Query:")
st.code(response, language="sql")
# Execute SQL query
try:
rows = read_sql_query(response, "student.db")
if rows:
st.subheader("๐ Query Result:")
st.dataframe(rows)
else:
st.warning("โ No results found for this query.")
except Exception as e:
st.error(f"โ An error occurred: {e}")
# Main content area
st.write("""
๐ **About Gemini SQL Assistant**
Gemini SQL Assistant is an intelligent tool that helps you convert English questions into SQL queries.
Whether you're a beginner or an expert in SQL, Gemini can assist you in generating SQL queries to retrieve data from the STUDENT database.
Simply enter your question in the sidebar, click on the 'Get SQL Query' button, and Gemini will generate the corresponding SQL query for you.
You can then execute the query to retrieve the desired data from the database.
๐ง **How to Use**
1. Enter your question in the sidebar.
2. Click on the 'Get SQL Query' button.
3. View the generated SQL query in the main area.
4. Execute the query to retrieve data from the database.
5. Explore the query results and analyze the data as needed.
๐ **Example Questions**
- How many entries of records are present?
- Tell me all the students studying in Data Science class.
- Show me the students with marks above 80.
Feel free to ask any question related to the STUDENT database, and Gemini will assist you in generating the SQL query to retrieve the data.
๐ฉโ๐ป **Powered By**
Gemini ๐ซ - A powerful generative AI model developed by Google.
Streamlit ๐ - An open-source app framework for Machine Learning and Data Science.
""")
# Footer
footer_with_image_light_blue = """
<style>
.footer {
background-color: #f0f0f0;
padding: 20px;
text-align: center;
border-top: 1px solid #ccc;
}
.footer img {
max-width: 100%;
margin-top: 10px;
}
.footer .connect-text {
color: #333;
font-weight: bold;
margin-bottom: 10px;
}
.footer a {
margin: 0 10px;
color: #333;
}
.footer .powered-by {
color: #333;
font-size: 14px;
margin-top: 10px;
}
.bright-text {
color: #004D40;
}
</style>
<div class="footer">
<div class="connect-text">Connect with me at</div>
<a href="https://github.com/FasilHameed" target="_blank"><img src="https://img.icons8.com/plasticine/30/000000/github.png" alt="GitHub"></a>
<a href="https://www.linkedin.com/in/faisal--hameed/" target="_blank"><img src="https://img.icons8.com/plasticine/30/000000/linkedin.png" alt="LinkedIn"></a>
<a href="tel:+917006862681"><img src="https://img.icons8.com/plasticine/30/000000/phone.png" alt="Phone"></a>
<a href="mailto:faisalhameed763@gmail.com"><img src="https://img.icons8.com/plasticine/30/000000/gmail.png" alt="Gmail"></a>
<div class="powered-by">Powered By <img src="https://img.icons8.com/clouds/30/000000/gemini.png" alt="Gemini"> Gemini ๐ซ and Streamlit ๐</div>
</div>
"""
# Render Footer
st.markdown(footer_with_image_light_blue, unsafe_allow_html=True)
|