sounar commited on
Commit
e59ec29
1 Parent(s): 6732ade

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -15
app.py CHANGED
@@ -1,11 +1,7 @@
1
-
2
-
3
- # Get API token from environment variable
4
- #api_token = os.getenv("HF_TOKEN").strip()
5
-
6
  import gradio as gr
7
  from transformers import AutoModel, AutoTokenizer
8
  import torch
 
9
 
10
  # Load the model and tokenizer
11
  model_name = "ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1"
@@ -13,20 +9,41 @@ model = AutoModel.from_pretrained(model_name, trust_remote_code=True, device_map
13
  tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
14
 
15
  def process_query(image, question):
16
- inputs = {"question": question}
17
- if image:
18
- inputs["image"] = image
19
-
20
- # Process the inputs and generate a response
21
- response = model.chat(image=inputs.get("image"), msgs=[{"role": "user", "content": question}], tokenizer=tokenizer)
22
- return response
23
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  iface = gr.Interface(
25
  fn=process_query,
26
- inputs=[gr.Image(label="Upload Medical Image"), gr.Textbox(label="Question")],
 
 
 
27
  outputs="text",
28
  title="Medical Multimodal Assistant",
29
- description="Upload a medical image and ask your question."
30
  )
31
 
32
  iface.launch()
 
 
 
 
 
 
1
  import gradio as gr
2
  from transformers import AutoModel, AutoTokenizer
3
  import torch
4
+ from PIL import Image
5
 
6
  # Load the model and tokenizer
7
  model_name = "ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1"
 
9
  tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
10
 
11
  def process_query(image, question):
12
+ try:
13
+ # Construct the messages for the model
14
+ msgs = [{"role": "user", "content": question}]
15
+
16
+ # Handle cases with and without an image
17
+ if image is not None:
18
+ # Convert the image to the required format
19
+ image_input = [Image.fromarray(image).convert("RGB")]
20
+ response = model.chat(
21
+ image=image_input,
22
+ msgs=msgs,
23
+ tokenizer=tokenizer,
24
+ )
25
+ else:
26
+ # For text-only queries, omit the `image` parameter
27
+ response = model.chat(
28
+ image=None,
29
+ msgs=msgs,
30
+ tokenizer=tokenizer,
31
+ )
32
+
33
+ return response
34
+ except Exception as e:
35
+ return f"Error: {str(e)}"
36
+
37
+ # Gradio interface
38
  iface = gr.Interface(
39
  fn=process_query,
40
+ inputs=[
41
+ gr.Image(type="numpy", label="Upload Medical Image (Optional)"),
42
+ gr.Textbox(label="Enter Your Medical Question"),
43
+ ],
44
  outputs="text",
45
  title="Medical Multimodal Assistant",
46
+ description="Upload a medical image and/or ask a question for AI-powered assistance.",
47
  )
48
 
49
  iface.launch()