unknown commited on
Commit
d5b5b3a
β€’
1 Parent(s): 32213ba

Lutece Vision Space

Browse files
Files changed (2) hide show
  1. app.py +81 -0
  2. sujetAI.svg +1 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ from PIL import Image
4
+ from transformers import AutoProcessor, AutoModelForCausalLM, AutoConfig
5
+ import json
6
+
7
+ import subprocess
8
+ subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
9
+
10
+ # Function to load the model and processor
11
+ @st.cache_resource
12
+ def load_model_and_processor():
13
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
+ config = AutoConfig.from_pretrained("microsoft/Florence-2-base-ft", trust_remote_code=True)
15
+ config.vision_config.model_type = "davit"
16
+ model = AutoModelForCausalLM.from_pretrained("sujet-ai/Lutece-Vision-Base", config=config, trust_remote_code=True).to(device).eval()
17
+ processor = AutoProcessor.from_pretrained("sujet-ai/Lutece-Vision-Base", config=config, trust_remote_code=True)
18
+ return model, processor, device
19
+
20
+ # Function to generate answer
21
+ def generate_answer(model, processor, device, image, prompt):
22
+ task = "<FinanceQA>"
23
+ inputs = processor(text=prompt, images=image, return_tensors="pt").to(device)
24
+ generated_ids = model.generate(
25
+ input_ids=inputs["input_ids"],
26
+ pixel_values=inputs["pixel_values"],
27
+ max_new_tokens=1024,
28
+ do_sample=False,
29
+ num_beams=3,
30
+ )
31
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
32
+ parsed_answer = processor.post_process_generation(generated_text, task=task, image_size=(image.width, image.height))
33
+ return parsed_answer[task]
34
+
35
+ # Function to display config without nested expanders
36
+ def display_config(config, depth=0):
37
+ for key, value in config.items():
38
+ if isinstance(value, dict):
39
+ st.markdown(f"{' ' * depth}**{key}**:")
40
+ display_config(value, depth + 1)
41
+ else:
42
+ st.markdown(f"{' ' * depth}{key}: {value}")
43
+
44
+ # Streamlit app
45
+ def main():
46
+ st.set_page_config(page_title="Lutece-Vision-Base Demo", page_icon="πŸ—Ό", layout="wide", initial_sidebar_state="expanded")
47
+
48
+ # Title and description
49
+ st.title("πŸ—Ό Lutece-Vision-Base Demo")
50
+ st.markdown("Upload a financial document and ask questions about it!")
51
+
52
+ # Sidebar with SujetAI watermark
53
+ st.sidebar.image("sujetAI.svg", use_column_width=True)
54
+ st.sidebar.markdown("---")
55
+ st.sidebar.markdown("Our website : [sujet.ai](https://sujet.ai)")
56
+
57
+ # Load model and processor
58
+ model, processor, device = load_model_and_processor()
59
+
60
+ # File uploader for document
61
+ uploaded_file = st.file_uploader("πŸ“„ Upload a financial document", type=["png", "jpg", "jpeg"])
62
+
63
+ if uploaded_file is not None:
64
+ image = Image.open(uploaded_file).convert('RGB')
65
+ st.image(image, caption="Uploaded Document", use_column_width=True)
66
+
67
+ # Question input
68
+ question = st.text_input("❓ Ask a question about the document", "")
69
+
70
+ if st.button("πŸ” Generate Answer"):
71
+ with st.spinner("Generating answer..."):
72
+ answer = generate_answer(model, processor, device, image, question)
73
+ st.success(f"## πŸ’‘ {answer}")
74
+
75
+ # # Model configuration viewer
76
+ # with st.expander("πŸ”§ Model Configuration"):
77
+ # config_dict = model.config.to_dict()
78
+ # display_config(config_dict)
79
+
80
+ if __name__ == "__main__":
81
+ main()
sujetAI.svg ADDED