mahedi420 commited on
Commit
23743a0
·
verified ·
1 Parent(s): a2a469a

create chatbot app

Browse files
Files changed (1) hide show
  1. app.py +158 -0
app.py ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+ load_dotenv() ## load all the environemnt variables
3
+ from extract_word import suggest_words
4
+ import streamlit as st
5
+ import os
6
+ import sqlite3
7
+ import pandas as pd
8
+ import re
9
+ import numpy as np
10
+ import Levenshtein
11
+
12
+ import google.generativeai as genai
13
+ ## Configure Genai Key
14
+
15
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
16
+
17
+ ## Function To Load Google Gemini Model and provide queries as response
18
+
19
+ def get_gemini_response(question,prompt):
20
+ model=genai.GenerativeModel('gemini-pro')
21
+ response=model.generate_content([prompt,question])
22
+ return response.text
23
+
24
+
25
+ def string_similarity(input_string, string_list):
26
+ similarities = []
27
+
28
+ for string in string_list:
29
+ distance = Levenshtein.distance(input_string, string)
30
+ similarity = 1 - distance / max(len(input_string), len(string))
31
+ similarities.append((string, similarity))
32
+
33
+ return similarities
34
+
35
+ def suggest_words(input_string, string_list, max_suggestions=5):
36
+ similarities = string_similarity(input_string, string_list)
37
+
38
+ # Sort the list of similarities by similarity score (descending order)
39
+ sorted_similarities = sorted(similarities, key=lambda x: x[1], reverse=True)
40
+
41
+ # Get the top N suggestions
42
+ suggestions = [string for string, _ in sorted_similarities[:max_suggestions]]
43
+
44
+ return suggestions
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+ ## Fucntion To retrieve query from the database
56
+
57
+ def read_sql_query(sql,db):
58
+ conn=sqlite3.connect(db)
59
+ cur=conn.cursor()
60
+ cur.execute(sql)
61
+ rows=cur.fetchall()
62
+ conn.commit()
63
+ conn.close()
64
+ for row in rows:
65
+ print(row)
66
+ return rows
67
+
68
+ ## Streamlit App
69
+ df= pd.DataFrame()
70
+ string_of_csv = []
71
+ st.set_page_config(page_title="I can Retrieve Any SQL query")
72
+ st.header("Data analysis by Chatbot")
73
+
74
+ uploaded_file = st.file_uploader("Upload a CSV file", type=["csv"], key="uploaded_file")
75
+
76
+ if uploaded_file is not None:
77
+ # Display the uploaded file
78
+ # st.write("Uploaded CSV file:", uploaded_file.name)
79
+
80
+ # Read the CSV file
81
+ df = pd.read_csv(uploaded_file)
82
+ df = df.dropna(axis=1, how="all")
83
+ df = df.dropna(axis=0, how="all")
84
+ df = df.fillna('nan')
85
+ object_columns = df.select_dtypes(include='object')
86
+ print(object_columns)
87
+ # Convert each object-type column to a Python list
88
+ lists_of_columns = [df[column].tolist() for column in object_columns.columns]
89
+ string_of_csv = [element for sublist in lists_of_columns for element in sublist]
90
+
91
+
92
+ df = df.applymap(lambda x: x.lower() if isinstance(x, str) else x)
93
+ df.columns = df.columns.str.replace(' ', '_')
94
+ df.columns = df.columns.str.replace('.', '')
95
+ # pattern = re.compile('[^a-zA-Z0-9]')
96
+ # # Use the sub() function to replace matched characters with an empty string
97
+ # df.columns = df.columns.map(lambda x: re.sub(pattern, '', x))
98
+ df.columns = df.columns.astype(str)
99
+ df.columns = df.columns.str.strip()
100
+
101
+ question = st.text_input("Enter a word prefix:", key="user_input")
102
+ # Display suggestions dynamically
103
+ suggestions = suggest_words(question.strip(), string_of_csv)
104
+ st.subheader("Suggestions:")
105
+ if suggestions:
106
+ for suggestion in suggestions:
107
+ st.write(suggestion)
108
+ else:
109
+ st.write("No suggestions found.")
110
+
111
+ question = question.lower()
112
+
113
+
114
+ # User input for the starting string
115
+
116
+
117
+
118
+
119
+ submit=st.button("Ask the question")
120
+
121
+ connection = sqlite3.connect('student.db')
122
+
123
+ df.to_sql('student', connection,if_exists='replace')
124
+ connection.commit()
125
+ connection.close()
126
+
127
+ ## Define Your Prompt
128
+ column_names = df.columns.tolist()
129
+ # print(column_names)
130
+ column = ', '.join(column_names)
131
+ prompt = f"""You are an expert in converting English questions to SQL query!
132
+ The SQL database has the name STUDENT and has the following columns - {column}
133
+ \n\n\nFor example: \n Example 1: Retrieve all the information about student ?, the sql query will be like
134
+ SELECT * FROM student;
135
+ \n Example 2: how many row in the student table ?, the sql query will be like
136
+ SELECT COUNT(*) as row_count FROM student;
137
+ \n Example 3: what is the unique name ?, the sql query will be like
138
+ SELECT DISTINCT name FROM student;
139
+ also the sql code should not have ``` in beginning or end and sql word in output
140
+ return only sql query
141
+ """
142
+ # if submit is clicked
143
+ if submit:
144
+ response=get_gemini_response(question,prompt)
145
+ print(response)
146
+ response=read_sql_query(response,"student.db")
147
+ print(response)
148
+ st.subheader("The REsponse is")
149
+ for row in response:
150
+ st.text(row)
151
+ # st.text(response)
152
+
153
+
154
+
155
+
156
+
157
+
158
+