Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ from langchain_core.output_parsers import StrOutputParser
|
|
| 9 |
api_key = "AIzaSyBuhWQHGfMQewIY_gdnmbzzYoori6faeUo" # Store in .streamlit/secrets.toml
|
| 10 |
|
| 11 |
# Database options
|
| 12 |
-
db_options = ["MySQL", "PostgreSQL", "SQLite", "MSSQL", "Oracle", "MongoDB", "Firebase"]
|
| 13 |
|
| 14 |
# Function to read PDF files
|
| 15 |
def read_pdf(file):
|
|
@@ -23,28 +23,29 @@ def read_docx(file):
|
|
| 23 |
text = "\n".join([para.text for para in doc.paragraphs])
|
| 24 |
return text
|
| 25 |
|
| 26 |
-
# System prompt for SQL and
|
| 27 |
prompt_template = ChatPromptTemplate.from_messages([
|
| 28 |
("system", """
|
| 29 |
-
You are an expert in database problem solving. Given a
|
| 30 |
-
provide the most efficient solution using SQL or
|
| 31 |
Ensure the query follows best practices, is optimized for performance, and works across multiple RDBMS like MySQL, PostgreSQL, SQLite, MSSQL, and Oracle.
|
| 32 |
Additionally, support NoSQL databases such as MongoDB and Firebase by providing appropriate queries.
|
|
|
|
| 33 |
|
| 34 |
Output format:
|
| 35 |
-
**
|
| 36 |
-
```sql or
|
| 37 |
-
--
|
| 38 |
```
|
| 39 |
|
| 40 |
**Explanation:**
|
| 41 |
-
- Step-by-step breakdown of the
|
| 42 |
- Performance considerations and optimizations if applicable.
|
| 43 |
-
- Compatibility notes for the selected
|
| 44 |
"""),
|
| 45 |
("human", """
|
| 46 |
-
|
| 47 |
-
Selected
|
| 48 |
""")
|
| 49 |
])
|
| 50 |
|
|
@@ -52,8 +53,8 @@ chat_model = ChatGoogleGenerativeAI(google_api_key=api_key, model='models/gemini
|
|
| 52 |
parser = StrOutputParser()
|
| 53 |
chain = prompt_template | chat_model | parser
|
| 54 |
|
| 55 |
-
def
|
| 56 |
-
"""Generates an AI-powered
|
| 57 |
try:
|
| 58 |
response = chain.invoke({"sql_problem": sql_problem, "db_type": db_type})
|
| 59 |
return response if response else "No solution available."
|
|
@@ -61,12 +62,12 @@ def solve_sql_problem(sql_problem, db_type):
|
|
| 61 |
return f"Error: {str(e)}"
|
| 62 |
|
| 63 |
# Streamlit UI
|
| 64 |
-
st.title("🛠️ AI-Powered Database Problem Solver")
|
| 65 |
-
st.write("Enter a
|
| 66 |
|
| 67 |
col1, col2 = st.columns(2)
|
| 68 |
with col1:
|
| 69 |
-
sql_problem = st.text_area("
|
| 70 |
uploaded_file = st.file_uploader("Upload a DOCX or PDF file", type=["pdf", "docx"])
|
| 71 |
if uploaded_file is not None:
|
| 72 |
if uploaded_file.type == "application/pdf":
|
|
@@ -76,11 +77,11 @@ with col1:
|
|
| 76 |
st.success("File uploaded successfully!")
|
| 77 |
|
| 78 |
with col2:
|
| 79 |
-
db_type = st.selectbox("Select
|
| 80 |
|
| 81 |
if st.button("Generate Solution") and sql_problem:
|
| 82 |
with st.spinner("Generating solution..."):
|
| 83 |
-
|
| 84 |
|
| 85 |
-
st.subheader("AI-Generated
|
| 86 |
-
st.markdown(
|
|
|
|
| 9 |
api_key = "AIzaSyBuhWQHGfMQewIY_gdnmbzzYoori6faeUo" # Store in .streamlit/secrets.toml
|
| 10 |
|
| 11 |
# Database options
|
| 12 |
+
db_options = ["MySQL", "PostgreSQL", "SQLite", "MSSQL", "Oracle", "MongoDB", "Firebase", "Pandas"]
|
| 13 |
|
| 14 |
# Function to read PDF files
|
| 15 |
def read_pdf(file):
|
|
|
|
| 23 |
text = "\n".join([para.text for para in doc.paragraphs])
|
| 24 |
return text
|
| 25 |
|
| 26 |
+
# System prompt for SQL, NoSQL, and Pandas problem solving
|
| 27 |
prompt_template = ChatPromptTemplate.from_messages([
|
| 28 |
("system", """
|
| 29 |
+
You are an expert in database and data analysis problem solving. Given a problem statement,
|
| 30 |
+
provide the most efficient solution using SQL, NoSQL, or Pandas depending on the selected system.
|
| 31 |
Ensure the query follows best practices, is optimized for performance, and works across multiple RDBMS like MySQL, PostgreSQL, SQLite, MSSQL, and Oracle.
|
| 32 |
Additionally, support NoSQL databases such as MongoDB and Firebase by providing appropriate queries.
|
| 33 |
+
If Pandas is selected, provide optimized Python code using Pandas functions.
|
| 34 |
|
| 35 |
Output format:
|
| 36 |
+
**Solution:**
|
| 37 |
+
```sql, NoSQL, or Python
|
| 38 |
+
-- Query or Pandas code
|
| 39 |
```
|
| 40 |
|
| 41 |
**Explanation:**
|
| 42 |
+
- Step-by-step breakdown of the logic.
|
| 43 |
- Performance considerations and optimizations if applicable.
|
| 44 |
+
- Compatibility notes for the selected system.
|
| 45 |
"""),
|
| 46 |
("human", """
|
| 47 |
+
Problem Statement: {sql_problem}
|
| 48 |
+
Selected System: {db_type}
|
| 49 |
""")
|
| 50 |
])
|
| 51 |
|
|
|
|
| 53 |
parser = StrOutputParser()
|
| 54 |
chain = prompt_template | chat_model | parser
|
| 55 |
|
| 56 |
+
def solve_problem(sql_problem, db_type):
|
| 57 |
+
"""Generates an AI-powered solution based on the selected system."""
|
| 58 |
try:
|
| 59 |
response = chain.invoke({"sql_problem": sql_problem, "db_type": db_type})
|
| 60 |
return response if response else "No solution available."
|
|
|
|
| 62 |
return f"Error: {str(e)}"
|
| 63 |
|
| 64 |
# Streamlit UI
|
| 65 |
+
st.title("🛠️ AI-Powered Data & Database Problem Solver")
|
| 66 |
+
st.write("Enter a problem statement, select the system type (SQL, NoSQL, or Pandas), and AI will generate an optimized solution for you.")
|
| 67 |
|
| 68 |
col1, col2 = st.columns(2)
|
| 69 |
with col1:
|
| 70 |
+
sql_problem = st.text_area("Problem Statement", placeholder="Describe your problem here...")
|
| 71 |
uploaded_file = st.file_uploader("Upload a DOCX or PDF file", type=["pdf", "docx"])
|
| 72 |
if uploaded_file is not None:
|
| 73 |
if uploaded_file.type == "application/pdf":
|
|
|
|
| 77 |
st.success("File uploaded successfully!")
|
| 78 |
|
| 79 |
with col2:
|
| 80 |
+
db_type = st.selectbox("Select System Type", db_options)
|
| 81 |
|
| 82 |
if st.button("Generate Solution") and sql_problem:
|
| 83 |
with st.spinner("Generating solution..."):
|
| 84 |
+
solution = solve_problem(sql_problem, db_type)
|
| 85 |
|
| 86 |
+
st.subheader("AI-Generated Solution")
|
| 87 |
+
st.markdown(solution, unsafe_allow_html=True)
|