Spaces:
Runtime error
Runtime error
File size: 4,048 Bytes
ee3932a 4b74957 ee3932a 9848b7b 8f7c5ed ee3932a f7a2e50 ee3932a 9848b7b ee3932a c4cdd35 ee3932a f7a2e50 ee3932a b7dc108 50e33c3 4b74957 50e33c3 b7dc108 50e33c3 b7dc108 50e33c3 b7dc108 f9a221e b7dc108 c4cdd35 b7dc108 f9a221e b7dc108 f9a221e b7dc108 ee3932a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
import openai
import numpy as np
import pandas as pd
import os
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
from langchain import HuggingFaceHub
from langchain.vectorstores import Chroma
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.llms import OpenAI
from langchain.chains import RetrievalQA
from langchain.chains import VectorDBQA
from langchain.document_loaders import TextLoader, WebBaseLoader, SeleniumURLLoader
from langchain.document_loaders import UnstructuredFileLoader
from flask import Flask, jsonify, render_template, request
from werkzeug.utils import secure_filename
from werkzeug.datastructures import FileStorage
import nltk
nltk.download("punkt")
import warnings
warnings.filterwarnings("ignore")
openai.api_key=os.getenv("OPENAI_API_KEY")
import flask
import os
from dotenv import load_dotenv
load_dotenv()
loader = UnstructuredFileLoader('Jio.txt', mode='elements')
documents= loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(documents)
embeddings = OpenAIEmbeddings()
doc_search = Chroma.from_documents(texts,embeddings)
chain = VectorDBQA.from_chain_type(llm=OpenAI(temperature=0.0), chain_type="stuff", vectorstore=doc_search)
app = flask.Flask(__name__, template_folder="./")
# Create a directory in a known location to save files to.
uploads_dir = os.path.join(app.root_path,'static', 'uploads')
os.makedirs(uploads_dir, exist_ok=True)
@app.route('/Home')
def index():
return flask.render_template('index.html')
@app.route('/post_json', methods=['POST'])
def process_json():
content_type = request.headers.get('Content-Type')
if (content_type == 'application/json'):
requestQuery = request.get_json()
response= chain.run(requestQuery['query'])
print("Ques:>>>>"+requestQuery['query']+"\n Ans:>>>"+response)
return jsonify(botMessage=response);
else:
return 'Content-Type not supported!'
@app.route('/file_upload',methods=['POST'])
def file_Upload():
print(request.files.getlist('files[]'))
print(request.files)
print(request.form)
print(request.form.getlist('weburl'))
for filename in os.listdir(uploads_dir):
file_path = os.path.join(uploads_dir, filename)
print("Clearing Doc Directory. Trying to delete"+file_path)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
documents = []
for file in request.files.getlist('files[]'):
print(file.filename)
file.save(os.path.join(uploads_dir, secure_filename(file.filename)))
loader = UnstructuredFileLoader(os.path.join(uploads_dir, secure_filename(file.filename)), mode='elements')
documents.extend(loader.load())
urlLoader=SeleniumURLLoader(request.form.getlist('weburl'))
documents.extend(urlLoader.load())
print(uploads_dir)
global chain;
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
texts = text_splitter.split_documents(documents)
embeddings = OpenAIEmbeddings()
vectordb = Chroma.from_documents(texts,embeddings)
chain = RetrievalQA.from_chain_type(llm=OpenAI(temperature=0.0),chain_type="stuff", retriever=vectordb.as_retriever())
return render_template("index.html")
@app.route('/')
def KBUpload():
return render_template("KBTrain.html")
@app.route('/aiassist')
def aiassist():
return render_template("index.html")
if __name__ == '__main__':
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|