Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import PyPDF2
|
| 3 |
+
import docx
|
| 4 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 5 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 6 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 7 |
+
|
| 8 |
+
# Secure API key handling
|
| 9 |
+
api_key = "YOUR_GOOGLE_API_KEY" # 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):
|
| 16 |
+
reader = PyPDF2.PdfReader(file)
|
| 17 |
+
text = "\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
|
| 18 |
+
return text
|
| 19 |
+
|
| 20 |
+
# Function to read DOCX files
|
| 21 |
+
def read_docx(file):
|
| 22 |
+
doc = docx.Document(file)
|
| 23 |
+
text = "\n".join([para.text for para in doc.paragraphs])
|
| 24 |
+
return text
|
| 25 |
+
|
| 26 |
+
# System prompt for SQL and NoSQL problem solving
|
| 27 |
+
prompt_template = ChatPromptTemplate.from_messages([
|
| 28 |
+
("system", """
|
| 29 |
+
You are an expert in database problem solving. Given a database-related problem statement,
|
| 30 |
+
provide the most efficient solution using SQL or NoSQL depending on the selected database 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 |
+
|
| 34 |
+
Output format:
|
| 35 |
+
**Query:**
|
| 36 |
+
```sql or NoSQL
|
| 37 |
+
-- Database query
|
| 38 |
+
```
|
| 39 |
+
|
| 40 |
+
**Explanation:**
|
| 41 |
+
- Step-by-step breakdown of the query logic.
|
| 42 |
+
- Performance considerations and optimizations if applicable.
|
| 43 |
+
- Compatibility notes for the selected database.
|
| 44 |
+
"""),
|
| 45 |
+
("human", """
|
| 46 |
+
Database Problem: {sql_problem}
|
| 47 |
+
Selected Database: {db_type}
|
| 48 |
+
""")
|
| 49 |
+
])
|
| 50 |
+
|
| 51 |
+
chat_model = ChatGoogleGenerativeAI(google_api_key=api_key, model='models/gemini-2.0-flash-exp')
|
| 52 |
+
parser = StrOutputParser()
|
| 53 |
+
chain = prompt_template | chat_model | parser
|
| 54 |
+
|
| 55 |
+
def solve_sql_problem(sql_problem, db_type):
|
| 56 |
+
"""Generates an AI-powered database solution based on the selected database type."""
|
| 57 |
+
try:
|
| 58 |
+
response = chain.invoke({"sql_problem": sql_problem, "db_type": db_type})
|
| 59 |
+
return response if response else "No solution available."
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return f"Error: {str(e)}"
|
| 62 |
+
|
| 63 |
+
# Streamlit UI
|
| 64 |
+
st.title("🛠️ AI-Powered Database Problem Solver")
|
| 65 |
+
st.write("Enter a database-related problem statement, select the database type, and AI will generate an optimized solution for you.")
|
| 66 |
+
|
| 67 |
+
col1, col2 = st.columns(2)
|
| 68 |
+
with col1:
|
| 69 |
+
sql_problem = st.text_area("Database Problem Statement", placeholder="Describe your database problem here...")
|
| 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":
|
| 73 |
+
sql_problem = read_pdf(uploaded_file)
|
| 74 |
+
elif uploaded_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
| 75 |
+
sql_problem = read_docx(uploaded_file)
|
| 76 |
+
st.success("File uploaded successfully!")
|
| 77 |
+
|
| 78 |
+
with col2:
|
| 79 |
+
db_type = st.selectbox("Select Database Type", db_options)
|
| 80 |
+
|
| 81 |
+
if st.button("Generate Solution") and sql_problem:
|
| 82 |
+
with st.spinner("Generating solution..."):
|
| 83 |
+
sql_solution = solve_sql_problem(sql_problem, db_type)
|
| 84 |
+
|
| 85 |
+
st.subheader("AI-Generated Database Solution")
|
| 86 |
+
st.markdown(sql_solution, unsafe_allow_html=True)
|