Created
September 3, 2023 21:29
-
-
Save fsndzomga/1f22e39490905f25eefe6a8d1f35bbc1 to your computer and use it in GitHub Desktop.
Serializing and Deserializing a bot instance created using Embedchain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import render_template, redirect, url_for, request, flash, session | |
from app import app | |
from app.forms import EmptyForm | |
from pdf_bot_class import PdfBot | |
from io import BytesIO | |
from uuid import uuid4 | |
@app.route('/') | |
@app.route('/index') | |
def index(): | |
form = EmptyForm() | |
return render_template('index.html', form=form) | |
@app.route('/upload', methods=['POST']) | |
def upload(): | |
form = EmptyForm() | |
if form.validate_on_submit(): | |
file = request.files['file'] | |
if file: | |
file_stream = BytesIO(file.read()) | |
pdf_bot_instance = PdfBot(file_stream) | |
session_id = str(uuid4()) | |
serialized_bot = pdf_bot_instance.serialize_bot() | |
session[session_id] = serialized_bot # Use Flask session to store serialized object | |
return redirect(url_for('chat', session_id=session_id)) | |
else: | |
flash("Please upload a valid PDF file") | |
return redirect(url_for('index')) | |
else: | |
return redirect(url_for('index')) | |
@app.route('/chat/<session_id>') | |
def chat(session_id): | |
serialized_bot = session.get(session_id) # Retrieve from Flask session | |
if serialized_bot: | |
pdf_bot_instance = PdfBot(serialized_state=serialized_bot) | |
question = "Are Embedding Spaces Interpretable?" | |
response = pdf_bot_instance.query_bot(question) | |
return f"Chat interface: {response}" | |
else: | |
return redirect(url_for('index')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment