Commit
•
c4fbb4c
1
Parent(s):
136acf1
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from langchain import HuggingFaceHub
|
3 |
+
from langchain.prompts import ChatPromptTemplate
|
4 |
+
from langchain_google_genai import ChatGoogleGenerativeAI, HarmBlockThreshold, HarmCategory
|
5 |
+
from langchain.chains.question_answering import load_qa_chain
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
import os
|
8 |
+
import json
|
9 |
+
import pandas as pd
|
10 |
+
import numpy as np
|
11 |
+
import sqlite3
|
12 |
+
|
13 |
+
|
14 |
+
load_dotenv()
|
15 |
+
|
16 |
+
conn = sqlite3.connect('your_database.db')
|
17 |
+
c = conn.cursor()
|
18 |
+
|
19 |
+
|
20 |
+
def get_database_schema():
|
21 |
+
c.execute("SELECT name FROM sqlite_master WHERE type='table';")
|
22 |
+
tables = c.fetchall()
|
23 |
+
schema_dict = {}
|
24 |
+
for table in tables:
|
25 |
+
c.execute(f"PRAGMA table_info({table[0]})")
|
26 |
+
schema = c.fetchall()
|
27 |
+
schema_dict[table[0]] = schema
|
28 |
+
return schema_dict
|
29 |
+
|
30 |
+
|
31 |
+
def get_chain():
|
32 |
+
template = """
|
33 |
+
You are a SQLite query generator.
|
34 |
+
return output executable query only or empyt text .
|
35 |
+
output should be json with key "query" and tabel name with key "table_name"
|
36 |
+
Based on user question, generate SQLite Query for given database Schema.
|
37 |
+
Schema : \n{context}\n
|
38 |
+
Question : {question}
|
39 |
+
"""
|
40 |
+
hf_model = HuggingFaceHub(repo_id="Mistralai/Mistralaichat", model_kwargs={"temperature": 0.4})
|
41 |
+
prompt=ChatPromptTemplate.from_template(template)
|
42 |
+
chain=LLMChain(llm=hf_model.load_chat(),prompt=prompt)
|
43 |
+
return chain
|
44 |
+
|
45 |
+
def get_query(txt1):
|
46 |
+
start_index = txt1.find('{')
|
47 |
+
end_index = txt1.find('}') + 1
|
48 |
+
json_string = txt1[start_index:end_index]
|
49 |
+
data = json.loads(json_string)
|
50 |
+
|
51 |
+
return data
|
52 |
+
|
53 |
+
|
54 |
+
def query_with_database(query, tablename=None):
|
55 |
+
if tablename:
|
56 |
+
c.execute(query)
|
57 |
+
else:
|
58 |
+
c.execute(query)
|
59 |
+
query_output = c.fetchall()
|
60 |
+
df = pd.DataFrame(query_output, columns=[desc[1] for desc in c.description])
|
61 |
+
st.table(df)
|
62 |
+
|
63 |
+
|
64 |
+
def user_input(user_question):
|
65 |
+
schema = get_database_schema()
|
66 |
+
chain = get_chain()
|
67 |
+
response = chain.run({"context": schema, "question": user_question})
|
68 |
+
generated_query = get_query(response)
|
69 |
+
st.write("Generated Query : ", generated_query["query"])
|
70 |
+
query_with_database(generated_query["query"])
|
71 |
+
|
72 |
+
|
73 |
+
def main():
|
74 |
+
st.header("Chat With SQLite DATABASE - HUGGING FACE MISTRALAI/MISTRALAICHAT")
|
75 |
+
with st.sidebar:
|
76 |
+
st.write("Develop by : Rehan Shekh")
|
77 |
+
st.write("Model : Hugging Face Mistralai/Mistralaichat")
|
78 |
+
buttoclick=st.text_input(label="Ask from Database")
|
79 |
+
st.write("Demo Database only have one table :- User_details")
|
80 |
+
if buttoclick:
|
81 |
+
|
82 |
+
user_input(str(buttoclick))
|
83 |
+
|
84 |
+
|
85 |
+
if __name__=="__main__":
|
86 |
+
main()
|