davanstrien HF staff commited on
Commit
be1e49c
β€’
1 Parent(s): c6c19f5
Files changed (1) hide show
  1. app.py +14 -59
app.py CHANGED
@@ -37,9 +37,11 @@ class GeneralRetrievalQuery(BaseModel):
37
  visual_element_explanation: str
38
 
39
 
40
- def get_retrieval_prompt(prompt_name: str) -> Tuple[str, BaseModel]:
41
- if prompt_name == "general":
42
- prompt = """You are an AI assistant specialized in document retrieval tasks. Given an image of a document page, your task is to generate retrieval queries that someone might use to find this document in a large corpus.
 
 
43
 
44
  Please generate 3 different types of retrieval queries:
45
 
@@ -71,52 +73,15 @@ Here is the document image to analyze:
71
  <image>
72
 
73
  Generate the queries based on this image and provide the response in the specified JSON format."""
74
- return prompt, GeneralRetrievalQuery
75
- elif prompt_name == "multimodal_rag":
76
- prompt = """You are an assistant specialized in Multimodal RAG tasks.
77
-
78
- The task is the following: given an image from a pdf page, you will have to generate questions that can be asked by a user to retrieve information from a large documentary corpus.
79
-
80
- The question should be relevant to the page, and should not be too specific or too general. The question should be about the subject of the page, and the answer needs to be found in the page.
81
 
82
- Remember that the question is asked by a user to get some information from a large documentary corpus that contains multimodal data. Generate a question that could be asked by a user without knowing the existence and the content of the corpus.
83
-
84
- Generate as well the answer to the question, which should be found in the page. And the format of the answer should be a list of words answering the question.
85
-
86
- Generate at most THREE pairs of questions and answers per page as JSON with the following format, answer ONLY using JSON, NOTHING ELSE:
87
-
88
- {
89
- "questions": [
90
- {
91
- "question": "XXXXXX",
92
- "answer": ["YYYYYY"]
93
- },
94
- {
95
- "question": "XXXXXX",
96
- "answer": ["YYYYYY"]
97
- },
98
- {
99
- "question": "XXXXXX",
100
- "answer": ["YYYYYY"]
101
- }
102
- ]
103
- }
104
-
105
- where XXXXXX is the question and ['YYYYYY'] is the corresponding list of answers that could be as long as needed.
106
-
107
- Note: If there are no questions to ask about the page, return an empty list. Focus on making relevant questions concerning the page.
108
-
109
- Here is the page:"""
110
- return prompt, BaseModel
111
- else:
112
- raise ValueError("Invalid prompt name")
113
 
114
 
115
  # defined like this so we can later add more prompting options
116
  prompt, pydantic_model = get_retrieval_prompt("general")
117
 
118
 
119
- def _prep_data_for_input(image, prompt):
120
  messages = [
121
  {
122
  "role": "user",
@@ -146,10 +111,10 @@ def _prep_data_for_input(image, prompt):
146
 
147
 
148
  @spaces.GPU
149
- def generate_response(image, prompt_name="general"):
150
- prompt, _ = get_retrieval_prompt(prompt_name)
151
- inputs = _prep_data_for_input(image, prompt)
152
- inputs.to("cuda")
153
  generated_ids = model.generate(**inputs, max_new_tokens=200)
154
  generated_ids_trimmed = [
155
  out_ids[len(in_ids) :]
@@ -185,23 +150,13 @@ If you want to convert a PDF(s) to a dataset of page images you can try out the
185
  """
186
 
187
  examples = [
188
- ["examples/Approche_no_13_1977.pdf_page_22.jpg", "ColPali paper prompt"],
189
- [
190
- "examples/SRCCL_Technical-Summary.pdf_page_7.jpg",
191
- "ColPali paper prompt",
192
- ],
193
  ]
194
 
195
  demo = gr.Interface(
196
  fn=generate_response,
197
- inputs=[
198
- gr.Image(type="pil"),
199
- gr.Dropdown(
200
- choices=["ColPali paper prompt", "retrieval focused prompt"],
201
- value="general",
202
- label="Prompt Type",
203
- ),
204
- ],
205
  outputs=gr.Json(),
206
  title=title,
207
  description=description,
 
37
  visual_element_explanation: str
38
 
39
 
40
+ def get_retrieval_prompt(prompt_name: str) -> Tuple[str, GeneralRetrievalQuery]:
41
+ if prompt_name != "general":
42
+ raise ValueError("Only 'general' prompt is available in this version")
43
+
44
+ prompt = """You are an AI assistant specialized in document retrieval tasks. Given an image of a document page, your task is to generate retrieval queries that someone might use to find this document in a large corpus.
45
 
46
  Please generate 3 different types of retrieval queries:
47
 
 
73
  <image>
74
 
75
  Generate the queries based on this image and provide the response in the specified JSON format."""
 
 
 
 
 
 
 
76
 
77
+ return prompt, GeneralRetrievalQuery
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
 
80
  # defined like this so we can later add more prompting options
81
  prompt, pydantic_model = get_retrieval_prompt("general")
82
 
83
 
84
+ def _prep_data_for_input(image):
85
  messages = [
86
  {
87
  "role": "user",
 
111
 
112
 
113
  @spaces.GPU
114
+ def generate_response(image):
115
+ inputs = _prep_data_for_input(image)
116
+ inputs = inputs.to("cuda")
117
+
118
  generated_ids = model.generate(**inputs, max_new_tokens=200)
119
  generated_ids_trimmed = [
120
  out_ids[len(in_ids) :]
 
150
  """
151
 
152
  examples = [
153
+ "examples/Approche_no_13_1977.pdf_page_22.jpg",
154
+ "examples/SRCCL_Technical-Summary.pdf_page_7.jpg",
 
 
 
155
  ]
156
 
157
  demo = gr.Interface(
158
  fn=generate_response,
159
+ inputs=gr.Image(type="pil"),
 
 
 
 
 
 
 
160
  outputs=gr.Json(),
161
  title=title,
162
  description=description,