Atualizar app/app.py
This commit is contained in:
+50
-7
@@ -690,11 +690,11 @@ def chat():
|
|||||||
@login_required
|
@login_required
|
||||||
@rate_limited
|
@rate_limited
|
||||||
def chat_stream():
|
def chat_stream():
|
||||||
model = request.args.get("model", "llama3.2")
|
|
||||||
cid = request.args.get("conversation_id")
|
cid = request.args.get("conversation_id")
|
||||||
|
email = session["user"]["email"] if app.config["AUTH_MODE"] == "oauth" else session["db_user"]["email"]
|
||||||
|
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
email = session["user"]["email"] if app.config["AUTH_MODE"] == "oauth" else session["db_user"]["email"]
|
|
||||||
msgs = (
|
msgs = (
|
||||||
db.query(Message)
|
db.query(Message)
|
||||||
.join(Conversation, Message.conversation_id == Conversation.id)
|
.join(Conversation, Message.conversation_id == Conversation.id)
|
||||||
@@ -702,11 +702,13 @@ def chat_stream():
|
|||||||
.order_by(Message.created_at)
|
.order_by(Message.created_at)
|
||||||
.all()
|
.all()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
chatml = [{"role": m.role, "content": m.content} for m in msgs]
|
chatml = [{"role": m.role, "content": m.content} for m in msgs]
|
||||||
|
# segue chamada ao Ollama
|
||||||
|
|
||||||
|
model = request.args.get("model", "llama3.2")
|
||||||
|
#cid = request.args.get("conversation_id")
|
||||||
|
|
||||||
def generate():
|
def generate():
|
||||||
url = f"{app.config['OLLAMA_BASE_URL']}/api/chat"
|
url = f"{app.config['OLLAMA_BASE_URL']}/api/chat"
|
||||||
@@ -772,6 +774,26 @@ def export_md(cid):
|
|||||||
db.close()
|
db.close()
|
||||||
return Response(md, mimetype="text/markdown")
|
return Response(md, mimetype="text/markdown")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/file/<file_id>")
|
||||||
|
@login_required
|
||||||
|
def get_file(file_id):
|
||||||
|
email = session["user"]["email"] if app.config["AUTH_MODE"] == "oauth" else session["db_user"]["email"]
|
||||||
|
|
||||||
|
db = SessionLocal()
|
||||||
|
f = db.query(File).filter_by(id=file_id, user_email=email).first()
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
if not f:
|
||||||
|
return jsonify({"erro": "Arquivo não encontrado."}), 404
|
||||||
|
|
||||||
|
return send_file(f.path)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
from weasyprint import HTML
|
from weasyprint import HTML
|
||||||
|
|
||||||
import uuid
|
import uuid
|
||||||
@@ -788,9 +810,14 @@ def import_file(cid):
|
|||||||
return jsonify({"error": "No file"}), 400
|
return jsonify({"error": "No file"}), 400
|
||||||
|
|
||||||
ext = file.filename.lower().split(".")[-1]
|
ext = file.filename.lower().split(".")[-1]
|
||||||
temp_path = f"/data/{uuid.uuid4()}.{ext}"
|
file_id = str(uuid.uuid4())
|
||||||
file.save(temp_path)
|
path = f"/data/{file_id}_{file.filename}"
|
||||||
|
|
||||||
|
file.save(path)
|
||||||
|
|
||||||
|
db.add(File(id=file_id, user_email=email, path=path, original_name=file.filename))
|
||||||
|
db.commit()
|
||||||
|
|
||||||
if ext == "pdf":
|
if ext == "pdf":
|
||||||
text = extract_text(temp_path)
|
text = extract_text(temp_path)
|
||||||
elif ext == "docx":
|
elif ext == "docx":
|
||||||
@@ -913,6 +940,22 @@ def get_file(file_id):
|
|||||||
return send_file(f.path)
|
return send_file(f.path)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/stream/<cid>")
|
||||||
|
@login_required
|
||||||
|
def stream(cid):
|
||||||
|
email = session["user"]["email"] if app.config["AUTH_MODE"] == "oauth" else session["db_user"]["email"]
|
||||||
|
|
||||||
|
db = SessionLocal()
|
||||||
|
conv = db.query(Conversation).filter_by(id=cid, user_email=email).first()
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
if not conv:
|
||||||
|
return jsonify({"erro": "Conversa não encontrada."}), 404
|
||||||
|
|
||||||
|
# segue streaming normalmente
|
||||||
|
# TODO
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/stream")
|
@app.route("/api/stream")
|
||||||
|
|||||||
Reference in New Issue
Block a user