sounar commited on
Commit
9da0a3e
·
verified ·
1 Parent(s): 5da7650

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -11
app.py CHANGED
@@ -25,21 +25,20 @@ model = AutoModel.from_pretrained(
25
  torch_dtype=torch.float16,
26
  trust_remote_code=True,
27
  token=api_token,
28
- revision="main" # Pin to specific revision
29
  )
30
 
31
  tokenizer = AutoTokenizer.from_pretrained(
32
  "ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1",
33
  trust_remote_code=True,
34
  token=api_token,
35
- revision="main" # Pin to specific revision
36
  )
37
 
38
  def analyze_input(image_data, question):
39
  try:
40
  # Handle base64 image if provided
41
  if isinstance(image_data, str) and image_data.startswith('data:image'):
42
- # Extract base64 data after the comma
43
  base64_data = image_data.split(',')[1]
44
  image_bytes = base64.b64decode(base64_data)
45
  image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
@@ -51,14 +50,30 @@ def analyze_input(image_data, question):
51
 
52
  # Process with or without image
53
  if image is not None:
54
- inputs = model.prepare_inputs_for_generation(
55
- input_ids=tokenizer(question, return_tensors="pt").input_ids,
56
- images=[image]
57
- )
 
58
  else:
59
- inputs = tokenizer(question, return_tensors="pt")
 
 
 
60
 
61
- outputs = model.generate(**inputs, max_new_tokens=256)
 
 
 
 
 
 
 
 
 
 
 
 
62
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
63
 
64
  return {
@@ -81,10 +96,10 @@ demo = gr.Interface(
81
  outputs=gr.JSON(label="Analysis"),
82
  title="Bio-Medical MultiModal Analysis",
83
  description="Ask questions with or without an image",
84
- flagging_mode="never" # Updated from allow_flagging
85
  )
86
 
87
- # Launch with simplified parameters
88
  demo.launch(
89
  share=True,
90
  server_name="0.0.0.0",
 
25
  torch_dtype=torch.float16,
26
  trust_remote_code=True,
27
  token=api_token,
28
+ revision="main"
29
  )
30
 
31
  tokenizer = AutoTokenizer.from_pretrained(
32
  "ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1",
33
  trust_remote_code=True,
34
  token=api_token,
35
+ revision="main"
36
  )
37
 
38
  def analyze_input(image_data, question):
39
  try:
40
  # Handle base64 image if provided
41
  if isinstance(image_data, str) and image_data.startswith('data:image'):
 
42
  base64_data = image_data.split(',')[1]
43
  image_bytes = base64.b64decode(base64_data)
44
  image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
 
50
 
51
  # Process with or without image
52
  if image is not None:
53
+ # Prepare inputs for multimodal generation
54
+ model_inputs = {
55
+ "input_ids": tokenizer(question, return_tensors="pt").input_ids.to(model.device),
56
+ "images": [image]
57
+ }
58
  else:
59
+ # Prepare inputs for text-only generation
60
+ model_inputs = {
61
+ "input_ids": tokenizer(question, return_tensors="pt").input_ids.to(model.device)
62
+ }
63
 
64
+ # Generate response with proper inputs
65
+ generation_config = {
66
+ "max_new_tokens": 256,
67
+ "do_sample": True,
68
+ "temperature": 0.7,
69
+ "top_p": 0.9,
70
+ }
71
+
72
+ outputs = model.generate(
73
+ model_inputs=model_inputs,
74
+ **generation_config
75
+ )
76
+
77
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
78
 
79
  return {
 
96
  outputs=gr.JSON(label="Analysis"),
97
  title="Bio-Medical MultiModal Analysis",
98
  description="Ask questions with or without an image",
99
+ flagging_mode="never"
100
  )
101
 
102
+ # Launch the interface
103
  demo.launch(
104
  share=True,
105
  server_name="0.0.0.0",