File size: 1,990 Bytes
37599f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import streamlit as st
import pandas as pd
from transformers import AutoTokenizer, BartForConditionalGeneration

# Load the TAPEX tokenizer and model (replace with your fine-tuned model names)
tokenizer = AutoTokenizer.from_pretrained("microsoft/tapex-large-finetuned-wtq")
model = BartForConditionalGeneration.from_pretrained("microsoft/tapex-large-finetuned-wtq")


def predict(table_path, query):
  """

  Predicts answer to a question using the TAPEX model on a given table.



  Args:

      table_path: Path to the CSV file containing the table data.

      query: The question to be answered.



  Returns:

      The predicted answer as a string.

  """
  # Load the sales data from CSV
  sales_record = pd.read_csv(r"C:/Users/sahit/Downloads/LLm of chatbot/10000 Sales Records.csv")
  sales_record = sales_record.astype(str)  # Ensure string type for tokenizer

  # Truncate the input to fit within the model's maximum sequence length
  max_length = model.config.max_position_embeddings
  encoding = tokenizer(table=sales_record, query=query, return_tensors="pt", truncation=True, max_length=max_length)

  # Generate the output
  outputs = model.generate(**encoding)

  # Decode the output
  prediction = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
  return prediction

st.title("Chatbot with CSV using TAPEX")

# Upload table data
uploaded_file = st.file_uploader("Upload Sales Data (CSV)", type="csv")

if uploaded_file is not None:
  # Read the uploaded CSV file
  df = pd.read_csv(uploaded_file)
  st.write(df)  # Display the uploaded table

  # User query input
  query = st.text_input("Hello ! Ask me anything about " + uploaded_file.name + " 🤗")

  if query:
    # Predict answer using the model
    prediction = predict(uploaded_file.name, query)
    st.write(f"*Your Question:* {query}")
    st.write(f"*Predicted Answer:* {prediction}")
else:
  st.info("Please upload a CSV file containing sales data.")