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