Spaces:
Runtime error
Runtime error
test
Browse files- app.py +140 -0
- data/.gitattributes +54 -0
- documents/test.txt +0 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import gradio
|
| 4 |
+
|
| 5 |
+
from llama_index import GPTSimpleVectorIndex, SimpleDirectoryReader, ServiceContext,LLMPredictor
|
| 6 |
+
from langchain.chat_models import ChatOpenAI
|
| 7 |
+
from llama_index.llm_predictor.chatgpt import ChatGPTLLMPredictor
|
| 8 |
+
import huggingface_hub
|
| 9 |
+
from huggingface_hub import Repository
|
| 10 |
+
from datetime import datetime
|
| 11 |
+
import csv
|
| 12 |
+
|
| 13 |
+
DATASET_REPO_URL = "https://huggingface.co/datasets/diazcalvi/kionlinde"#"https://huggingface.co/datasets/julien-c/persistent-space-dataset"
|
| 14 |
+
DATA_FILENAME = "kion.json"
|
| 15 |
+
DATA_FILE = os.path.join("data", DATA_FILENAME)
|
| 16 |
+
|
| 17 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 18 |
+
print("is none?", HF_TOKEN is None)
|
| 19 |
+
|
| 20 |
+
print("hfh", huggingface_hub.__version__)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
#os.system("git config --global user.name \"Carlos Diaz\"")
|
| 25 |
+
#os.system("git config --global user.email \"diazcalvi@gmail.com\"")
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
##repo = Repository(
|
| 29 |
+
# local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
|
| 30 |
+
#)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
index_name = "./data/kion.json"
|
| 34 |
+
documents_folder = "./documents"
|
| 35 |
+
#@st.experimental_memo
|
| 36 |
+
#@st.cache_resource
|
| 37 |
+
def initialize_index(index_name, documents_folder):
|
| 38 |
+
#llm_predictor = ChatGPTLLMPredictor()
|
| 39 |
+
llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo")) # text-davinci-003"))"gpt-3.5-turbo"
|
| 40 |
+
|
| 41 |
+
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor)
|
| 42 |
+
if os.path.exists(index_name):
|
| 43 |
+
index = GPTSimpleVectorIndex.load_from_disk(index_name)
|
| 44 |
+
else:
|
| 45 |
+
documents = SimpleDirectoryReader(documents_folder).load_data()
|
| 46 |
+
index = GPTSimpleVectorIndex.from_documents(documents)
|
| 47 |
+
index.save_to_disk(index_name)
|
| 48 |
+
print(DATA_FILE)
|
| 49 |
+
index.save_to_disk(DATA_FILE)
|
| 50 |
+
|
| 51 |
+
return index
|
| 52 |
+
|
| 53 |
+
#@st.experimental_memo
|
| 54 |
+
#@st.cache_data(max_entries=200, persist=True)
|
| 55 |
+
def query_index(_index, query_text):
|
| 56 |
+
response = _index.query(query_text)
|
| 57 |
+
return str(response)
|
| 58 |
+
|
| 59 |
+
def generate_html() -> str:
|
| 60 |
+
with open(DATA_FILE) as csvfile:
|
| 61 |
+
reader = csv.DictReader(csvfile)
|
| 62 |
+
rows = []
|
| 63 |
+
for row in reader:
|
| 64 |
+
rows.append(row)
|
| 65 |
+
rows.reverse()
|
| 66 |
+
if len(rows) == 0:
|
| 67 |
+
return "no messages yet"
|
| 68 |
+
else:
|
| 69 |
+
html = "<div class='chatbot'>"
|
| 70 |
+
for row in rows:
|
| 71 |
+
html += "<div>"
|
| 72 |
+
html += f"<span>{row['name']}</span>"
|
| 73 |
+
html += f"<span class='message'>{row['message']}</span>"
|
| 74 |
+
html += "</div>"
|
| 75 |
+
html += "</div>"
|
| 76 |
+
return html
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def store_message(name: str, message: str):
|
| 80 |
+
if name and message:
|
| 81 |
+
print(DATA_FILE)
|
| 82 |
+
print(DATA_FILENAME)
|
| 83 |
+
print(DATASET_REPO_URL)
|
| 84 |
+
with open(DATA_FILE, "a") as csvfile:
|
| 85 |
+
writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
|
| 86 |
+
writer.writerow(
|
| 87 |
+
{"name": name, "message": message, "time": str(datetime.now())}
|
| 88 |
+
)
|
| 89 |
+
commit_url = repo.push_to_hub()
|
| 90 |
+
print(commit_url)
|
| 91 |
+
|
| 92 |
+
return commit_url #generate_html()
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def greet(text):
|
| 97 |
+
response = query_index(index, "Act as a KION equipment expert and answer this with detail:" + text + ". (Include the context reference details, file name, page number, and date if available)")
|
| 98 |
+
return response
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
index = None
|
| 104 |
+
api_key = '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'#st.text_input("Enter your OpenAI API key here:", type="password")
|
| 105 |
+
if api_key:
|
| 106 |
+
os.environ['OPENAI_API_KEY'] = api_key
|
| 107 |
+
index = initialize_index(index_name, documents_folder)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
if index is None:
|
| 111 |
+
st.warning("Please enter your api key first.")
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
gradio_interface = gradio.Interface(
|
| 116 |
+
fn=greet,
|
| 117 |
+
inputs="text",
|
| 118 |
+
outputs="text",
|
| 119 |
+
examples=[
|
| 120 |
+
["What can I ask you? Give me 20 different examples."],
|
| 121 |
+
["What are some of the LPG Lift trucks, and what series and models? Make a list."],
|
| 122 |
+
["What dealers do we have in Michigan and how can I contact them?"],
|
| 123 |
+
["What can you tell me about Eike Wibrow? Expand on background"],
|
| 124 |
+
["What do you know about Bravo Montacargas and how to contact them? When were they added to the Dealer Network?"],
|
| 125 |
+
["Give me some details on the P60"],
|
| 126 |
+
["What is the Youth Apprentice Signing Day?"],
|
| 127 |
+
["Do we have a dealer in NC? List them"],
|
| 128 |
+
["Tell me more about Tri-Lift NC"],
|
| 129 |
+
["What are some the optional equipment for the E18, E20? Series 346?"],
|
| 130 |
+
["Who are our contact/leads on HTX?"],
|
| 131 |
+
["KBG40 and KBG50. What is the overall length?"],
|
| 132 |
+
["What are the mission, vision and values of KION NA? List them"],
|
| 133 |
+
["When was the new linde MT18 added to the product line?"],
|
| 134 |
+
["Who is Jonathan Dawley?"]
|
| 135 |
+
],
|
| 136 |
+
title="KION - Linde & Baoli AI",
|
| 137 |
+
description="Enter a query about any KION/Linde & Baoli products, parts, news. The AI knows all the details, loads, sizes, manuals and procedures to support hundreds of parts and equipment. Also is aware of all the recent news. You can check out also our repository [here](https://www.kion-na.com/products/)",
|
| 138 |
+
article="�� Carlos Diaz Calvi 2023"
|
| 139 |
+
)
|
| 140 |
+
gradio_interface.launch()
|
data/.gitattributes
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.lz4 filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
# Audio files - uncompressed
|
| 37 |
+
*.pcm filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
*.sam filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
*.raw filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
# Audio files - compressed
|
| 41 |
+
*.aac filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
*.flac filter=lfs diff=lfs merge=lfs -text
|
| 43 |
+
*.mp3 filter=lfs diff=lfs merge=lfs -text
|
| 44 |
+
*.ogg filter=lfs diff=lfs merge=lfs -text
|
| 45 |
+
*.wav filter=lfs diff=lfs merge=lfs -text
|
| 46 |
+
# Image files - uncompressed
|
| 47 |
+
*.bmp filter=lfs diff=lfs merge=lfs -text
|
| 48 |
+
*.gif filter=lfs diff=lfs merge=lfs -text
|
| 49 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
| 50 |
+
*.tiff filter=lfs diff=lfs merge=lfs -text
|
| 51 |
+
# Image files - compressed
|
| 52 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
| 53 |
+
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
| 54 |
+
*.webp filter=lfs diff=lfs merge=lfs -text
|
documents/test.txt
ADDED
|
File without changes
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain==0.0.123
|
| 2 |
+
llama-index==0.5.1
|
| 3 |
+
streamlit==1.19.0
|
| 4 |
+
PyPDF2
|