Spaces:
Runtime error
Runtime error
timer added
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import string
|
|
|
|
| 3 |
from typing import Any, Dict, List, Tuple, Union
|
| 4 |
|
| 5 |
import chromadb
|
|
@@ -8,7 +9,6 @@ import openai
|
|
| 8 |
import pandas as pd
|
| 9 |
import requests
|
| 10 |
import streamlit as st
|
| 11 |
-
import time
|
| 12 |
from datasets import load_dataset
|
| 13 |
from langchain.document_loaders import TextLoader
|
| 14 |
from langchain.embeddings.sentence_transformer import SentenceTransformerEmbeddings
|
|
@@ -70,11 +70,14 @@ if option == "YSA":
|
|
| 70 |
dataset = load_dataset(
|
| 71 |
"eagle0504/youthless-homeless-shelter-web-scrape-dataset-qa-formatted"
|
| 72 |
)
|
| 73 |
-
end_t
|
| 74 |
st.success(f"Time: {end_t - begin_t} sec")
|
| 75 |
initial_input = "Tell me about YSA"
|
| 76 |
else:
|
|
|
|
| 77 |
dataset = load_dataset("eagle0504/larkin-web-scrape-dataset-qa-formatted")
|
|
|
|
|
|
|
| 78 |
initial_input = "Tell me about Larkin"
|
| 79 |
|
| 80 |
|
|
@@ -99,11 +102,14 @@ collection = client.create_collection(combined_string)
|
|
| 99 |
# Embed and store the first N supports for this demo
|
| 100 |
with st.spinner("Loading, please be patient with us ... π"):
|
| 101 |
L = len(dataset["train"]["questions"])
|
|
|
|
| 102 |
collection.add(
|
| 103 |
ids=[str(i) for i in range(0, L)], # IDs are just strings
|
| 104 |
documents=dataset["train"]["questions"], # Enter questions here
|
| 105 |
metadatas=[{"type": "support"} for _ in range(0, L)],
|
| 106 |
)
|
|
|
|
|
|
|
| 107 |
|
| 108 |
|
| 109 |
# React to user input
|
|
@@ -115,7 +121,10 @@ if prompt := st.chat_input(initial_input):
|
|
| 115 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 116 |
|
| 117 |
question = prompt
|
|
|
|
| 118 |
results = collection.query(query_texts=question, n_results=5)
|
|
|
|
|
|
|
| 119 |
idx = results["ids"][0]
|
| 120 |
idx = [int(i) for i in idx]
|
| 121 |
ref = pd.DataFrame(
|
|
@@ -141,7 +150,10 @@ if prompt := st.chat_input(initial_input):
|
|
| 141 |
|
| 142 |
if option == "YSA":
|
| 143 |
try:
|
|
|
|
| 144 |
llm_response = llama2_7b_ysa(question)
|
|
|
|
|
|
|
| 145 |
except:
|
| 146 |
st.warning("Sorry, the inference endpoint is temporarily down. π")
|
| 147 |
llm_response = "NA."
|
|
@@ -158,6 +170,7 @@ if prompt := st.chat_input(initial_input):
|
|
| 158 |
# add ai judge as additional rating
|
| 159 |
if run_ai_judge == "Yes":
|
| 160 |
independent_ai_judge_score = []
|
|
|
|
| 161 |
for i in range(final_ref.shape[0]):
|
| 162 |
this_content = final_ref["answers"][i]
|
| 163 |
if len(this_content) > 3:
|
|
@@ -171,6 +184,9 @@ if prompt := st.chat_input(initial_input):
|
|
| 171 |
|
| 172 |
final_ref["ai_judge"] = independent_ai_judge_score
|
| 173 |
|
|
|
|
|
|
|
|
|
|
| 174 |
engineered_prompt = f"""
|
| 175 |
Based on the context: {ref_from_db_search}
|
| 176 |
|
|
@@ -179,7 +195,10 @@ if prompt := st.chat_input(initial_input):
|
|
| 179 |
Answer the question directly (don't say "based on the context, ...")
|
| 180 |
"""
|
| 181 |
|
|
|
|
| 182 |
answer = call_chatgpt(engineered_prompt)
|
|
|
|
|
|
|
| 183 |
response = answer
|
| 184 |
|
| 185 |
# Display assistant response in chat message container
|
|
|
|
| 1 |
import os
|
| 2 |
import string
|
| 3 |
+
import time
|
| 4 |
from typing import Any, Dict, List, Tuple, Union
|
| 5 |
|
| 6 |
import chromadb
|
|
|
|
| 9 |
import pandas as pd
|
| 10 |
import requests
|
| 11 |
import streamlit as st
|
|
|
|
| 12 |
from datasets import load_dataset
|
| 13 |
from langchain.document_loaders import TextLoader
|
| 14 |
from langchain.embeddings.sentence_transformer import SentenceTransformerEmbeddings
|
|
|
|
| 70 |
dataset = load_dataset(
|
| 71 |
"eagle0504/youthless-homeless-shelter-web-scrape-dataset-qa-formatted"
|
| 72 |
)
|
| 73 |
+
end_t = time.time()
|
| 74 |
st.success(f"Time: {end_t - begin_t} sec")
|
| 75 |
initial_input = "Tell me about YSA"
|
| 76 |
else:
|
| 77 |
+
begin_t = time.time()
|
| 78 |
dataset = load_dataset("eagle0504/larkin-web-scrape-dataset-qa-formatted")
|
| 79 |
+
end_t = time.time()
|
| 80 |
+
st.success(f"Database loaded. | Time: {end_t - begin_t} sec")
|
| 81 |
initial_input = "Tell me about Larkin"
|
| 82 |
|
| 83 |
|
|
|
|
| 102 |
# Embed and store the first N supports for this demo
|
| 103 |
with st.spinner("Loading, please be patient with us ... π"):
|
| 104 |
L = len(dataset["train"]["questions"])
|
| 105 |
+
begin_t = time.time()
|
| 106 |
collection.add(
|
| 107 |
ids=[str(i) for i in range(0, L)], # IDs are just strings
|
| 108 |
documents=dataset["train"]["questions"], # Enter questions here
|
| 109 |
metadatas=[{"type": "support"} for _ in range(0, L)],
|
| 110 |
)
|
| 111 |
+
end_t = time.time()
|
| 112 |
+
st.success(f"Add to VectorDB. | Time: {end_t - begin_t} sec")
|
| 113 |
|
| 114 |
|
| 115 |
# React to user input
|
|
|
|
| 121 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 122 |
|
| 123 |
question = prompt
|
| 124 |
+
begin_t = time.time()
|
| 125 |
results = collection.query(query_texts=question, n_results=5)
|
| 126 |
+
end_t = time.time()
|
| 127 |
+
st.success(f"Query answser. | Time: {end_t - begin_t} sec")
|
| 128 |
idx = results["ids"][0]
|
| 129 |
idx = [int(i) for i in idx]
|
| 130 |
ref = pd.DataFrame(
|
|
|
|
| 150 |
|
| 151 |
if option == "YSA":
|
| 152 |
try:
|
| 153 |
+
begin_t = time.time()
|
| 154 |
llm_response = llama2_7b_ysa(question)
|
| 155 |
+
end_t = time.time()
|
| 156 |
+
st.success(f"Running LLM. | Time: {end_t - begin_t} sec")
|
| 157 |
except:
|
| 158 |
st.warning("Sorry, the inference endpoint is temporarily down. π")
|
| 159 |
llm_response = "NA."
|
|
|
|
| 170 |
# add ai judge as additional rating
|
| 171 |
if run_ai_judge == "Yes":
|
| 172 |
independent_ai_judge_score = []
|
| 173 |
+
begin_t = time.time()
|
| 174 |
for i in range(final_ref.shape[0]):
|
| 175 |
this_content = final_ref["answers"][i]
|
| 176 |
if len(this_content) > 3:
|
|
|
|
| 184 |
|
| 185 |
final_ref["ai_judge"] = independent_ai_judge_score
|
| 186 |
|
| 187 |
+
end_t = time.time()
|
| 188 |
+
st.success(f"Using AI Judge. | Time: {end_t - begin_t} sec")
|
| 189 |
+
|
| 190 |
engineered_prompt = f"""
|
| 191 |
Based on the context: {ref_from_db_search}
|
| 192 |
|
|
|
|
| 195 |
Answer the question directly (don't say "based on the context, ...")
|
| 196 |
"""
|
| 197 |
|
| 198 |
+
begin_t = time.time()
|
| 199 |
answer = call_chatgpt(engineered_prompt)
|
| 200 |
+
end_t = time.time()
|
| 201 |
+
st.success(f"Final API Call. | Time: {end_t - begin_t} sec")
|
| 202 |
response = answer
|
| 203 |
|
| 204 |
# Display assistant response in chat message container
|