arjunanand13 commited on
Commit
af95c2b
·
verified ·
1 Parent(s): 360d2bc

Create house_excel_query.py

Browse files
Files changed (1) hide show
  1. house_excel_query.py +187 -0
house_excel_query.py ADDED
@@ -0,0 +1,187 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image, ImageFilter
3
+ import os
4
+ from txt2sql_code3 import SQLPromptModel
5
+ from qa_bot.qa_bot import QAInfer
6
+ from gradio import Button
7
+ import time
8
+
9
+ image = Image.open(os.path.join(os.path.abspath(''), "house_excel_sheet.png"))
10
+
11
+ def image_display(steps=0):
12
+ return image
13
+
14
+ query = None
15
+ rows = None
16
+ columns = None
17
+ user_choices = None
18
+ data = [
19
+ [1, "Buckingham Palace", 27, 34, 0.12, "London", "United Kingdom", 601, 920, 105000, "pdf_files/pdf/Buckingham_palace.pdf"],
20
+ [2, "White House", 355, 67, 0.08, "Washington D.C.", "United States", 601, 1527, 80000, "pdf_files/pdf/White_House.pdf"],
21
+ [3, "Taj Mahal Palace", 455, 76, 0.15, "Mumbai", "India", 795, 748, 67000, "pdf_files/pdf/Taj_Mahal_palace.pdf"],
22
+ [4, "Versailles Palace", 455, 45, 0.1, "Versailles", "France", 731, 1800, 145000, "pdf_files/pdf/Palace_of_Versailes.pdf"],
23
+ [5, "Villa Leopolda", 223, 21, 0.05, "Villefranche-sur-Mer", "France", 680, 6886, 65000, "pdf_files/pdf/Villa_Leopolda.pdf"],
24
+ [6, "Antilia", 455, 70, 0.46, "Mumbai", "India", 612, 2520, 179000, "pdf_files/pdf/Antilia.pdf"],
25
+ [7, "The Biltmore Estate", 544, 93, 0.2, "Asheville", "United States", 639, 2040, 50000, "pdf_files/pdf/Biltmore_Estate.pdf"],
26
+ [8, "Hearst Castle", 303, 57, 0.08, "San Simeon", "United States", 731, 1050, 71600, "pdf_files/pdf/Hearst_castle.pdf"],
27
+ [9, "Villa Les Cèdres", 489, 88, 0.09, "Saint-Jean-Cap-Ferrat", "France", 730, 1092, 100000, "pdf_files/pdf/Villa_Les_Cedres.pdf"],
28
+ [10, "Istana Nurul Iman", 350, 34, 7.46, "Bandar Seri Begawan", "Brunei", 670, 5403, 300000, "pdf_files/pdf/Istana_Nurul_Iman.pdf"]
29
+ ]
30
+ choices = [item[1] for item in data]
31
+
32
+
33
+
34
+ def execute_sql_query(input_prompt):
35
+ global query, rows, columns, user_choices
36
+
37
+ model_dir = "multi_table_demo/checkpoint-2600"
38
+ database = r"sql_pdf.db"
39
+ sql_model = SQLPromptModel(model_dir, database)
40
+
41
+ user_prompt = "Give complete details of properties in India"
42
+ for _ in range(3): # Retry logic, try 3 times
43
+ try:
44
+ table_schema = sql_model.fetch_table_schema("sql_pdf")
45
+ if table_schema:
46
+ if input_prompt.strip():
47
+ # query = sql_model.text2sql(table_schema, user_prompt, input_prompt)
48
+ # query = sql_model.text2sql_chatgpt(table_schema, user_prompt, input_prompt)
49
+ query = sql_model.text2sql_gemini(table_schema, user_prompt, input_prompt)
50
+ else:
51
+ # query = sql_model.text2sql(table_schema, user_prompt, user_prompt)
52
+ # query = sql_model.text2sql_chatgpt(table_schema, user_prompt, user_prompt)
53
+ query = sql_model.text2sql_gemini(table_schema, user_prompt, user_prompt)
54
+ rows, columns = sql_model.execute_query(query)
55
+ print(rows)
56
+ user_choices = []
57
+ for item in rows:
58
+ if isinstance(item[0], str):
59
+ user_choices.append(item[0])
60
+ else:
61
+ for val in item:
62
+ if isinstance(val, str):
63
+ user_choices.append(val)
64
+ break
65
+ return rows
66
+ else:
67
+ print("Table schema not found.")
68
+ return None
69
+ except Exception as e:
70
+ print(f"An error occurred: {e}")
71
+ print("Retrying...")
72
+ time.sleep(1) # Wait for 1 second before retrying
73
+ return None # Return None if all retries fail
74
+
75
+
76
+ # def qa_infer_interface(row,query_on_pdf):
77
+ # qa_infer=QAInfer()
78
+ # qa_infer.qa_infer(query,rows,columns)
79
+ user_choices = None
80
+ def update_choices(nothing):
81
+ print("callback called")
82
+ print("choices",choices)
83
+ print("user_choices",user_choices)
84
+ if user_choices:
85
+ examples = [[user_choices[0], "Structure of the property"], [user_choices[0], "Property History"], [user_choices[0], "How many floors does the property have"]]
86
+ return gr.Dropdown(choices=user_choices, label="Property Choice",info="List of all properties",interactive=True)#,examples
87
+ else:
88
+ return gr.Dropdown(choices=[], label="Property Choice",info="List of all properties",interactive=True)
89
+
90
+ def update_examples(nothing):
91
+ if user_choices:
92
+ examples = [[user_choices[0], "Structure of the property"], [user_choices[0], "Property History"], [user_choices[0], "How many floors does the property have"]]
93
+ return examples
94
+ else :
95
+ examples=[["","Structure of the property "],[ ""," Property History "] ,["", " How many floors does the property have"]],
96
+
97
+
98
+ def qa_infer_interface(property_choice, query_question):
99
+ qa_infer = QAInfer()
100
+ if not property_choice and user_choices:
101
+ property_choice = user_choices[0]
102
+ property_row = [row for row in data if row[1] == property_choice][0] # Find the row corresponding to the selected property
103
+ if not query_question:
104
+ query_question = "Explain the structure of the property"
105
+ retries = 3
106
+ while retries > 0:
107
+ try:
108
+ print(property_row)
109
+ answer = qa_infer.qa_infer_interface(property_row, query_question)
110
+ return answer
111
+ except Exception as e:
112
+ print(f"Error occurred while inferring QA: {e}")
113
+ retries -= 1
114
+ print("Failed to infer QA after 3 retries.")
115
+ return None
116
+
117
+
118
+
119
+ user_dropdown=gr.Dropdown(choices=[], label="Property Choice",info="List of all properties")
120
+ properties_text=gr.components.Textbox(lines=2,label="User Database Query",placeholder="Click on an query from 'examples' below or write your own query based on the database above. Default : 'Properties in India'")
121
+ interface_1_output=gr.Json(label="json")
122
+ stage2_examples=[["","Structure of the property "],[ ""," Property History "] ,["", " How many floors does the property have"]]
123
+ stage2_text=gr.components.Textbox(lines=2,label="Question on property",placeholder="Enter a question to know more about the properties , you can choose from one of the options below or write a own question Default: 'Explain the structure of the property'",)
124
+ stage2_output="text"
125
+
126
+ with gr.Blocks(title="House Excel Query") as demo:
127
+
128
+ gr.Markdown("# House Excel Query")
129
+
130
+ generated_image = image_display()
131
+ gr.Image(generated_image)
132
+
133
+ gr.Markdown("""### The database provided contains information about different properties, including their fundamental details. Additional specifics about each property are stored in associated PDF files, which are referenced in the "PDF" column. You have the capability to query this database using various criteria. When a query is initiated, the system generates SQL queries and extracts relevant rows from the database in the backend.
134
+ \n ### Once the properties are retrieved based on the query, you can utilize the user interface (UI) below to perform question answering (QA). Simply select a property from the list of returned properties and compose a question pertaining to that property. You will receive an answer based on the available information.""")
135
+
136
+ interface_1 = gr.Interface(
137
+ execute_sql_query,
138
+ inputs=properties_text,
139
+ # "textbox",
140
+ outputs=interface_1_output,
141
+ # live=True,
142
+ # cache_examples=["Give me all details of properties from India"],
143
+ examples=["Properties in France "," Properties greater than a acre","Properties with more than 400 bedrooms"],
144
+ )
145
+ # print(interface_1.input_components[0])
146
+
147
+
148
+ interface_2 = gr.Interface(
149
+ qa_infer_interface,
150
+ inputs=[user_dropdown,stage2_text],
151
+ # inputs=[gr.Dropdown.change(fn=update_choices),gr.components.Textbox(lines=2,label="Question on property",placeholder="Enter a question to know more about the properties")],
152
+ outputs=stage2_output,
153
+ # examples=stage2_examples,
154
+ # live=True,
155
+ # gr.Button("Next"),
156
+ # Button.click(next,value="Next"),
157
+
158
+ )
159
+
160
+ gr.Examples(["How many floors does the property have "," Total square feet of the property " ," Total area of the property"],inputs=stage2_text,outputs=stage2_output,fn=qa_infer_interface)
161
+
162
+
163
+ properties_text.change(update_choices,inputs=[properties_text],outputs=[user_dropdown])
164
+ interface_1_output.change(update_choices,inputs=[interface_1_output],outputs=[user_dropdown])
165
+
166
+
167
+ # user_dropdown.change(update_examples, inputs=[user_dropdown], outputs=[stage2_examples])
168
+
169
+ # properties_text.change(update_choices,inputs=[stage2_examples],outputs=[interface_2.examples])
170
+ # interface_1_output.change(update_choices,inputs=[stage2_examples],outputs=[interface_2.examples])
171
+
172
+ # user_dropdown.change(update_choices, inputs=[user_dropdown], outputs=[user_dropdown, interface_2])
173
+
174
+ # user_dropdown.change(fn=update_choices,inputs=[user_dropdown],outputs=[user_dropdown])
175
+
176
+ # with gr.Row():
177
+ # save_btn = gr.Button("Next")
178
+
179
+ # Button.click(next,value="Next",),
180
+
181
+ if __name__ == "__main__":
182
+ demo.launch(share=True)
183
+
184
+
185
+ ## download pdf button
186
+ ## upload pdf
187
+ ## dynamic selection