Spaces:
Build error
Build error
Commit
·
04376ef
1
Parent(s):
a4a1c61
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,6 +18,14 @@ client = weaviate.Client(
|
|
| 18 |
embedded_options=EmbeddedOptions()
|
| 19 |
)
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def map_dtype_to_weaviate(dtype):
|
| 22 |
"""
|
| 23 |
Map pandas data types to Weaviate data types.
|
|
@@ -131,47 +139,31 @@ def summarize_map_reduce(data, questions):
|
|
| 131 |
all_answers.extend(chunk_answers)
|
| 132 |
return all_answers
|
| 133 |
|
| 134 |
-
st.title("TAPAS Table Question Answering with Weaviate
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
-
|
| 137 |
-
class_name = st.text_input("Enter the class name
|
| 138 |
-
class_description = st.text_input("Enter a description for
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
# Upload CSV data
|
| 141 |
csv_file = st.file_uploader("Upload a CSV file", type=["csv"])
|
| 142 |
if csv_file is not None:
|
| 143 |
data = csv_file.read().decode("utf-8")
|
| 144 |
dataframe = pd.read_csv(StringIO(data))
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
questions = st.text_area("Enter your questions (one per line)")
|
| 155 |
-
questions = questions.split("\n") # split questions by line
|
| 156 |
-
questions = [q for q in questions if q] # remove empty strings
|
| 157 |
-
|
| 158 |
-
if st.button("Submit"):
|
| 159 |
-
if data and questions:
|
| 160 |
-
answers = summarize_map_reduce(data, questions)
|
| 161 |
-
st.write("Answers:")
|
| 162 |
-
for q, a in zip(questions, answers):
|
| 163 |
-
st.write(f"Question: {q}")
|
| 164 |
-
st.write(f"Answer: {a}")
|
| 165 |
-
|
| 166 |
-
# Add Ctrl+Enter functionality for submitting the questions
|
| 167 |
-
st.markdown("""
|
| 168 |
-
<script>
|
| 169 |
-
document.addEventListener("DOMContentLoaded", function(event) {
|
| 170 |
-
document.addEventListener("keydown", function(event) {
|
| 171 |
-
if (event.ctrlKey && event.key === "Enter") {
|
| 172 |
-
document.querySelector(".stButton button").click();
|
| 173 |
-
}
|
| 174 |
-
});
|
| 175 |
-
});
|
| 176 |
-
</script>
|
| 177 |
-
""", unsafe_allow_html=True)
|
|
|
|
| 18 |
embedded_options=EmbeddedOptions()
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# Function to check if a class already exists in Weaviate
|
| 22 |
+
def class_exists(class_name):
|
| 23 |
+
try:
|
| 24 |
+
client.schema.get_class(class_name)
|
| 25 |
+
return True
|
| 26 |
+
except:
|
| 27 |
+
return False
|
| 28 |
+
|
| 29 |
def map_dtype_to_weaviate(dtype):
|
| 30 |
"""
|
| 31 |
Map pandas data types to Weaviate data types.
|
|
|
|
| 139 |
all_answers.extend(chunk_answers)
|
| 140 |
return all_answers
|
| 141 |
|
| 142 |
+
st.title("TAPAS Table Question Answering with Weaviate")
|
| 143 |
+
|
| 144 |
+
# Get existing classes from Weaviate
|
| 145 |
+
existing_classes = [cls["class"] for cls in client.schema.get()["classes"]]
|
| 146 |
+
class_options = existing_classes + ["New Class"]
|
| 147 |
+
selected_class = st.selectbox("Select a class or create a new one:", class_options)
|
| 148 |
|
| 149 |
+
if selected_class == "New Class":
|
| 150 |
+
class_name = st.text_input("Enter the new class name:")
|
| 151 |
+
class_description = st.text_input("Enter a description for the class:")
|
| 152 |
+
else:
|
| 153 |
+
class_name = selected_class
|
| 154 |
+
class_description = "" # We can fetch the description from Weaviate if needed
|
| 155 |
|
| 156 |
# Upload CSV data
|
| 157 |
csv_file = st.file_uploader("Upload a CSV file", type=["csv"])
|
| 158 |
if csv_file is not None:
|
| 159 |
data = csv_file.read().decode("utf-8")
|
| 160 |
dataframe = pd.read_csv(StringIO(data))
|
| 161 |
+
|
| 162 |
+
# Display the schema if an existing class is selected
|
| 163 |
+
if selected_class != "New Class":
|
| 164 |
+
st.write(f"Schema for {selected_class}:")
|
| 165 |
+
class_schema = client.schema.get_class(selected_class)
|
| 166 |
+
st.write(class_schema)
|
| 167 |
+
|
| 168 |
+
# Ingest data into Weaviate
|
| 169 |
+
ingest_data_to_weaviate(dataframe, class_name, class_description)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|