Spaces:
Build error
Build error
Commit ·
d304ae4
1
Parent(s): 862e59b
Update app.py
Browse files5 unmodified lines678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152537 unmodified lines61626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920016 unmodified lines21721821922022122222318 unmodified lines2422432442452462472482 unmodified lines25125225325425525625716 unmodified lines2742752762775 unmodified linesfrom tapas_utils import *from ui_utils import *# ...selected_class = ui_utils.display_class_dropdown(client)ui_utils.handle_new_class_selection(selected_class)ui_utils.csv_upload_and_ingestion(selected_class)ui_utils.display_query_input()# ...# Initialize Weaviate clientclient = initialize_weaviate_client()# Initialize TAPAStokenizer, model = initialize_tapas()# UI componentsdisplay_initial_buttons()selected_class = display_class_dropdown(client)handle_new_class_selection()csv_upload_and_ingestion()display_query_input()# Initialize session state attributesif "debug" not in st.session_state:st.session_state.debug = Falsest_callback = StreamlitCallbackHandler(st.container())class StreamlitCallbackHandler(logging.Handler):def emit(self, record):log_entry = self.format(record)st.write(log_entry)# Initialize TAPAS model and tokenizer#tokenizer = AutoTokenizer.from_pretrained("google/tapas-large-finetuned-wtq")#model = AutoModelForTableQuestionAnswering.from_pretrained("google/tapas-large-finetuned-wtq")# Initialize Weaviate client for the embedded instance#client = weaviate.Client(# embedded_options=EmbeddedOptions()#)# Global list to store debugging informationDEBUG_LOGS = []def log_debug_info(message):if st.session_state.debug:7 unmodified lineslogger.debug(message)# Function to check if a class already exists in Weaviate#def class_exists(class_name):# try:# client.schema.get_class(class_name)# return True# except:# return False#def map_dtype_to_weaviate(dtype):## """# Map pandas data types to Weaviate data types.# """# if "int" in str(dtype):# return "int"# elif "float" in str(dtype):# return "number"# elif "bool" in str(dtype):# return "boolean"# else:# return "string"# def ingest_data_to_weaviate(dataframe, class_name, class_description):# # Create class schema# class_schema = {# "class": class_name,# "description": class_description,# "properties": [] # Start with an empty properties list# }## # Try to create the class without properties first# try:# client.schema.create({"classes": [class_schema]})# except weaviate.exceptions.SchemaValidationException:# # Class might already exist, so we can continue# pass## # Now, let's add properties to the class# for column_name, data_type in zip(dataframe.columns, dataframe.dtypes):# property_schema = {# "name": column_name,# "description": f"Property for {column_name}",# "dataType": [map_dtype_to_weaviate(data_type)]# }# try:# client.schema.property.create(class_name, property_schema)# except weaviate.exceptions.SchemaValidationException:# # Property might already exist, so we can continue# pass## # Ingest data# for index, row in dataframe.iterrows():# obj = {# "class": class_name,# "id": str(index),# "properties": row.to_dict()# }# client.data_object.create(obj)# Log data ingestion# log_debug_info(f"Data ingested into Weaviate for class: {class_name}")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 results#def ask_llm_chunk(chunk, questions):# chunk = chunk.astype(str)# try:# inputs = tokenizer(table=chunk, queries=questions, padding="max_length", truncation=True, return_tensors="pt")# except Exception as e:# log_debug_info(f"Tokenization error: {e}")# st.write(f"An error occurred: {e}")# return ["Error occurred while tokenizing"] * len(questions)### if inputs["input_ids"].shape[1] > 512:# log_debug_info("Token limit exceeded for chunk")# st.warning("Token limit exceeded for chunk")# return ["Token limit exceeded for chunk"] * len(questions)### outputs = model(**inputs)# predicted_answer_coordinates, predicted_aggregation_indices = tokenizer.convert_logits_to_predictions(# inputs,# outputs.logits.detach(),# outputs.logits_aggregation.detach()# )## answers = []# for coordinates in predicted_answer_coordinates:# if len(coordinates) == 1:# row, col = coordinates[0]# try:# value = chunk.iloc[row, col]# log_debug_info(f"Accessed value for row {row}, col {col}: {value}")# answers.append(value)# except Exception as e:# log_debug_info(f"Error accessing value for row {row}, col {col}: {e}")# st.write(f"An error occurred: {e}")# else:# cell_values = []# for coordinate in coordinates:# row, col = coordinate# try:# value = chunk.iloc[row, col]# cell_values.append(value)# except Exception as e:# log_debug_info(f"Error accessing value for row {row}, col {col}: {e}")# st.write(f"An error occurred: {e}")# answers.append(", ".join(map(str, cell_values)))## return answers# MAX_ROWS_PER_CHUNK = 200# def summarize_map_reduce(data, questions):# dataframe = pd.read_csv(StringIO(data))# num_chunks = len(dataframe) // MAX_ROWS_PER_CHUNK + 1# dataframe_chunks = [deepcopy(chunk) for chunk in np.array_split(dataframe, num_chunks)]# all_answers = []# for chunk in dataframe_chunks:# chunk_answers = ask_llm_chunk(chunk, questions)# all_answers.extend(chunk_answers)# return all_answersdef get_class_schema(class_name):"""Get the schema for a specific class."""all_classes = client.schema.get()["classes"]for cls in all_classes:if cls["class"] == class_name:return clsreturn Nonest.title("TAPAS Table Question Answering with Weaviate")16 unmodified linesclass_schema = None # Initialize class_schema to Noneif selected_class != "New Class":st.write(f"Schema for {selected_class}:")class_schema = get_class_schema(selected_class)if class_schema:properties = class_schema["properties"]schema_df = pd.DataFrame(properties)18 unmodified linesst.error("The columns in the uploaded CSV do not match the schema of the selected class. Please check and upload the correct CSV or create a new class.")else:# Ingest data into Weaviateingest_data_to_weaviate(dataframe, class_name, class_description)# Input for questionsquestions = st.text_area("Enter your questions (one per line)")2 unmodified linesif 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}")16 unmodified lines});});</script>""", unsafe_allow_html=True)5 unmodified lines67891011121314151617181920212223247 unmodified lines3233343536373839404142434445464716 unmodified lines6465666768697018 unmodified lines899091929394952 unmodified lines989910010110210310416 unmodified lines1211221231245 unmodified linesfrom tapas_utils import *from ui_utils import *# Initialize Weaviate clientclient = initialize_weaviate_client()# Initialize TAPAStokenizer, model = initialize_tapas()# Global list to store debugging informationDEBUG_LOGS = []class StreamlitCallbackHandler(logging.Handler):def emit(self, record):log_entry = self.format(record)st.write(log_entry)def log_debug_info(message):if st.session_state.debug:7 unmodified lineslogger.debug(message)# UI componentsui_utils.display_initial_buttons()selected_class = ui_utils.display_class_dropdown(client)ui_utils.handle_new_class_selection(client, selected_class)ui_utils.csv_upload_and_ingestion(client, selected_class)ui_utils.display_query_input()# Initialize session state attributesif "debug" not in st.session_state:st.session_state.debug = Falsest.title("TAPAS Table Question Answering with Weaviate")16 unmodified linesclass_schema = None # Initialize class_schema to Noneif selected_class != "New Class":st.write(f"Schema for {selected_class}:")class_schema = get_class_schema(client, selected_class)if class_schema:properties = class_schema["properties"]schema_df = pd.DataFrame(properties)18 unmodified linesst.error("The columns in the uploaded CSV do not match the schema of the selected class. Please check and upload the correct CSV or create a new class.")else:# Ingest data into Weaviateingest_data_to_weaviate(client, dataframe, class_name, class_description)# Input for questionsquestions = st.text_area("Enter your questions (one per line)")2 unmodified linesif st.button("Submit"):if data and questions:answers = summarize_map_reduce(tokenizer, model, data, questions)st.write("Answers:")for q, a in zip(questions, answers):st.write(f"Question: {q}")16 unmodified lines});});</script>""", unsafe_allow_html=True)
app.py+15-168