syzdekbr commited on
Commit
bdbc613
·
verified ·
1 Parent(s): 0093fa9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -9
app.py CHANGED
@@ -156,25 +156,84 @@ def _data_uri(png_bytes: bytes) -> str:
156
  return "data:image/png;base64," + base64.b64encode(png_bytes).decode("ascii")
157
 
158
 
159
- def generate_alt_text(client, model, prompt, png_bytes, max_tokens):
160
- completion = client.chat.completions.create(
161
- model=model,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
  messages=[
 
 
 
 
163
  {
164
  "role": "user",
165
  "content": [
166
- {"type": "text", "text": prompt},
167
  {
168
- "type": "image_url",
169
- "image_url": {"url": _data_uri(png_bytes)},
170
  },
171
- ],
 
 
 
 
 
 
172
  }
173
  ],
174
- max_tokens=int(max_tokens),
175
  temperature=0,
 
176
  )
177
- return (completion.choices[0].message.content or "").strip()
 
 
 
 
 
 
 
 
178
 
179
 
180
  def process_table(
 
156
  return "data:image/png;base64," + base64.b64encode(png_bytes).decode("ascii")
157
 
158
 
159
+ ALT_TEXT_SYSTEM_PROMPT = """
160
+ You create accessibility alt text for images.
161
+
162
+ Return only the finished alt text.
163
+
164
+ Requirements:
165
+ - Write one concise sentence.
166
+ - Describe the image's essential meaning and purpose.
167
+ - Do not describe every minor visual detail.
168
+ - Do not begin with "Image of", "Picture of", or "This image shows".
169
+ - Do not include analysis, reasoning, headings, XML tags, or bullet points.
170
+ - Do not include <think> tags.
171
+ - Do not identify people unless their identity is explicitly provided.
172
+ - For diagrams, summarize the main process or relationship.
173
+ - For charts, state the chart type and principal trend or conclusion.
174
+ - Keep the response under 250 characters when practical.
175
+ """
176
+
177
+ def generate_alt_text(client, image_data_uri, prompt=None):
178
+ system_prompt = """
179
+ You are an accessibility specialist writing alt text.
180
+
181
+ Return exactly one plain-text alt description and nothing else.
182
+
183
+ Rules:
184
+ 1. Do not expose reasoning or analysis.
185
+ 2. Do not use <think> tags.
186
+ 3. Do not add a heading or label.
187
+ 4. Do not begin with "Image of", "Picture of", or "This image shows".
188
+ 5. Describe the image's essential information and function.
189
+ 6. For diagrams, explain the main process or relationship rather than listing
190
+ every label.
191
+ 7. Include visible text only when it is necessary to understand the image.
192
+ 8. Do not speculate about unclear content.
193
+ 9. Aim for one sentence and fewer than 250 characters.
194
+ """
195
+
196
+ user_prompt = prompt or (
197
+ "Write accessible alt text for this image. "
198
+ "Return only the finished alt text."
199
+ )
200
+
201
+ response = client.chat.completions.create(
202
+ model="qwen/qwen3.6-27b",
203
+ reasoning_effort="none",
204
  messages=[
205
+ {
206
+ "role": "system",
207
+ "content": system_prompt
208
+ },
209
  {
210
  "role": "user",
211
  "content": [
 
212
  {
213
+ "type": "text",
214
+ "text": user_prompt
215
  },
216
+ {
217
+ "type": "image_url",
218
+ "image_url": {
219
+ "url": image_data_uri
220
+ }
221
+ }
222
+ ]
223
  }
224
  ],
 
225
  temperature=0,
226
+ max_tokens=100
227
  )
228
+
229
+ raw_text = response.choices[0].message.content or ""
230
+ cleaned_text = clean_alt_text(raw_text)
231
+
232
+ return {
233
+ "alt_text": cleaned_text,
234
+ "model_used": "qwen/qwen3.6-27b",
235
+ "error": ""
236
+ }
237
 
238
 
239
  def process_table(