Spaces:
No application file
No application file
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, render_template,send_from_directory
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
import logging
|
| 4 |
+
from query_ai import query_gemini
|
| 5 |
+
from knowledge_base import search_knowledge_base, save_to_knowledge_base
|
| 6 |
+
from prompts import get_prompt
|
| 7 |
+
import os
|
| 8 |
+
# from speech_to_text import recognize_speech
|
| 9 |
+
|
| 10 |
+
# Initialize Flask app
|
| 11 |
+
app = Flask(__name__, static_folder="Static")
|
| 12 |
+
CORS(app, resources={r"/*": {"origins": "*"}})
|
| 13 |
+
|
| 14 |
+
# Configure logging
|
| 15 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 16 |
+
|
| 17 |
+
@app.route("/")
|
| 18 |
+
def home():
|
| 19 |
+
# return "Chatbot is running"
|
| 20 |
+
return render_template("index.html")
|
| 21 |
+
|
| 22 |
+
@app.route('/chat', methods=['POST'])
|
| 23 |
+
def chat():
|
| 24 |
+
try:
|
| 25 |
+
data = request.json
|
| 26 |
+
user_input = data.get("message", "").strip()
|
| 27 |
+
query_type = data.get("format", "default")
|
| 28 |
+
|
| 29 |
+
if not user_input:
|
| 30 |
+
return jsonify({"response": "Please provide a valid message."})
|
| 31 |
+
|
| 32 |
+
# 🔹 Step 1: Check predefined prompts
|
| 33 |
+
prompt_response = get_prompt(user_input.lower()) or "Let me find an answer for you."
|
| 34 |
+
|
| 35 |
+
# 🔹 Step 2: Search the knowledge base
|
| 36 |
+
kb_response = search_knowledge_base(user_input)
|
| 37 |
+
|
| 38 |
+
# 🔹 Step 3: Construct final prompt
|
| 39 |
+
final_prompt = f"{prompt_response}\n\nUser Query: {user_input}"
|
| 40 |
+
if kb_response:
|
| 41 |
+
final_prompt = f"{prompt_response}\n\nBased on the knowledge base: {kb_response}\n\nUser Query: {user_input}"
|
| 42 |
+
|
| 43 |
+
# 🔹 Step 4: Generate AI response
|
| 44 |
+
response = query_gemini(final_prompt, query_type=query_type)
|
| 45 |
+
|
| 46 |
+
# 🔹 Step 5: Save response if it's new
|
| 47 |
+
if not kb_response:
|
| 48 |
+
save_to_knowledge_base(user_input, response)
|
| 49 |
+
|
| 50 |
+
return jsonify({"response": response})
|
| 51 |
+
|
| 52 |
+
except Exception as e:
|
| 53 |
+
logging.error(f"Error in /chat endpoint: {e}")
|
| 54 |
+
return jsonify({"response": "An error occurred while processing your request."})
|
| 55 |
+
|
| 56 |
+
# @app.route("/script/chatbot.js")
|
| 57 |
+
# def chatbot_script():
|
| 58 |
+
# return send_from_directory("static", "chatbot.js")
|
| 59 |
+
|
| 60 |
+
@app.route('/speech', methods=['GET'])
|
| 61 |
+
def speech_to_text():
|
| 62 |
+
"""Endpoint to recognize speech and return text."""
|
| 63 |
+
text = recognize_speech()
|
| 64 |
+
return jsonify({"recognized_text": text})
|
| 65 |
+
|
| 66 |
+
if __name__ == '__main__':
|
| 67 |
+
# port = int(os.environ.get("PORT", 10000)) # Use Render's default port
|
| 68 |
+
app.run(host="0.0.0.0", debug=True)
|