Spaces:
Paused
Paused
File size: 2,299 Bytes
882bd69 f0efcef 832ce7b 882bd69 fa9231d 5f6d422 832ce7b f0efcef 832ce7b f0efcef fa9231d 0b2a88c f0efcef 9998c92 f0efcef fa9231d 882bd69 f0efcef 882bd69 f0efcef 882bd69 9998c92 f0efcef 9998c92 882bd69 f0efcef 882bd69 f0efcef 882bd69 f0efcef 882bd69 f0efcef 882bd69 f0efcef |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
import torch
from PIL import Image
from transformers import AutoModel, AutoTokenizer, BitsAndBytesConfig
import gradio as gr
import os
# Retrieve the token from environment variables
api_token = os.getenv("HF_TOKEN").strip()
# Configure quantization for efficient memory usage
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_use_double_quant=True,
bnb_4bit_compute_dtype=torch.float16,
)
# Load the model and tokenizer with required arguments
model = AutoModel.from_pretrained(
"ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1",
quantization_config=bnb_config,
device_map="auto",
torch_dtype=torch.float16,
trust_remote_code=True,
attn_implementation="flash_attention_2",
token=api_token # Authenticate with your Hugging Face token
)
tokenizer = AutoTokenizer.from_pretrained(
"ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1",
trust_remote_code=True,
token=api_token # Authenticate with your Hugging Face token
)
# Function to handle input text and image
def process_query(image, question):
try:
# Convert image to RGB format
image = image.convert('RGB')
# Construct the input message
msgs = [{'role': 'user', 'content': [image, question]}]
# Generate response using the model
res = model.chat(
image=image,
msgs=msgs,
tokenizer=tokenizer,
sampling=True,
temperature=0.95,
stream=True
)
# Collect the generated response
generated_text = ""
for new_text in res:
generated_text += new_text
return generated_text
except Exception as e:
return f"Error: {str(e)}"
# Define Gradio interface
iface = gr.Interface(
fn=process_query,
inputs=[
gr.Image(type="pil", label="Upload an Image"),
gr.Textbox(label="Enter a Question")
],
outputs="text",
title="ContactDoctor Multimodal Medical Assistant",
description="Upload an image (e.g., X-ray or skin condition) and ask a medical question."
)
# Launch the Gradio app with API enabled
if __name__ == "__main__":
iface.launch(enable_api=True, share=True) # Enables API and generates a public link
|