Daniel Marques commited on
Commit
8c08762
1 Parent(s): f2fde57

feat: add dele folder

Browse files
Files changed (4) hide show
  1. appold.py +0 -78
  2. appv1.py +0 -183
  3. constants.py +3 -1
  4. main.py +12 -6
appold.py DELETED
@@ -1,78 +0,0 @@
1
- import os
2
- import gradio as gr
3
- import copy
4
- import time
5
- import llama_cpp
6
- import ingest
7
- from llama_cpp import Llama
8
- from huggingface_hub import hf_hub_download
9
-
10
- import run_localGPT_API
11
-
12
-
13
- llm = Llama(
14
- model_path=hf_hub_download(
15
- repo_id=os.environ.get("REPO_ID", "TheBloke/Llama-2-7b-Chat-GGUF"),
16
- filename=os.environ.get("MODEL_FILE", "llama-2-7b-chat.Q4_K_M.gguf"),
17
- ),
18
- n_ctx=2048,
19
- n_gpu_layers=50, # change n_gpu_layers if you have more or less VRAM
20
- )
21
-
22
- history = []
23
-
24
- system_message = """
25
- you are a helpful, respectful and honest assistant. you should only respond to the following topics: water, climate, global warming, NASA data and geography. Always answer in the most helpful and safe way possible. Your answers should not include harmful, unethical, racist, sexist, toxic, dangerous or illegal content. Make sure that your answers are socially unbiased and positive in nature, as well as sticking to the topics of water, climate, global warming, NASA data and geography.
26
- If a question doesn't make sense or isn't factually coherent, explain that only questions on the topics of water, climate, global warming, NASA data and geography are accepted. If you don't know the answer to a question, don't share false information.
27
- """
28
-
29
-
30
- def generate_text(message, history):
31
- temp = ""
32
- input_prompt = f"[INST] <<SYS>>\n{system_message}\n<</SYS>>\n\n "
33
- for interaction in history:
34
- input_prompt = input_prompt + str(interaction[0]) + " [/INST] " + str(interaction[1]) + " </s><s> [INST] "
35
-
36
- input_prompt = input_prompt + str(message) + " [/INST] "
37
-
38
- output = llm(
39
- input_prompt,
40
- temperature=0.15,
41
- top_p=0.1,
42
- top_k=40,
43
- repeat_penalty=1.1,
44
- max_tokens=1024,
45
- stop=[
46
- "<|prompter|>",
47
- "<|endoftext|>",
48
- "<|endoftext|> \n",
49
- "ASSISTANT:",
50
- "USER:",
51
- "SYSTEM:",
52
- ],
53
- stream=True,
54
- )
55
- for out in output:
56
- stream = copy.deepcopy(out)
57
- temp += stream["choices"][0]["text"]
58
- yield temp
59
-
60
- history = ["init", input_prompt]
61
-
62
-
63
- demo = gr.ChatInterface(
64
- generate_text,
65
- title="Katara LLM",
66
- description="LLM of project https://katara.earth/",
67
- examples=["Show me all about water"],
68
- cache_examples=True,
69
- retry_btn=None,
70
- undo_btn="Delete Previous",
71
- clear_btn="Clear",
72
- )
73
- demo.queue(concurrency_count=1, max_size=5)
74
-
75
- demo.launch()
76
-
77
- ingest.main()
78
- run_localGPT_API.main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
appv1.py DELETED
@@ -1,183 +0,0 @@
1
- import logging
2
- import os
3
- import shutil
4
- import subprocess
5
-
6
- import torch
7
- from flask import Flask, jsonify, request, render_template
8
- from langchain.chains import RetrievalQA
9
- from langchain.embeddings import HuggingFaceInstructEmbeddings
10
-
11
- # from langchain.embeddings import HuggingFaceEmbeddings
12
- from run_localGPT import load_model
13
- from prompt_template_utils import get_prompt_template
14
-
15
- # from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
16
- from langchain.vectorstores import Chroma
17
- from werkzeug.utils import secure_filename
18
-
19
- from constants import CHROMA_SETTINGS, EMBEDDING_MODEL_NAME, PERSIST_DIRECTORY, MODEL_ID, MODEL_BASENAME
20
-
21
- if torch.backends.mps.is_available():
22
- DEVICE_TYPE = "mps"
23
- elif torch.cuda.is_available():
24
- DEVICE_TYPE = "cuda"
25
- else:
26
- DEVICE_TYPE = "cpu"
27
-
28
- SHOW_SOURCES = True
29
-
30
- EMBEDDINGS = HuggingFaceInstructEmbeddings(model_name=EMBEDDING_MODEL_NAME, model_kwargs={"device": DEVICE_TYPE})
31
-
32
- # uncomment the following line if you used HuggingFaceEmbeddings in the ingest.py
33
- # EMBEDDINGS = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL_NAME)
34
- # if os.path.exists(PERSIST_DIRECTORY):
35
- # try:
36
- # shutil.rmtree(PERSIST_DIRECTORY)
37
- # except OSError as e:
38
- # print(f"Error: {e.filename} - {e.strerror}.")
39
- # else:
40
- # print("The directory does not exist")
41
-
42
- # run_langest_commands = ["python", "ingest.py"]
43
- # if DEVICE_TYPE == "cpu":
44
- # run_langest_commands.append("--device_type")
45
- # run_langest_commands.append(DEVICE_TYPE)
46
-
47
- # result = subprocess.run(run_langest_commands, capture_output=True)
48
- # if result.returncode != 0:
49
- # raise FileNotFoundError(
50
- # "No files were found inside SOURCE_DOCUMENTS, please put a starter file inside before starting the API!"
51
- # )
52
-
53
- # load the vectorstore
54
- DB = Chroma(
55
- persist_directory=PERSIST_DIRECTORY,
56
- embedding_function=EMBEDDINGS,
57
- client_settings=CHROMA_SETTINGS,
58
- )
59
-
60
- RETRIEVER = DB.as_retriever()
61
-
62
- LLM = load_model(device_type=DEVICE_TYPE, model_id=MODEL_ID, model_basename=MODEL_BASENAME)
63
- prompt, memory = get_prompt_template(promptTemplate_type="llama", history=False)
64
-
65
- QA = RetrievalQA.from_chain_type(
66
- llm=LLM,
67
- chain_type="stuff",
68
- retriever=RETRIEVER,
69
- return_source_documents=SHOW_SOURCES,
70
- chain_type_kwargs={
71
- "prompt": prompt,
72
- },
73
- )
74
-
75
- app = Flask(__name__)
76
-
77
- @app.route("/")
78
- def index():
79
- return render_template("home.html")
80
-
81
-
82
- @app.route("/api/delete_source", methods=["GET"])
83
- def delete_source_route():
84
- folder_name = "SOURCE_DOCUMENTS"
85
-
86
- if os.path.exists(folder_name):
87
- shutil.rmtree(folder_name)
88
-
89
- os.makedirs(folder_name)
90
-
91
- return jsonify({"message": f"Folder '{folder_name}' successfully deleted and recreated."})
92
-
93
-
94
- @app.route("/api/save_document", methods=["GET", "POST"])
95
- def save_document_route():
96
- if "document" not in request.files:
97
- return "No document part", 400
98
- file = request.files["document"]
99
- if file.filename == "":
100
- return "No selected file", 400
101
- if file:
102
- filename = secure_filename(file.filename)
103
- folder_path = "SOURCE_DOCUMENTS"
104
- if not os.path.exists(folder_path):
105
- os.makedirs(folder_path)
106
- file_path = os.path.join(folder_path, filename)
107
- file.save(file_path)
108
- return "File saved successfully", 200
109
-
110
-
111
- @app.route("/api/run_ingest", methods=["GET"])
112
- def run_ingest_route():
113
- global DB
114
- global RETRIEVER
115
- global QA
116
- try:
117
- if os.path.exists(PERSIST_DIRECTORY):
118
- try:
119
- shutil.rmtree(PERSIST_DIRECTORY)
120
- except OSError as e:
121
- print(f"Error: {e.filename} - {e.strerror}.")
122
- else:
123
- print("The directory does not exist")
124
-
125
- run_langest_commands = ["python", "ingest.py"]
126
- if DEVICE_TYPE == "cpu":
127
- run_langest_commands.append("--device_type")
128
- run_langest_commands.append(DEVICE_TYPE)
129
-
130
- result = subprocess.run(run_langest_commands, capture_output=True)
131
- if result.returncode != 0:
132
- return "Script execution failed: {}".format(result.stderr.decode("utf-8")), 500
133
- # load the vectorstore
134
- DB = Chroma(
135
- persist_directory=PERSIST_DIRECTORY,
136
- embedding_function=EMBEDDINGS,
137
- client_settings=CHROMA_SETTINGS,
138
- )
139
- RETRIEVER = DB.as_retriever()
140
- prompt, memory = get_prompt_template(promptTemplate_type="llama", history=False)
141
-
142
- QA = RetrievalQA.from_chain_type(
143
- llm=LLM,
144
- chain_type="stuff",
145
- retriever=RETRIEVER,
146
- return_source_documents=SHOW_SOURCES,
147
- chain_type_kwargs={
148
- "prompt": prompt,
149
- },
150
- )
151
- return "Script executed successfully: {}".format(result.stdout.decode("utf-8")), 200
152
- except Exception as e:
153
- return f"Error occurred: {str(e)}", 500
154
-
155
-
156
- @app.route("/api/prompt_route", methods=["GET", "POST"])
157
- def prompt_route():
158
- global QA
159
- user_prompt = request.form.get("user_prompt")
160
- if user_prompt:
161
- # print(f'User Prompt: {user_prompt}')
162
- # Get the answer from the chain
163
- res = QA(user_prompt)
164
- answer, docs = res["result"], res["source_documents"]
165
-
166
- prompt_response_dict = {
167
- "Prompt": user_prompt,
168
- "Answer": answer,
169
- }
170
-
171
- prompt_response_dict["Sources"] = []
172
- for document in docs:
173
- prompt_response_dict["Sources"].append(
174
- (os.path.basename(str(document.metadata["source"])), str(document.page_content))
175
- )
176
-
177
- return jsonify(prompt_response_dict), 200
178
- else:
179
- return "No user prompt received", 400
180
-
181
-
182
- if __name__ == "__main__":
183
- app.run(host="0.0.0.0", port=5110)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
constants.py CHANGED
@@ -11,8 +11,10 @@ from langchain.document_loaders import UnstructuredFileLoader
11
  # load_dotenv()
