Spaces:
Build error
Weaviate schema based on user input and csv upload
Browse filesUI Input for Class and Description:
We'll add an input field in the Streamlit app where users can define the class name and description of the CSV they're uploading.
Auto-Populate Schema from CSV:
Once the CSV is uploaded, we'll read its headers to determine the column names. We can then use simple heuristics to determine the data type of each column (e.g., if a column contains only numbers, it's likely a float or int, if it matches date patterns, it's a date, otherwise, it's a string). We'll also use the column names as descriptions for simplicity, but this can be enhanced further if needed.
Create Schema in Weaviate:
Using the class name, description, and the auto-populated properties, we'll define the schema and create it in Weaviate.
Ingest Data:
Once the schema is created, we can then ingest the data from the CSV into Weaviate.
15 unmodified lines161718192021222324252627282930313233343553 unmodified lines89909192939495969798991001011021031041051061071081 unmodified line11011111211311411511611711811912012112212315 unmodified linesembedded_options=EmbeddedOptions())# Function to ingest data into Weaviatedef ingest_data_to_weaviate(dataframe):for index, row in dataframe.iterrows():obj = {"class": "YourClassName","id": str(index),"properties": row.to_dict()}client.data_object.create(obj)# Function to query data from Weaviatedef query_weaviate(question):# This is a basic example; adapt the query based on the questionresults = client.query.get('YourClassName').with_near_text(question).do()return resultsdef ask_llm_chunk(chunk, questions):53 unmodified linesall_answers.extend(chunk_answers)return all_answersst.title("TAPAS Table Question Answering with Weaviate")# Upload CSV datacsv_file = st.file_uploader("Upload a CSV file", type=["csv"])if csv_file is not None:data = csv_file.read().decode("utf-8")dataframe = pd.read_csv(StringIO(data))# Ingest data into Weaviateingest_data_to_weaviate(dataframe)st.write("CSV Data Preview:")st.write(dataframe.head())# Input for questionsquestions = st.text_area("Enter your questions (one per line)")questions = questions.split("\n") # split questions by line1 unmodified lineif st.button("Submit"):if data and questions:# Query Weaviate to get relevant datarelevant_data = query_weaviate(questions[0]) # Example: using the first question# Convert the relevant data to a DataFrame (you might need to adjust this based on the Weaviate response format)relevant_df = pd.DataFrame(relevant_data)# Pass the relevant data to TAPASanswers = summarize_map_reduce(relevant_df, questions)st.write("Answers:")for q, a in zip(questions, answers):st.write(f"Question: {q}")15 unmodified lines1617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606153 unmodified lines1151161171181191201211221231241251261271281291301311321331341351361371381391 unmodified line14114214314414514614715 unmodified linesembedded_options=EmbeddedOptions())def ingest_data_to_weaviate(dataframe, class_name, class_description):properties = []for column in dataframe.columns:data_type = "string"if dataframe[column].dtype == "float64":data_type = "float"elif dataframe[column].dtype == "int64":data_type = "int"properties.append({"name": column,"description": column,"dataType": [data_type]})schema = {"classes": [{"class": class_name,"description": class_description,"properties": properties}]}# Create Schema in Weaviateclient.schema.create(schema)# Ingest Databatch_request = weaviate.ObjectsBatchRequest()for _, row in dataframe.iterrows():obj = {"class": class_name,"properties": row.to_dict()}batch_request.add(obj)client.batch.create(batch_request)def query_weaviate(question):# This is a basic example; adapt the query based on the questionresults = client.query.get(class_name).with_near_text(question).do()return resultsdef ask_llm_chunk(chunk, questions):53 unmodified linesall_answers.extend(chunk_answers)return all_answersst.title("TAPAS Table Question Answering with Weaviate Integration")# UI Input for Class and Descriptionclass_name = st.text_input("Enter the class name for your CSV data:")class_description = st.text_input("Enter a description for your class:")# Upload CSV datacsv_file = st.file_uploader("Upload a CSV file", type=["csv"])if csv_file is not None:data = csv_file.read().decode("utf-8")dataframe = pd.read_csv(StringIO(data))st.write("CSV Data Preview:")st.write(dataframe.head())# Ingest data to Weaviateif st.button("Ingest to Weaviate"):ingest_data_to_weaviate(dataframe, class_name, class_description)st.write("Data ingested successfully!")# Input for questionsquestions = st.text_area("Enter your questions (one per line)")questions = questions.split("\n") # split questions by line1 unmodified lineif st.button("Submit"):if data and questions:answers = summarize_map_reduce(data, questions)st.write("Answers:")for q, a in zip(questions, answers):st.write(f"Question: {q}")