File size: 3,129 Bytes
ee3932a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f7c5ed
 
 
 
ee3932a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7dc108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9a221e
b7dc108
f9a221e
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
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
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()

# 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)


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(), chain_type="stuff", vectorstore=doc_search)

app = flask.Flask(__name__, template_folder="./")


@app.route('/')
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()
        print("Ques:>>>>"+requestQuery['query']+"\n Ans:>>>"+chain.run(requestQuery['query']))
        return jsonify(botMessage=chain.run(requestQuery['query'])); 
    else:
        return 'Content-Type not supported!'


@app.route('/file_upload',methods=['POST'])
def file_Upload():
    
        #print(request.headers.get('Content-Type'))
        file=request.files['file']
        print(uploads_dir)
        global chain;
      

        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= 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(), chain_type="stuff", vectorstore=doc_search)
        
        return render_template("index.html")

@app.route('/KBTrain')
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)))