12
  ROOT_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
13
 
 
 
14
  # Define the folder for storing database
15
- SOURCE_DIRECTORY = f"{ROOT_DIRECTORY}/SOURCE_DOCUMENTS"
16
 
17
  PERSIST_DIRECTORY = f"{ROOT_DIRECTORY}/DB"
18
 
 
11
  # load_dotenv()
12
  ROOT_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
13
 
14
+ PATH_NAME_SOURCE_DIRECTORY = "SOURCE_DOCUMENTS"
15
+
16
  # Define the folder for storing database
17
+ SOURCE_DIRECTORY = f"{ROOT_DIRECTORY}/{PATH_NAME_SOURCE_DIRECTORY}"
18
 
19
  PERSIST_DIRECTORY = f"{ROOT_DIRECTORY}/DB"
20
 
main.py CHANGED
@@ -6,7 +6,6 @@ from pydantic import BaseModel
6
  import os
7
  import shutil
8
  import subprocess
9
- import shutil
10
 
11
  # import torch
12
  from langchain.chains import RetrievalQA
@@ -20,7 +19,7 @@ from prompt_template_utils import get_prompt_template
20
  # from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
21
  from langchain.vectorstores import Chroma
22
 
23
- from constants import CHROMA_SETTINGS, EMBEDDING_MODEL_NAME, PERSIST_DIRECTORY, MODEL_ID, MODEL_BASENAME
24
 
