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4 |
+
|
| 5 |
+
|
| 6 |
+
def is_image(file_path):
|
| 7 |
+
"""Check valid for image file extensions"""
|
| 8 |
+
image_extensions = [
|
| 9 |
+
".jpeg",
|
| 10 |
+
".jpg",
|
| 11 |
+
".png",
|
| 12 |
+
".gif",
|
| 13 |
+
".bmp",
|
| 14 |
+
".tiff",
|
| 15 |
+
".svg",
|
| 16 |
+
".pdf",
|
| 17 |
+
]
|
| 18 |
+
return any(file_path.lower().endswith(ext) for ext in image_extensions)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def encode_image(image_path):
|
| 22 |
+
"""Function to encode the image"""
|
| 23 |
+
with open(image_path, "rb") as image_file:
|
| 24 |
+
return base64.b64encode(image_file.read()).decode("utf-8")
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def construct_conversation_history(chat_history):
|
| 28 |
+
"""Build conversation history"""
|
| 29 |
+
messages = []
|
| 30 |
+
previous_role = None
|
| 31 |
+
combined_content = []
|
| 32 |
+
|
| 33 |
+
for history in chat_history:
|
| 34 |
+
role = history["role"]
|
| 35 |
+
content = history["content"]
|
| 36 |
+
|
| 37 |
+
if isinstance(content, tuple) and len(content) > 0:
|
| 38 |
+
content = content[0]
|
| 39 |
+
|
| 40 |
+
if role == "user":
|
| 41 |
+
if isinstance(content, str) and is_image(content):
|
| 42 |
+
base64_file = encode_image(content)
|
| 43 |
+
part = {
|
| 44 |
+
"type": "image_url",
|
| 45 |
+
"image_url": {"url": f"data:image/jpeg;base64,{base64_file}"},
|
| 46 |
+
}
|
| 47 |
+
else:
|
| 48 |
+
part = {"type": "text", "text": content}
|
| 49 |
+
|
| 50 |
+
if role == previous_role:
|
| 51 |
+
combined_content.append(part)
|
| 52 |
+
else:
|
| 53 |
+
# Only append previous combined content if previous role was user
|
| 54 |
+
if previous_role == "user" and combined_content:
|
| 55 |
+
messages.append(
|
| 56 |
+
{"role": previous_role, "content": combined_content}
|
| 57 |
+
)
|
| 58 |
+
combined_content = [part]
|
| 59 |
+
else:
|
| 60 |
+
# Handle non-user role
|
| 61 |
+
if previous_role == "user" and combined_content:
|
| 62 |
+
messages.append({"role": "user", "content": combined_content})
|
| 63 |
+
combined_content = []
|
| 64 |
+
messages.append({"role": role, "content": content})
|
| 65 |
+
|
| 66 |
+
previous_role = role
|
| 67 |
+
|
| 68 |
+
# Add any remaining combined content after loop ends
|
| 69 |
+
if combined_content:
|
| 70 |
+
messages.append({"role": previous_role, "content": combined_content})
|
| 71 |
+
|
| 72 |
+
return messages
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def construct_latest_conversation(latest_message, messages):
|
| 76 |
+
"""Build latest conversation-
|
| 77 |
+
# Add new user message
|
| 78 |
+
# Extract the text and files"""
|
| 79 |
+
text = latest_message["text"]
|
| 80 |
+
files = latest_message["files"]
|
| 81 |
+
|
| 82 |
+
input_messages = []
|
| 83 |
+
if len(files) > 0:
|
| 84 |
+
for file in files:
|
| 85 |
+
base64_file = encode_image(file)
|
| 86 |
+
input_messages.append(
|
| 87 |
+
{
|
| 88 |
+
"type": "image_url",
|
| 89 |
+
"image_url": {"url": f"data:image/jpeg;base64,{base64_file}"},
|
| 90 |
+
}
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
input_messages.append({"type": "text", "text": text})
|
| 94 |
+
messages.append({"role": "user", "content": input_messages})
|
| 95 |
+
else:
|
| 96 |
+
messages.append({"role": "user", "content": text})
|
| 97 |
+
|
| 98 |
+
return messages
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def chat(message, chat_history, api_key):
|
| 102 |
+
if not api_key.strip():
|
| 103 |
+
return "⚠️ Please enter your GROQ API key first!"
|
| 104 |
+
|
| 105 |
+
try:
|
| 106 |
+
client = Groq(api_key=api_key.strip())
|
| 107 |
+
|
| 108 |
+
# Build conversation history
|
| 109 |
+
messages = construct_conversation_history(chat_history)
|
| 110 |
+
|
| 111 |
+
# Build latest conversation
|
| 112 |
+
messages = construct_latest_conversation(message, messages)
|
| 113 |
+
|
| 114 |
+
chat_completion = client.chat.completions.create(
|
| 115 |
+
messages=messages,
|
| 116 |
+
model="llama-3.2-90b-vision-preview",
|
| 117 |
+
temperature=0.7,
|
| 118 |
+
max_tokens=1024,
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
bot_message = chat_completion.choices[0].message.content
|
| 122 |
+
return bot_message
|
| 123 |
+
except Exception as e:
|
| 124 |
+
return f"❌ Error: {str(e)}"
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
if __name__ == "__main__":
|
| 128 |
+
with gr.Blocks() as app:
|
| 129 |
+
with gr.Accordion("Enter your Groq key!", open=False):
|
| 130 |
+
api_key = gr.Textbox(
|
| 131 |
+
label="GROQ API Key",
|
| 132 |
+
placeholder="Enter your key here...",
|
| 133 |
+
type="password",
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
with gr.Accordion("BioMEDICAL VisionLM AI Tool", open=True):
|
| 137 |
+
gr.ChatInterface(
|
| 138 |
+
title="BioMED⚕️",
|
| 139 |
+
description="> 🚨 This application is designed as a comprehensive AI tool for medical analysis, leveraging advanced multimodal capabilities to assist healthcare professionals and potentially extend access to underserved communities.",
|
| 140 |
+
theme="soft",
|
| 141 |
+
show_progress="full",
|
| 142 |
+
fill_height=True,
|
| 143 |
+
fill_width=True,
|
| 144 |
+
fn=chat,
|
| 145 |
+
additional_inputs=api_key,
|
| 146 |
+
type="messages",
|
| 147 |
+
multimodal=True,
|
| 148 |
+
save_history=True,
|
| 149 |
+
examples=[],
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
app.launch(share=True)
|