Spaces:
Sleeping
Sleeping
Commit ·
9181263
1
Parent(s): 3ee05c9
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import sqlite3
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
+
|
| 7 |
+
translator = pipeline(model= "saikiranmaddukuri/chat_to_sql0.17", max_new_tokens= 100)
|
| 8 |
+
|
| 9 |
+
## Function to read SQL data
|
| 10 |
+
def read_sql_query(sql, db):
|
| 11 |
+
conn = sqlite3.connect(db)
|
| 12 |
+
cur = conn.cursor()
|
| 13 |
+
cur.execute(sql)
|
| 14 |
+
rows = cur.fetchall()
|
| 15 |
+
for row in rows:
|
| 16 |
+
print(row)
|
| 17 |
+
conn.close()
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def process_text(input_text):
|
| 21 |
+
# Backend function to process the input text (replace with your actual processing logic)
|
| 22 |
+
processed_text = f"You entered: {input_text}"
|
| 23 |
+
return processed_text
|
| 24 |
+
|
| 25 |
+
# Streamlit app
|
| 26 |
+
def main():
|
| 27 |
+
st.title("Conversational AI: Text-2-SQL")
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
create_table_statement= "CREATE TABLE car_sales (Manufacturer text, Model text, Sales_in_thousands float, __year_resale_value float, Vehicle_type text, Price_in_thousands float, Engine_size float, Horsepower float, Wheelbase float, Width float, Length float, Curb_weight float, Fuel_capacity float, Fuel_efficiency float, Latest_Launch text, Power_perf_factor float)"
|
| 31 |
+
|
| 32 |
+
# Step 1: User input
|
| 33 |
+
user_query = st.text_input("Ask question and press Enter:")
|
| 34 |
+
|
| 35 |
+
# Step 2: Backend processing on Enter
|
| 36 |
+
if user_query:
|
| 37 |
+
text='question:{}. table:{}'.format(user_query,create_table_statement.replace("text", 'VARCHAR') )
|
| 38 |
+
generated_sql= translator([text])
|
| 39 |
+
generated_sql= generated_sql[0]['generated_text']
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
i_gen_query = st.text_area("AI Generated SQL", generated_sql)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# Step 4: Process edited text on Enter
|
| 47 |
+
if st.button("Enter") and i_gen_query:
|
| 48 |
+
final_output= read_sql_query(i_gen_query, "Cars_db.sqlite")
|
| 49 |
+
|
| 50 |
+
# Step 5: Display final output
|
| 51 |
+
st.write("Final Output:", final_output)
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
main()
|