Spaces:
Runtime error
Runtime error
barathm111
commited on
Commit
•
6d901c3
1
Parent(s):
0237617
Upload 5 files
Browse files- Dockerfile +33 -0
- app.py +83 -0
- requirements.txt +9 -0
- static/script.js +51 -0
- templates/index.html +59 -0
Dockerfile
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use an official Python runtime as the parent image
|
2 |
+
FROM python:3.9-slim
|
3 |
+
|
4 |
+
# Set the working directory in the container
|
5 |
+
WORKDIR /app
|
6 |
+
|
7 |
+
# Copy the Python requirements file into the container
|
8 |
+
COPY requirements.txt .
|
9 |
+
|
10 |
+
# Install any needed packages specified in requirements.txt
|
11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
12 |
+
|
13 |
+
# Copy the backend Python script
|
14 |
+
COPY app.py .
|
15 |
+
|
16 |
+
# Create directories for templates and static files
|
17 |
+
RUN mkdir templates static
|
18 |
+
|
19 |
+
# Copy the HTML template
|
20 |
+
COPY templates/index.html templates/
|
21 |
+
|
22 |
+
# Copy the JavaScript file
|
23 |
+
COPY static/script.js static/
|
24 |
+
|
25 |
+
# Make port 8000 available to the world outside this container
|
26 |
+
EXPOSE 8000
|
27 |
+
|
28 |
+
# Define environment variable
|
29 |
+
ENV FLASK_APP=app.py
|
30 |
+
ENV FLASK_RUN_HOST=0.0.0.0
|
31 |
+
|
32 |
+
# Run app.py when the container launches
|
33 |
+
CMD ["flask", "run", "--port=8000"]
|
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from flask import Flask, request, jsonify, render_template
|
3 |
+
from transformers import pipeline
|
4 |
+
import mysql.connector
|
5 |
+
from groq import Groq
|
6 |
+
|
7 |
+
app = Flask(name)
|
8 |
+
|
9 |
+
# Initialize the text generation pipeline
|
10 |
+
pipe = pipeline("text-generation", model="defog/sqlcoder-7b-2")
|
11 |
+
|
12 |
+
# Initialize the Groq client
|
13 |
+
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
14 |
+
|
15 |
+
# Database connection details
|
16 |
+
DB_CONFIG = {
|
17 |
+
'host': 'auth-db579.hstgr.io',
|
18 |
+
'user': 'u121769371_ki_aiml_test',
|
19 |
+
'password': os.environ.get("DB_PASSWORD"),
|
20 |
+
'database': 'u121769371_ki_aiml_test'
|
21 |
+
}
|
22 |
+
|
23 |
+
def generate_sql(text):
|
24 |
+
output = pipe(text, max_new_tokens=50)
|
25 |
+
return output[0]['generated_text']
|
26 |
+
|
27 |
+
def execute_query(query):
|
28 |
+
try:
|
29 |
+
connection = mysql.connector.connect(**DB_CONFIG)
|
30 |
+
cursor = connection.cursor()
|
31 |
+
cursor.execute(query)
|
32 |
+
results = cursor.fetchall()
|
33 |
+
cursor.close()
|
34 |
+
connection.close()
|
35 |
+
return results
|
36 |
+
except mysql.connector.Error as err:
|
37 |
+
print(f"Error: {err}")
|
38 |
+
return None
|
39 |
+
|
40 |
+
@app.route('/')
|
41 |
+
def index():
|
42 |
+
return render_template('index.html')
|
43 |
+
|
44 |
+
@app.route('/chatbot', methods=['POST'])
|
45 |
+
def chatbot():
|
46 |
+
data = request.json
|
47 |
+
user_query = data.get('text')
|
48 |
+
|
49 |
+
if not user_query:
|
50 |
+
return jsonify({"error": "No query provided"}), 400
|
51 |
+
|
52 |
+
try:
|
53 |
+
# Step 1: Convert natural language to SQL
|
54 |
+
sql_query = generate_sql(user_query)
|
55 |
+
|
56 |
+
# Step 2: Execute SQL query
|
57 |
+
query_result = execute_query(sql_query)
|
58 |
+
|
59 |
+
if query_result is None:
|
60 |
+
return jsonify({"error": "Database query execution failed"}), 500
|
61 |
+
|
62 |
+
# Step 3: Generate natural language response using Groq
|
63 |
+
prompt = f"Original query: {user_query}\nSQL query: {sql_query}\nQuery result: {query_result}\nPlease provide a natural language summary of the query result."
|
64 |
+
|
65 |
+
chat_completion = groq_client.chat.completions.create(
|
66 |
+
messages=[
|
67 |
+
{
|
68 |
+
"role": "user",
|
69 |
+
"content": prompt,
|
70 |
+
}
|
71 |
+
],
|
72 |
+
model="llama3-8b-8192",
|
73 |
+
)
|
74 |
+
|
75 |
+
natural_language_response = chat_completion.choices[0].message.content
|
76 |
+
|
77 |
+
return jsonify({"response": natural_language_response})
|
78 |
+
|
79 |
+
except Exception as e:
|
80 |
+
return jsonify({"error": str(e)}), 500
|
81 |
+
|
82 |
+
if name == 'main':
|
83 |
+
app.run(host='0.0.0.0', port=8000)
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.74.*
|
2 |
+
uvicorn[standard]==0.17.*
|
3 |
+
requests==2.27.*
|
4 |
+
transformers==4.*
|
5 |
+
torch==1.11.*
|
6 |
+
numpy<2.0.0
|
7 |
+
mysql-connector-python==8.0.*
|
8 |
+
groq==0.3.*
|
9 |
+
pydantic==1.9.*
|
static/script.js
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
document.addEventListener('DOMContentLoaded', (event) => {
|
2 |
+
const chatMessages = document.getElementById('chat-messages');
|
3 |
+
const userInput = document.getElementById('user-input');
|
4 |
+
const sendButton = document.getElementById('send-button');
|
5 |
+
|
6 |
+
function addMessage(message, isUser = false) {
|
7 |
+
const messageElement = document.createElement('p');
|
8 |
+
messageElement.textContent = message;
|
9 |
+
messageElement.style.backgroundColor = isUser ? '#e6f2ff' : '#f0f0f0';
|
10 |
+
messageElement.style.padding = '10px';
|
11 |
+
messageElement.style.borderRadius = '5px';
|
12 |
+
messageElement.style.marginBottom = '10px';
|
13 |
+
chatMessages.appendChild(messageElement);
|
14 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
15 |
+
}
|
16 |
+
|
17 |
+
function sendMessage() {
|
18 |
+
const message = userInput.value.trim();
|
19 |
+
if (message) {
|
20 |
+
addMessage(You: ${message}, true);
|
21 |
+
userInput.value = '';
|
22 |
+
|
23 |
+
fetch('/chatbot', {
|
24 |
+
method: 'POST',
|
25 |
+
headers: {
|
26 |
+
'Content-Type': 'application/json',
|
27 |
+
},
|
28 |
+
body: JSON.stringify({ text: message }),
|
29 |
+
})
|
30 |
+
.then(response => response.json())
|
31 |
+
.then(data => {
|
32 |
+
if (data.error) {
|
33 |
+
addMessage(Error: ${data.error});
|
34 |
+
} else {
|
35 |
+
addMessage(Chatbot: ${data.response});
|
36 |
+
}
|
37 |
+
})
|
38 |
+
.catch((error) => {
|
39 |
+
console.error('Error:', error);
|
40 |
+
addMessage('Error: Unable to get response from the server.');
|
41 |
+
});
|
42 |
+
}
|
43 |
+
}
|
44 |
+
|
45 |
+
sendButton.addEventListener('click', sendMessage);
|
46 |
+
userInput.addEventListener('keypress', function (e) {
|
47 |
+
if (e.key === 'Enter') {
|
48 |
+
sendMessage();
|
49 |
+
}
|
50 |
+
});
|
51 |
+
});
|
templates/index.html
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8" />
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
6 |
+
<title>SQL Chatbot</title>
|
7 |
+
<style>
|
8 |
+
body {
|
9 |
+
font-family: Arial, sans-serif;
|
10 |
+
max-width: 800px;
|
11 |
+
margin: 0 auto;
|
12 |
+
padding: 20px;
|
13 |
+
background-color: #f0f0f0;
|
14 |
+
}
|
15 |
+
#chat-container {
|
16 |
+
background-color: white;
|
17 |
+
border-radius: 10px;
|
18 |
+
padding: 20px;
|
19 |
+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
20 |
+
}
|
21 |
+
#chat-messages {
|
22 |
+
height: 400px;
|
23 |
+
overflow-y: auto;
|
24 |
+
border: 1px solid #ddd;
|
25 |
+
padding: 10px;
|
26 |
+
margin-bottom: 20px;
|
27 |
+
}
|
28 |
+
#user-input {
|
29 |
+
width: 100%;
|
30 |
+
padding: 10px;
|
31 |
+
border: 1px solid #ddd;
|
32 |
+
border-radius: 5px;
|
33 |
+
margin-bottom: 10px;
|
34 |
+
}
|
35 |
+
#send-button {
|
36 |
+
background-color: #4caf50;
|
37 |
+
color: white;
|
38 |
+
border: none;
|
39 |
+
padding: 10px 20px;
|
40 |
+
text-align: center;
|
41 |
+
text-decoration: none;
|
42 |
+
display: inline-block;
|
43 |
+
font-size: 16px;
|
44 |
+
margin: 4px 2px;
|
45 |
+
cursor: pointer;
|
46 |
+
border-radius: 5px;
|
47 |
+
}
|
48 |
+
</style>
|
49 |
+
</head>
|
50 |
+
<body>
|
51 |
+
<div id="chat-container">
|
52 |
+
<h1>SQL Chatbot</h1>
|
53 |
+
<div id="chat-messages"></div>
|
54 |
+
<input type="text" id="user-input" placeholder="Ask a question..." />
|
55 |
+
<button id="send-button">Send</button>
|
56 |
+
</div>
|
57 |
+
<script src="{{ url_for('static', filename='script.js') }}"></script>
|
58 |
+
</body>
|
59 |
+
</html>
|