Skip to content

Commit

Permalink
feat: loggers + url cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
Jlarrinzal committed Dec 3, 2024
1 parent 7ca9aaf commit 368003d
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 239 deletions.
5 changes: 4 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from flask import Flask, send_from_directory
from dotenv import load_dotenv
from routes.routes import routes_bp
from logger_config import configure_logger

# os.environ.pop('WEAVIATE_API_KEY', None)
# os.environ.pop('URL_CLUSTER', None)
Expand All @@ -12,6 +13,8 @@
app = Flask(__name__, static_folder='static', static_url_path='')
app.config['UPLOAD_FOLDER'] = 'uploads'

configure_logger(app)

app.register_blueprint(routes_bp)

@app.route('/')
Expand All @@ -21,4 +24,4 @@ def serve_index():
if __name__ == '__main__':
if not os.path.exists(app.config['UPLOAD_FOLDER']):
os.makedirs(app.config['UPLOAD_FOLDER'])
app.run(host='0.0.0.0', debug=True, use_reloader=False)
app.run(host='0.0.0.0', debug=True, use_reloader=False)
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ services:
- FLASK_ENV=development
- OPENAI_API_KEY=${OPENAI_API_KEY}
- WEAVIATE_API_KEY=${WEAVIATE_API_KEY}
# - URL_CLUSTER=${URL_CLUSTER}
- URL_CLUSTER=http://weaviate:8080
- URL_CLUSTER=${URL_CLUSTER}
# - URL_CLUSTER=http://weaviate:8080

# weaviate:
# command:
Expand Down
11 changes: 11 additions & 0 deletions logger_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import logging

def configure_logger(app):
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)

app.logger.addHandler(console_handler)
app.logger.setLevel(logging.DEBUG)

40 changes: 8 additions & 32 deletions routes/routes.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import os
from typing import List, Optional, Tuple
from flask import Blueprint, request, jsonify, send_from_directory
from flask import Blueprint, request, jsonify, send_from_directory, current_app
from werkzeug.utils import secure_filename
from services.embeddings_basic import initialize_weaviate
from services.embeddings_basic import obtener_respuesta_rag
from openai import OpenAI
import weaviate
import traceback


routes_bp = Blueprint('routes', __name__)

openai_api_key = os.getenv('OPENAI_API_KEY')
llm = OpenAI(api_key=openai_api_key)


@routes_bp.route('/upload', methods=['POST'])
def upload_file():
current_app.logger.info("upload_file")
if 'file' not in request.files:
return jsonify({"error": "No file part"}), 400

Expand All @@ -30,43 +33,16 @@ def upload_file():
filename = secure_filename(file.filename)
file_path = os.path.join('uploads', filename)
file.save(file_path)

current_app.logger.info("Fichero guardado en "+file_path)
initialize_weaviate(file_path)
current_app.logger.info("inicializado weaviate sobre "+file_path)
except Exception as e:
print(f"Ha fallado subir a la carpeta uploads: {e}")
return jsonify({"error": f"Failed to process file {file.filename}: {str(e)}"}), 500

print(f"Files uploaded, processed, and embeddings stored in Weaviate")
return jsonify({"message": "Files uploaded, processed, and embeddings stored in Weaviate"}), 200


# @routes_bp.route('/ask', methods=['POST'])
# def ask_question():
# data = request.get_json()
# question = data.get('question')

# if not question:
# return jsonify({"error": "No question provided"}), 400

# try:
# client = weaviate.connect_to_wcs(
# cluster_url=os.getenv('URL_CLUSTER'),
# auth_credentials=weaviate.auth.AuthApiKey(os.getenv('WEAVIATE_API_KEY'))
# )

# vs = WeaviateVectorStore(client=client, index_name="rag1", embedding=OpenAIEmbeddings(), text_key= "text")
# retriever = vs.as_retriever()

# response = obtener_respuesta_rag(question, retriever)

# client.close()

# return jsonify({"answer": response}), 200

# except Exception as e:
# traceback.print_exc()
# return jsonify({"error": str(e)}), 500

@routes_bp.route('/ask', methods=['POST'])
def ask_question():
data = request.get_json()
Expand Down Expand Up @@ -106,9 +82,9 @@ def check_file_exists():

file_path = os.path.join('uploads', secure_filename(filename))
if os.path.exists(file_path):
return jsonify({"exists": True}), 200
return jsonify({"** exists": True}), 200
else:
return jsonify({"exists": False}), 200
return jsonify({"** exists": False}), 200

@routes_bp.route('/download/<filename>', methods=['GET'])
def download_file(filename):
Expand Down
Loading

0 comments on commit 368003d

Please sign in to comment.