mattraj commited on
Commit
2a63a46
·
verified ·
1 Parent(s): 0b0cd22

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -32
app.py CHANGED
@@ -17,28 +17,6 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
17
  model = PaliGemmaForConditionalGeneration.from_pretrained(model_id, torch_dtype=torch.bfloat16).eval().to(device)
18
  processor = PaliGemmaProcessor.from_pretrained(model_id)
19
 
20
- def resize_and_pad(image, target_dim):
21
- # Calculate the aspect ratio
22
- scale_factor = 1
23
- aspect_ratio = image.width / image.height
24
- if aspect_ratio > 1:
25
- # Width is greater than height
26
- new_width = int(target_dim * scale_factor)
27
- new_height = int((target_dim / aspect_ratio) * scale_factor)
28
- else:
29
- # Height is greater than width
30
- new_height = int(target_dim * scale_factor)
31
- new_width = int(target_dim * aspect_ratio * scale_factor)
32
-
33
- resized_image = image.resize((new_width, new_height), Image.LANCZOS)
34
-
35
- # Create a new image with the target dimensions and a white background
36
- new_image = Image.new("RGB", (target_dim, target_dim), (255, 255, 255))
37
- new_image.paste(resized_image, ((target_dim - new_width) // 2, (target_dim - new_height) // 2))
38
-
39
- return new_image
40
-
41
-
42
  ###### Transformers Inference
43
  @spaces.GPU
44
  def infer(
@@ -46,7 +24,7 @@ def infer(
46
  text: str,
47
  max_new_tokens: int
48
  ) -> str:
49
- inputs = processor(text=text, images=resize_and_pad(image, 448), return_tensors="pt", padding="longest", do_convert_rgb=True).to(device).to(dtype=model.dtype)
50
  with torch.no_grad():
51
  generated_ids = model.generate(
52
  **inputs,
@@ -57,16 +35,8 @@ def infer(
57
 
58
  ######## Demo
59
 
60
- INTRO_TEXT = """## Curacel Handwritten Arabic demo\n\n
61
  Finetuned from: google/paligemma-3b-pt-448
62
-
63
-
64
- Translation model demo at: https://prod.arabic-gpt.ai/
65
-
66
- Prompts:
67
- Translate the Arabic to English: {model output}
68
-
69
- The following is a diagnosis in Arabic from a medical billing form we need to translate to English. The transcriber is not necessariily accurate so one or more characters or words may be wrong. Given what is written, what is the most likely diagnosis. Think step by step, and think about similar words or mispellings in Arabic. Give multiple arabic diagnoses along with the translation in English for each, then finally select the diagnosis that makes the most sense given what was transcribed and print the English translation as your most likely final translation. Transcribed text: {model output}
70
  """
71
 
72
  with gr.Blocks(css="style.css") as demo:
 
17
  model = PaliGemmaForConditionalGeneration.from_pretrained(model_id, torch_dtype=torch.bfloat16).eval().to(device)
18
  processor = PaliGemmaProcessor.from_pretrained(model_id)
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  ###### Transformers Inference
21
  @spaces.GPU
22
  def infer(
 
24
  text: str,
25
  max_new_tokens: int
26
  ) -> str:
27
+ inputs = processor(text=text, images=image, return_tensors="pt", padding="longest", do_convert_rgb=True).to(device).to(dtype=model.dtype)
28
  with torch.no_grad():
29
  generated_ids = model.generate(
30
  **inputs,
 
35
 
36
  ######## Demo
37
 
38
+ INTRO_TEXT = """## Curacel Auto Damage demo\n\n
39
  Finetuned from: google/paligemma-3b-pt-448
 
 
 
 
 
 
 
 
40
  """
41
 
42
  with gr.Blocks(css="style.css") as demo: