Spaces:
Runtime error
Runtime error
| import os, sys | |
| from os.path import dirname as up | |
| sys.path.append(os.path.abspath(os.path.join(up(__file__), os.pardir))) | |
| from langchain.document_loaders import CSVLoader | |
| from langchain.indexes import VectorstoreIndexCreator | |
| from langchain.chains import RetrievalQA | |
| from langchain.llms import OpenAI | |
| import os | |
| import gradio as gr | |
| import pandas as pd | |
| from utils.constants import * | |
| os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY | |
| # Load the documents | |
| loader = CSVLoader(file_path=CSV_FILE_PATH) | |
| # Create an index using the loaded documents | |
| index_creator = VectorstoreIndexCreator() | |
| docsearch = index_creator.from_loaders([loader]) | |
| # Create a question-answering chain using the index | |
| chain = RetrievalQA.from_chain_type( | |
| llm=OpenAI(), | |
| chain_type="stuff", | |
| retriever=docsearch.vectorstore.as_retriever(), | |
| input_key="question", | |
| ) | |
| def return_response_chain(query: str): | |
| response = chain({"question": query}) | |
| return response['result'] | |
| def clear_fields(query: str, output: str): | |
| query = "" | |
| output = "" | |
| # if __name__ == "__main__": | |
| # # Pass a query to the chain | |
| # query = "How does UAE compare with USA in terms of gdp?" | |
| # response = chain({"question": query}) | |
| # print(response['result']) | |