Atualizar app/app.py
This commit is contained in:
+141
-2
@@ -64,11 +64,97 @@ def new_conversation():
|
|||||||
@login_required
|
@login_required
|
||||||
def list_conversations():
|
def list_conversations():
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
convs = db.query(Conversation).all()
|
user_email = session["user"]["email"]
|
||||||
data = [{"id": c.id, "title": c.title} for c in convs]
|
convs = db.query(Conversation).filter(
|
||||||
|
(Conversation.user_email == user_email) |
|
||||||
|
(Conversation.shared == user_email)
|
||||||
|
).all()
|
||||||
|
data = [{"id": c.id, "title": c.title, "tags": c.tags} for c in convs]
|
||||||
db.close()
|
db.close()
|
||||||
return jsonify(data)
|
return jsonify(data)
|
||||||
|
|
||||||
|
@app.route("/api/export/md/<cid>")
|
||||||
|
@login_required
|
||||||
|
def export_md(cid):
|
||||||
|
db = SessionLocal()
|
||||||
|
conv = db.query(Conversation).filter_by(id=cid).first()
|
||||||
|
msgs = conv.messages
|
||||||
|
|
||||||
|
md = f"# {conv.title}\n\n"
|
||||||
|
if conv.tags:
|
||||||
|
md += f"**Tags:** {conv.tags}\n\n"
|
||||||
|
|
||||||
|
for m in msgs:
|
||||||
|
md += f"### {m.role.capitalize()}\n\n{m.content}\n\n"
|
||||||
|
|
||||||
|
db.close()
|
||||||
|
return Response(md, mimetype="text/markdown")
|
||||||
|
|
||||||
|
from weasyprint import HTML
|
||||||
|
|
||||||
|
import uuid
|
||||||
|
import pypandoc
|
||||||
|
from pdfminer.high_level import extract_text
|
||||||
|
from docx import Document
|
||||||
|
|
||||||
|
@app.route("/api/import/<cid>", methods=["POST"])
|
||||||
|
@login_required
|
||||||
|
def import_file(cid):
|
||||||
|
file = request.files.get("file")
|
||||||
|
if not file:
|
||||||
|
return jsonify({"error": "No file"}), 400
|
||||||
|
|
||||||
|
ext = file.filename.lower().split(".")[-1]
|
||||||
|
temp_path = f"/data/{uuid.uuid4()}.{ext}"
|
||||||
|
file.save(temp_path)
|
||||||
|
|
||||||
|
if ext == "pdf":
|
||||||
|
text = extract_text(temp_path)
|
||||||
|
elif ext == "docx":
|
||||||
|
doc = Document(temp_path)
|
||||||
|
text = "\n".join([p.text for p in doc.paragraphs])
|
||||||
|
elif ext == "doc":
|
||||||
|
text = pypandoc.convert_file(temp_path, "md")
|
||||||
|
else:
|
||||||
|
return jsonify({"error": "Unsupported format"}), 400
|
||||||
|
|
||||||
|
md = pypandoc.convert_text(text, "md", format="plain")
|
||||||
|
|
||||||
|
db = SessionLocal()
|
||||||
|
msg = Message(
|
||||||
|
id=str(uuid.uuid4()),
|
||||||
|
conversation_id=cid,
|
||||||
|
role="user",
|
||||||
|
content=md
|
||||||
|
)
|
||||||
|
db.add(msg)
|
||||||
|
db.commit()
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
return jsonify({"status": "ok", "imported_as_markdown": True})
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/export/pdf/<cid>")
|
||||||
|
@login_required
|
||||||
|
def export_pdf(cid):
|
||||||
|
db = SessionLocal()
|
||||||
|
conv = db.query(Conversation).filter_by(id=cid).first()
|
||||||
|
msgs = conv.messages
|
||||||
|
|
||||||
|
html = "<h1>{}</h1>".format(conv.title)
|
||||||
|
if conv.tags:
|
||||||
|
html += "<p><strong>Tags:</strong> {}</p>".format(conv.tags)
|
||||||
|
|
||||||
|
for m in msgs:
|
||||||
|
html += "<h3>{}</h3><p>{}</p>".format(m.role.capitalize(), m.content)
|
||||||
|
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
pdf = HTML(string=html).write_pdf()
|
||||||
|
return Response(pdf, mimetype="application/pdf")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/history/<cid>")
|
@app.route("/api/history/<cid>")
|
||||||
@login_required
|
@login_required
|
||||||
def history(cid):
|
def history(cid):
|
||||||
@@ -156,6 +242,59 @@ def stream():
|
|||||||
return Response(generate(), mimetype="text/event-stream")
|
return Response(generate(), mimetype="text/event-stream")
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/share/<cid>", methods=["POST"])
|
||||||
|
@login_required
|
||||||
|
def share(cid):
|
||||||
|
target_email = request.json.get("email")
|
||||||
|
db = SessionLocal()
|
||||||
|
conv = db.query(Conversation).filter_by(id=cid).first()
|
||||||
|
conv.shared = target_email
|
||||||
|
db.commit()
|
||||||
|
db.close()
|
||||||
|
return jsonify({"status": "ok"})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/tags/<cid>", methods=["POST"])
|
||||||
|
@login_required
|
||||||
|
def update_tags(cid):
|
||||||
|
tags = request.json.get("tags", [])
|
||||||
|
db = SessionLocal()
|
||||||
|
conv = db.query(Conversation).filter_by(id=cid).first()
|
||||||
|
conv.tags = ",".join(tags)
|
||||||
|
db.commit()
|
||||||
|
db.close()
|
||||||
|
return jsonify({"status": "ok"})
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/search")
|
||||||
|
@login_required
|
||||||
|
def search():
|
||||||
|
q = request.args.get("q", "").lower()
|
||||||
|
user_email = session["user"]["email"]
|
||||||
|
|
||||||
|
db = SessionLocal()
|
||||||
|
convs = db.query(Conversation).filter_by(user_email=user_email).all()
|
||||||
|
|
||||||
|
results = []
|
||||||
|
for c in convs:
|
||||||
|
for m in c.messages:
|
||||||
|
if q in m.content.lower():
|
||||||
|
results.append({
|
||||||
|
"conversation_id": c.id,
|
||||||
|
"title": c.title,
|
||||||
|
"role": m.role,
|
||||||
|
"content": m.content,
|
||||||
|
"created_at": m.created_at.isoformat()
|
||||||
|
})
|
||||||
|
|
||||||
|
db.close()
|
||||||
|
return jsonify(results)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# ***
|
# ***
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
|||||||
Reference in New Issue
Block a user