25
  # if torch.backends.mps.is_available():
26
  # DEVICE_TYPE = "mps"
@@ -179,11 +178,10 @@ async def create_upload_file(file: UploadFile):
179
  # move the cursor back to the beginning
180
  await file.seek(0)
181
 
182
- if file_size > 2 * 1024 * 1024:
183
- # more than 2 MB
184
  raise HTTPException(status_code=400, detail="File too large")
185
 
186
- # check the content type (MIME type)
187
  content_type = file.content_type
188
 
189
  if content_type not in [
@@ -200,7 +198,15 @@ async def create_upload_file(file: UploadFile):
200
  "application/x-python-code"]:
201
  raise HTTPException(status_code=400, detail="Invalid file type")
202
 
203
- # do something with the valid file
 
 
 
 
 
 
 
 
204
  return {"filename": file.filename}
205
  # async def create_upload_file(file: Union[UploadFile, None] = None):
206
  # try:
 
6
  import os
7
  import shutil
8
  import subprocess
 
9
 
10
  # import torch
11
  from langchain.chains import RetrievalQA
 
19
  # from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
20
  from langchain.vectorstores import Chroma
21
 
22
+ from constants import CHROMA_SETTINGS, EMBEDDING_MODEL_NAME, PERSIST_DIRECTORY, MODEL_ID, MODEL_BASENAME, PATH_NAME_SOURCE_DIRECTORY
23
 
24
  # if torch.backends.mps.is_available():
25
  # DEVICE_TYPE = "mps"
 
178
  # move the cursor back to the beginning
179
  await file.seek(0)
180
 
181
+ if file_size > 10 * 1024 * 1024:
182
+ # more than 10 MB
183
  raise HTTPException(status_code=400, detail="File too large")
184
 
 
185
  content_type = file.content_type
186
 
187
  if content_type not in [
 
198
  "application/x-python-code"]:
199
  raise HTTPException(status_code=400, detail="Invalid file type")
200
 
201
+ upload_dir = os.path.join(os.getcwd(), PATH_NAME_SOURCE_DIRECTORY)
202
+ if not os.path.exists(upload_dir):
203
+ os.makedirs(upload_dir)
204
+
205
+ dest = os.path.join(upload_dir, file.filename)
206
+
207
+ with open(dest, "wb") as buffer:
208
+ shutil.copyfileobj(file.file, buffer)
209
+
210
  return {"filename": file.filename}
211
  # async def create_upload_file(file: Union[UploadFile, None] = None):
212
  # try: