Spaces:
Sleeping
Sleeping
import os | |
import streamlit as st | |
from llama_index import GPTSimpleVectorIndex, SimpleDirectoryReader, ServiceContext,LLMPredictor | |
from langchain.chat_models import ChatOpenAI | |
from llama_index.llm_predictor.chatgpt import ChatGPTLLMPredictor | |
import huggingface_hub | |
from huggingface_hub import Repository | |
from datetime import datetime | |
import csv | |
DATASET_REPO_URL = "https://huggingface.co/datasets/diazcalvi/kionlinde"#"https://huggingface.co/datasets/julien-c/persistent-space-dataset" | |
DATA_FILENAME = "kion.json" | |
DATA_FILE = os.path.join("data", DATA_FILENAME) | |
HF_TOKEN = os.environ.get("HF_TOKEN") | |
print("is none?", HF_TOKEN is None) | |
print("hfh", huggingface_hub.__version__) | |
#os.system("git config --global user.name \"Carlos Diaz\"") | |
#os.system("git config --global user.email \"[email protected]\"") | |
##repo = Repository( | |
# local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN | |
#) | |
index_name = "./data/kion.json" | |
documents_folder = "./documents" | |
#@st.experimental_memo | |
#@st.cache_resource | |
def initialize_index(index_name, documents_folder): | |
#llm_predictor = ChatGPTLLMPredictor() | |
llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo")) # text-davinci-003"))"gpt-3.5-turbo" | |
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor) | |
if os.path.exists(index_name): | |
index = GPTSimpleVectorIndex.load_from_disk(index_name) | |
else: | |
documents = SimpleDirectoryReader(documents_folder).load_data() | |
index = GPTSimpleVectorIndex.from_documents(documents) | |
index.save_to_disk(index_name) | |
print(DATA_FILE) | |
index.save_to_disk(DATA_FILE) | |
return index | |
#@st.experimental_memo | |
#@st.cache_data(max_entries=200, persist=True) | |
def query_index(_index, query_text): | |
response = _index.query(query_text) | |
return str(response) | |
def generate_html() -> str: | |
with open(DATA_FILE) as csvfile: | |
reader = csv.DictReader(csvfile) | |
rows = [] | |
for row in reader: | |
rows.append(row) | |
rows.reverse() | |
if len(rows) == 0: | |
return "no messages yet" | |
else: | |
html = "<div class='chatbot'>" | |
for row in rows: | |
html += "<div>" | |
html += f"<span>{row['name']}</span>" | |
html += f"<span class='message'>{row['message']}</span>" | |
html += "</div>" | |
html += "</div>" | |
return html | |
def store_message(name: str, message: str): | |
if name and message: | |
print(DATA_FILE) | |
print(DATA_FILENAME) | |
print(DATASET_REPO_URL) | |
with open(DATA_FILE, "a") as csvfile: | |
writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"]) | |
writer.writerow( | |
{"name": name, "message": message, "time": str(datetime.now())} | |
) | |
commit_url = repo.push_to_hub() | |
print(commit_url) | |
return commit_url #generate_html() | |
st.title("KION-Linde AI") | |
st.header("Welcome to KION-Linde's Artificial Intelligence Knowledge Base") | |
st.write("Enter a query about any KION/Linde products. The AI knows all the details, loads, sizes, manuals and procedures to support hundreds of parts and equipment. You can check out also our repository [here](https://www.linde-mh.com/en/About-us/Media/)") | |
index = None | |
api_key = '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'#st.text_input("Enter your OpenAI API key here:", type="password") | |
if api_key: | |
os.environ['OPENAI_API_KEY'] = api_key | |
index = initialize_index(index_name, documents_folder) | |
if index is None: | |
st.warning("Please enter your api key first.") | |
text = st.text_input("Query text:", value="What type of tires sizes, front, rear the R20 uses?") | |
if st.button("Run Query") and text is not None: | |
response = query_index(index, "Act as a KION equipment expert:" + text) | |
#myhtml = store_message(text, response) | |
st.markdown(response) | |
llm_col, embed_col = st.columns(2) | |
with llm_col: | |
st.markdown(f"LLM Tokens Used: {index.service_context.llm_predictor._last_token_usage}") | |
with embed_col: | |
st.markdown(f"Embedding Tokens Used: {index.service_context.embed_model._last_token_usage}") | |