|
|
|
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) |
|
|
|
|
|
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 |
|
|
|
|
|
result = scraper.scrape_url(url) |
|
if not result or not result['content']: |
|
return jsonify({'error': 'Failed to scrape website'}), 400 |
|
|
|
|
|
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') |
|
|
|
if not message: |
|
return jsonify({'error': 'Message is required'}), 400 |
|
|
|
|
|
context = vector_db.search(message, url) |
|
|
|
|
|
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) |