Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from flask import Flask, request, jsonify
|
3 |
+
from flask_cors import CORS
|
4 |
+
from scraper import WebScraper
|
5 |
+
from vector_db import VectorDatabase
|
6 |
+
from chatbot import ChatBot
|
7 |
+
import os
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
|
10 |
+
load_dotenv()
|
11 |
+
|
12 |
+
app = Flask(__name__)
|
13 |
+
CORS(app) # Enable CORS for all routes
|
14 |
+
|
15 |
+
# Initialize our components
|
16 |
+
scraper = WebScraper()
|
17 |
+
vector_db = VectorDatabase()
|
18 |
+
chatbot = ChatBot()
|
19 |
+
|
20 |
+
@app.route('/api/scrape', methods=['POST'])
|
21 |
+
def scrape_url():
|
22 |
+
try:
|
23 |
+
data = request.json
|
24 |
+
url = data.get('url')
|
25 |
+
if not url:
|
26 |
+
return jsonify({'error': 'URL is required'}), 400
|
27 |
+
|
28 |
+
# Scrape the URL
|
29 |
+
result = scraper.scrape_url(url)
|
30 |
+
if not result or not result['content']:
|
31 |
+
return jsonify({'error': 'Failed to scrape website'}), 400
|
32 |
+
|
33 |
+
# Process and store in vector database
|
34 |
+
chunks_count, vector_store_id = vector_db.process_and_store(result)
|
35 |
+
|
36 |
+
return jsonify({
|
37 |
+
'message': f'Successfully scraped and processed into {chunks_count} chunks',
|
38 |
+
'chunks': chunks_count,
|
39 |
+
'url': result['url']
|
40 |
+
})
|
41 |
+
except Exception as e:
|
42 |
+
return jsonify({'error': str(e)}), 500
|
43 |
+
|
44 |
+
@app.route('/api/chat', methods=['POST'])
|
45 |
+
def chat():
|
46 |
+
try:
|
47 |
+
data = request.json
|
48 |
+
message = data.get('message')
|
49 |
+
url = data.get('url') # Optional URL to specify context
|
50 |
+
|
51 |
+
if not message:
|
52 |
+
return jsonify({'error': 'Message is required'}), 400
|
53 |
+
|
54 |
+
# Get context from vector database
|
55 |
+
context = vector_db.search(message, url)
|
56 |
+
|
57 |
+
# Generate response
|
58 |
+
response = chatbot.generate_response(message, context, url)
|
59 |
+
|
60 |
+
return jsonify({'response': response})
|
61 |
+
except Exception as e:
|
62 |
+
return jsonify({'error': str(e)}), 500
|
63 |
+
|
64 |
+
if __name__ == '__main__':
|
65 |
+
app.run(host='0.0.0.0', port=7860)
|