import logging import gradio as gr from nlp import NLPToSQL from db import Database nlp = NLPToSQL() db = Database() logging.basicConfig(level=logging.INFO) def chatbot(user_query): try: sql = nlp.query_to_sql(user_query) logging.info(f"Generated SQL: {sql}") result = db.execute_query(sql) if 'error' in result: return f"❌ Error: {result['error']}" if not result['data']: return "⚠️ No data found." # Formatting SQL output response = f"**Query:**\n```\n{sql}\n```\n\n" response += "**Result:**\n" response += "
{col} | " response += "
---|
{cell} | " response += "
{{ response }}# {% endif %} # {% if error %} #
{{ error }}
# {% endif %} # # # """ # @app.route("/", methods=["GET", "POST"]) # def index(): # if request.method == 'POST': # user_query = request.form['query'] # try: # sql = nlp.query_to_sql(user_query) # logging.info(f"Generated SQL: {sql}") # result = db.execute_query(sql) # if 'error' in result: # return render_template_string(HTML_TEMPLATE, error=result['error']) # if not result['data']: # return render_template_string(HTML_TEMPLATE, error="No data found") # response = " | ".join(result['columns']) + "\n" # response += "-"*50 + "\n" # for row in result['data']: # response += " | ".join(str(cell) for cell in row) + "\n" # return render_template_string(HTML_TEMPLATE, response=response) # except Exception as e: # logging.error(f"Error: {str(e)}") # return render_template_string(HTML_TEMPLATE, error=f"Error: {str(e)}") # return render_template_string(HTML_TEMPLATE) # if __name__ == "__main__": # app.run(debug=True)