barathm111 commited on
Commit
035476a
1 Parent(s): 283a0f0

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -17
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from fastapi import FastAPI, HTTPException
2
  from pydantic import BaseModel
3
  from transformers import pipeline
4
  import mysql.connector
@@ -67,12 +67,20 @@ def get_database_schema():
67
  def home():
68
  return {"message": "SQL Generation Server is running"}
69
 
70
- @app.post("/query")
71
- def handle_query(request: QueryRequest):
72
  try:
73
- print("Received query:", request.query) # Debugging: Print the received query
74
- text = request.query
 
 
 
75
 
 
 
 
 
 
76
  # Fetch the database schema
77
  schema = get_database_schema()
78
  schema_str = json.dumps(schema, indent=4)
@@ -85,19 +93,19 @@ def handle_query(request: QueryRequest):
85
  {schema_str}
86
  When creating your answers, consider the following:
87
 
88
- 1. If a query involves a column or value that is not present in the provided database schema, correct it and mention the correction in the summary. If a column or value is missing, provide an explanation of the issue and adjust the query accordingly.
89
- 2. If there is a spelling mistake in the column name or value, attempt to correct it by matching the closest possible column or value from the schema. Mention this correction in the summary to clarify any changes made.
90
- 3. Ensure that the correct columns and values are used based on the schema provided. Verify the query against the schema to confirm accuracy.
91
- 4. Include column name headers in the query results for clarity.
92
 
93
- Always provide your answer in the JSON format below:
94
 
95
- {{ "summary": "your-summary", "query": "your-query" }}
96
-
97
- Output ONLY JSON.
98
- In the preceding JSON response, substitute "your-query" with a MariaDB query to retrieve the requested data.
99
- In the preceding JSON response, substitute "your-summary" with a summary of the query and any corrections or clarifications made.
100
- Always include all columns in the table.
101
  """
102
 
103
  prompt = f"{system_message}\n\nUser request:\n\n{text}\n\nSQL query:"
@@ -123,7 +131,6 @@ def handle_query(request: QueryRequest):
123
  print("Error occurred:", str(e)) # Debugging: Print the error
124
  raise HTTPException(status_code=500, detail=str(e))
125
 
126
-
127
  if __name__ == "__main__":
128
  import uvicorn
129
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ from fastapi import FastAPI, HTTPException, Request
2
  from pydantic import BaseModel
3
  from transformers import pipeline
4
  import mysql.connector
 
67
  def home():
68
  return {"message": "SQL Generation Server is running"}
69
 
70
+ @app.api_route("/query", methods=["GET", "POST"])
71
+ async def handle_query(request: Request):
72
  try:
73
+ if request.method == "POST":
74
+ request_data = await request.json()
75
+ text = request_data.get("query", "")
76
+ elif request.method == "GET":
77
+ text = request.query_params.get("query", "")
78
 
79
+ print("Received query:", text) # Debugging: Print the received query
80
+
81
+ if not text:
82
+ raise ValueError("No query provided.")
83
+
84
  # Fetch the database schema
85
  schema = get_database_schema()
86
  schema_str = json.dumps(schema, indent=4)
 
93
  {schema_str}
94
  When creating your answers, consider the following:
95
 
96
+ 1. If a query involves a column or value that is not present in the provided database schema, correct it and mention the correction in the summary. If a column or value is missing, provide an explanation of the issue and adjust the query accordingly.
97
+ 2. If there is a spelling mistake in the column name or value, attempt to correct it by matching the closest possible column or value from the schema. Mention this correction in the summary to clarify any changes made.
98
+ 3. Ensure that the correct columns and values are used based on the schema provided. Verify the query against the schema to confirm accuracy.
99
+ 4. Include column name headers in the query results for clarity.
100
 
101
+ Always provide your answer in the JSON format below:
102
 
103
+ {{ "summary": "your-summary", "query": "your-query" }}
104
+
105
+ Output ONLY JSON.
106
+ In the preceding JSON response, substitute "your-query" with a MariaDB query to retrieve the requested data.
107
+ In the preceding JSON response, substitute "your-summary" with a summary of the query and any corrections or clarifications made.
108
+ Always include all columns in the table.
109
  """
110
 
111
  prompt = f"{system_message}\n\nUser request:\n\n{text}\n\nSQL query:"
 
131
  print("Error occurred:", str(e)) # Debugging: Print the error
132
  raise HTTPException(status_code=500, detail=str(e))
133
 
 
134
  if __name__ == "__main__":
135
  import uvicorn
136
  uvicorn.run(app, host="0.0.0.0", port=7860)