Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
from transformers import T5ForConditionalGeneration, T5Tokenizer | |
from docx import Document | |
# Load the generator model (FLAN-T5) | |
generator_tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-large") | |
generator_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-large") | |
# Function to read DOCS files | |
def read_docs(file): | |
doc = Document(file) | |
text = "\n".join([paragraph.text for paragraph in doc.paragraphs]) | |
return text | |
# Function to process user input and generate financial statements | |
def generate_financial_statements(file, file_type): | |
# Read the file based on its type | |
if file_type == "csv": | |
df = pd.read_csv(file) | |
context = df.to_string() | |
elif file_type == "xlsx": | |
df = pd.read_excel(file) | |
context = df.to_string() | |
elif file_type == "docx": | |
context = read_docs(file) | |
else: | |
st.error("Unsupported file type. Please upload a CSV, Excel, or DOCS file.") | |
return None | |
# Define financial statement queries | |
queries = [ | |
"Generate a journal from the following financial data:", | |
"Generate a general ledger from the following financial data:", | |
"Generate an income statement from the following financial data:", | |
"Generate a balance sheet from the following financial data:", | |
"Generate a cash flow statement from the following financial data:" | |
] | |
# Generate financial statements using the generator model | |
financial_statements = {} | |
for query in queries: | |
# Combine query and context | |
input_text = f"{query}\n{context}" | |
# Generate response using the generator model | |
input_ids = generator_tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True).input_ids | |
output = generator_model.generate(input_ids, max_length=512) | |
response = generator_tokenizer.decode(output[0], skip_special_tokens=True) | |
# Store the result | |
financial_statements[query] = response | |
return financial_statements | |
# Streamlit UI | |
st.title("Financial Statement Generator") | |
st.write("Upload your financial data (CSV, Excel, or DOCS) to generate journal, general ledger, income statement, balance sheet, and cash flow statement.") | |
# File upload | |
uploaded_file = st.file_uploader("Upload your file", type=["csv", "xlsx", "docx"]) | |
if uploaded_file is not None: | |
file_type = uploaded_file.name.split(".")[-1].lower() # Ensure lowercase file type | |
financial_statements = generate_financial_statements(uploaded_file, file_type) | |
# Display results if financial_statements is not None | |
if financial_statements is not None: | |
for statement_type, statement in financial_statements.items(): | |
st.subheader(statement_type) | |
st.write(statement) | |
else: | |
st.error("Failed to generate financial statements. Please check the file type and content.") |