Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import streamlit as st | |
| from langchain.embeddings.openai import OpenAIEmbeddings | |
| from langchain.text_splitter import CharacterTextSplitter | |
| from langchain.vectorstores import Chroma | |
| from langchain.chains import ConversationalRetrievalChain | |
| from langchain.chat_models import ChatOpenAI | |
| from langchain.document_loaders import PyPDFLoader | |
| import os | |
| import fitz | |
| from PIL import Image | |
| # Global variables | |
| COUNT, N = 0, 0 | |
| chat_history = [] | |
| chain = None # Initialize chain as None | |
| # Function to set the OpenAI API key | |
| def set_apikey(api_key): | |
| os.environ['OPENAI_API_KEY'] = api_key | |
| return disable_box | |
| # Function to enable the API key input box | |
| def enable_api_box(): | |
| return enable_box | |
| # Function to add text to the chat history | |
| def add_text(history, text): | |
| if not text: | |
| raise gr.Error('Enter text') | |
| history = history + [(text, '')] | |
| return history | |
| # Function to process the PDF file and create a conversation chain | |
| def process_file(file): | |
| global chain | |
| if 'OPENAI_API_KEY' not in os.environ: | |
| raise gr.Error('Upload your OpenAI API key') | |
| # Replace with your actual PDF processing logic | |
| loader = PyPDFLoader(file.name) | |
| documents = loader.load() | |
| embeddings = OpenAIEmbeddings() | |
| pdfsearch = Chroma.from_documents(documents, embeddings) | |
| chain = ConversationalRetrievalChain.from_llm(ChatOpenAI(temperature=0.3), | |
| retriever=pdfsearch.as_retriever(search_kwargs={"k": 1}), | |
| return_source_documents=True) | |
| return chain | |
| # Function to generate a response based on the chat history and query | |
| def generate_response(history, query, pdf_upload): | |
| global COUNT, N, chat_history, chain | |
| if not pdf_upload: | |
| raise gr.Error(message='Upload a PDF') | |
| if COUNT == 0: | |
| chain = process_file(pdf_upload) | |
| COUNT += 1 | |
| # Replace with your LangChain logic to generate a response | |
| result = chain({"question": query, 'chat_history': chat_history}, return_only_outputs=True) | |
| chat_history += [(query, result["answer"])] | |
| N = list(result['source_documents'][0])[1][1]['page'] # Adjust as needed | |
| for char in result['answer']: | |
| history[-1][-1] += char | |
| return history, '' | |
| # Function to render a specific page of a PDF file as an image | |
| def render_file(file): | |
| global N | |
| doc = fitz.open(file.name) | |
| page = doc[N] | |
| pix = page.get_pixmap(matrix=fitz.Matrix(300/72, 300/72)) | |
| image = Image.frombytes('RGB', [pix.width, pix.height], pix.samples) | |
| return image | |
| # Function to render initial content from the PDF | |
| def render_first(pdf_file): | |
| # Replace with logic to process the PDF and generate an initial image | |
| image = Image.new('RGB', (600, 400), color = 'white') # Placeholder | |
| return image | |
| # Streamlit & Gradio Interface | |
| st.title("PDF-Powered Chatbot") | |
| with st.container(): | |
| gr.Markdown(""" | |
| <style> | |
| .image-container { height: 680px; } | |
| </style> | |
| """) | |
| with gr.Blocks() as demo: | |
| pdf_upload1 = gr.UploadButton("๐ Upload PDF 1", file_types=[".pdf"]) # Define pdf_upload1 | |
| # ... (rest of your interface creation) | |
| txt = gr.Textbox(label="Enter your query", placeholder="Ask a question...") | |
| submit_btn = gr.Button('Submit') | |
| def on_submit(): | |
| add_text(chatbot, txt) | |
| generate_response(chatbot, txt, pdf_upload1) # Use pdf_upload1 here | |
| render_file(pdf_upload1) # Use pdf_upload1 here | |
| if __name__ == "__main__": | |
| gr.Interface( | |
| [render_first, add_text, generate_response, render_file], | |
| [ | |
| gr.inputs.File(label="Upload PDF 1"), # Define pdf_upload1 | |
| gr.outputs.Textbox(label="Chatbot Output"), # Define chatbot output | |
| gr.inputs.Textbox(label="Enter your query") # Define txt | |
| ], | |
| [ | |
| gr.outputs.Image(label="Rendered PDF Image"), # Define show_img | |
| gr.outputs.Textbox(label="Chatbot Output"), # Define chatbot output | |
| gr.inputs.Textbox(label="Enter your query") # Define txt | |
| ], | |
| title="PDF-Powered Chatbot" | |
| ).launch() | |