Spaces:
Build error
Build error
Commit ·
512f2de
1
Parent(s): cd0ba3c
iloc errors around chunking and token length plus script for allowing ctrl+enter submissions
Browse filesErrors:
FutureWarnings: These are warnings indicating that the code is using deprecated features that may be removed in future versions of the libraries. While not urgent, it's good to address these warnings to future-proof your code.
You can replace 'DataFrame.swapaxes' with 'DataFrame.transpose'.
Replace instances where you use Series.__getitem__ as positions with Series.iloc.
Token indices sequence length: This indicates that the tokenized input sequence is longer than the maximum allowed length for the model (512 tokens for TAPAS). You need to manage the size of your input sequence.
Also added script to accept "Ctrl+Enter" as Submit
123456781 unmodified line1011121314151617181920212223242526272829303132333435363738394041424344454647484918 unmodified lines6869707172737475767778import streamlit as stimport pandas as pdimport numpy as npfrom io import StringIOfrom transformers import AutoTokenizer, AutoModelForTableQuestionAnswering# Initialize TAPAS model and tokenizertokenizer = AutoTokenizer.from_pretrained("google/tapas-large-finetuned-wtq")1 unmodified linedef ask_llm_chunk(chunk, questions):chunk = chunk.astype(str)try:inputs = tokenizer(table=chunk, queries=questions, padding="max_length", return_tensors="pt")if inputs["input_ids"].shape[1] > 512:return ["Token limit exceeded for this 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())except Exception as e:st.write(f"An error occurred: {e}")return ["Error processing this chunk"] * len(questions)answers = []for coordinates in predicted_answer_coordinates:if len(coordinates) == 1:answers.append(chunk.iat[coordinates[0]])else:cell_values = []for coordinate in coordinates:cell_values.append(chunk.iat[coordinate])answers.append(", ".join(cell_values))return answersMAX_ROWS_PER_CHUNK = 50 # Reduced chunk sizedef summarize_map_reduce(data, questions):try:dataframe = pd.read_csv(StringIO(data))except Exception as e:st.write(f"Error reading the CSV file: {e}")return []num_chunks = len(dataframe) // MAX_ROWS_PER_CHUNK + 1dataframe_chunks = np.array_split(dataframe, num_chunks)all_answers = []18 unmodified linesif st.button("Submit"):if data and questions:try:answers = summarize_map_reduce(data, questions)st.write("Answers:")for q, a in zip(questions, answers):st.write(f"Question: {q}")st.write(f"Answer: {a}")except Exception as e:st.write(f"An error occurred: {e}")123456781 unmodified line1011121314151617181920212223242526272829303132333435363738394041424318 unmodified lines626364656667686970717273747576777879808182import streamlit as stimport pandas as pdfrom io import StringIOfrom transformers import AutoTokenizer, AutoModelForTableQuestionAnsweringimport numpy as np# Initialize TAPAS model and tokenizertokenizer = AutoTokenizer.from_pretrained("google/tapas-large-finetuned-wtq")1 unmodified linedef ask_llm_chunk(chunk, questions):chunk = chunk.astype(str)inputs = tokenizer(table=chunk, queries=questions, padding="max_length", return_tensors="pt")# Check for token limitif inputs["input_ids"].shape[1] > 512: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:answers.append(chunk.iloc[coordinates[0]].values)else:cell_values = []for coordinate in coordinates:cell_values.append(chunk.iloc[coordinate].values)answers.append(", ".join(cell_values))return answersMAX_ROWS_PER_CHUNK = 200def summarize_map_reduce(data, questions):dataframe = pd.read_csv(StringIO(data))num_chunks = len(dataframe) // MAX_ROWS_PER_CHUNK + 1dataframe_chunks = np.array_split(dataframe, num_chunks)all_answers = []18 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}")st.write(f"Answer: {a}")# Add Ctrl+Enter functionality for submitting the questionsst.markdown("""<script>document.addEventListener("DOMContentLoaded", function(event) {document.addEventListener("keydown", function(event) {if (event.ctrlKey && event.key === "Enter") {document.querySelector(".stButton button").click();}});});</script>""", unsafe_allow_html=True)
app.py+36-32